繁体   English   中英

如何在Xvfb上启动mvn测试阶段?

[英]How do I start mvn test phase on Xvfb?

我使用fest进行了一些单元测试,现在我需要在无头系统上使用maven运行mvn我的测试。 我想使用Xvfb运行测试,但我需要帮助来配置maven在测试之前启动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

其实我使用这个插件配置:

<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无法运行,但这不是问题),这不是问题,但问题是Xvfb资源仍然很忙。 解决方法可能是将testFailureIgnore = "true"添加到maven-surefire-plugin的配置中,但这样我就不能轻易看出某些测试是否失败。

这项工作可以通过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>

不幸的是,这个插件似乎没有得到维护。 这是一部分mojohaus项目最近从迁移codehaus.orggithub.com 似乎还没有人将selenium-maven-plugin移植到github,因此mojohaus的github页面上目前没有源代码和文档。

但是,JAR可以在Maven Central上使用 ,插件仍然可以使用。 还有就是在GitHub上可用的来源和网站叉子在这里 ,如果你需要查找一些配置参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM