【发布时间】:2013-01-19 09:08:03
【问题描述】:
我使用fest 进行了一些单元测试,现在我需要在无头系统上使用 maven 运行 mvn 我的测试。我想使用 Xvfb 运行测试,但我需要帮助配置 maven 以在测试前启动 Xvfb 并在所有完成后停止它。
【问题讨论】:
标签: java unit-testing maven junit xvfb
我使用fest 进行了一些单元测试,现在我需要在无头系统上使用 maven 运行 mvn 我的测试。我想使用 Xvfb 运行测试,但我需要帮助配置 maven 以在测试前启动 Xvfb 并在所有完成后停止它。
【问题讨论】:
标签: java unit-testing maven junit xvfb
用exec-maven-plugin:
您必须定义两个执行,一个用于启动服务器,一个用于停止它。您必须将这些执行配置与适当的 maven 阶段联系起来——在测试阶段之前启动 Xvfb,并在测试阶段之后停止 Xvfb。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>exec-at-test-compile</id>
<phase>test-compile</phase> <!-- runs right before 'test' -->
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>/home/anew/bin/manage-xvfb.sh</executable>
<arguments>
<argument>start</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>exec-at-prepare-package</id>
<phase>prepare-package</phase> <!-- runs right after 'test' -->
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>/home/anew/bin/manage-xvfb.sh</executable>
<arguments>
<argument>stop</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
这是manage-xvfb.sh脚本的内容:
#!/bin/bash
XVFB_CMD="sudo /usr/bin/Xvfb :15 -ac -screen 0 1024x768x8"
function stop_xvfb {
XVFB_PID=`ps ax | pgrep "Xvfb"`
if [ "${XVFB_PID}" != "" ]; then
sudo kill ${XVFB_PID}
fi
}
if [ "${1}" == "start" ]; then
stop_xvfb
${XVFB_CMD} &
elif [ "${1}" == "stop" ]; then
stop_xvfb
fi
请注意,您需要在 sudoers 文件中设置 NOPASSWD。
【讨论】:
kill `ps axuf | pgrep Xvfb` 之类的操作才能杀死它。 b) 如果 Xvfb 未在行尾使用 & 运算符运行,则启动 Xvfb,以便您的 shell 脚本可以退出,但 Xvfb 将异步运行,如下所示:/usr/X11R6/bin/Xvfb :15 -ac -screen 0 1024x768x8 &
其实我是用这个插件配置的:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>start-xvfb</id>
<phase>process-test-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Starting xvfb ..." />
<exec executable="Xvfb" spawn="true">
<arg value=":1" />
</exec>
</tasks>
</configuration>
</execution>
<execution>
<id>shutdown-xvfb</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Ending xvfb ..." />
<exec executable="killall">
<arg value="Xvfb" />
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
好处是你得到了一个后台进程(spawn = "true"),你可以杀死 Xvfb 进程(killall)而无需编写任何脚本。此外,在我的 Ubuntu 发行版中,我不必在我的 sudoers 文件中设置任何特殊设置来让它工作。
shutdown-xvfb 执行在测试阶段结束时执行,但如果测试失败(这里是问题)不执行。如果要重新启动另一个测试,这不是问题(旧的 Xvfb 仍在运行,新的无法运行,但这不是问题),但问题是 Xvfb 仍然忙于资源。解决此问题的方法可能是将testFailureIgnore = "true" 添加到 maven-surefire-plugin 的配置中,但这样我无法轻易查看某些测试是否失败。
【讨论】:
start-xvfb 执行中开始Xvfb 之前添加<exec executable="killall"><arg value="Xvfb" /></exec>?或者您是否最担心不让 Xvfb 因另一个(童子军营地)原因而运行?或者使用像Jenkins这样的CI环境,在失败时触发其他构建(即“停止Xvfb”构建)是微不足道的。
这项工作可以通过selenium-maven-plugin 轻松完成:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<executions>
<execution>
<id>setup-xvfb</id>
<phase>pre-integration-test</phase>
<goals>
<goal>xvfb</goal>
</goals>
</execution>
</executions>
</plugin>
不幸的是,这个插件似乎没有维护。它是最近从codehaus.org 迁移到github.com 的mojohaus project 的一部分。似乎还没有人将selenium-maven-plugin 移植到 github,因此目前在 mojohaus 的 github 页面上没有可用的资源和文档。
但是,Maven Central 上提供了 JAR,并且该插件仍然可以使用。如果您需要查找更多配置参数,可以在 github here 上找到源和站点的一个分支。
【讨论】: