在surefire执行中的所有测试之前和之后运行代码 [英] Running code before and after all tests in a surefire execution

查看:134
本文介绍了在surefire执行中的所有测试之前和之后运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在测试组执行的整个过程中运行Grizzly HttpServer 。另外,我想在测试中自己从 @Rule 与全局HttpServer实例进行交互。

I have a Grizzly HttpServer that I want to run for the entire duration of a test group execution. Additionally, I want to interact with the global HttpServer instance from a @Rule inside the tests themselves.

因为我使用Maven Surefire而不是使用JUnit测试套件,我不能在 @BeforeClass / @AfterClass 上使用测试套件本身。

Since I'm using Maven Surefire rather than using JUnit test suites, I can't use @BeforeClass/@AfterClass on the test suite itself.

现在,我能想到的就是懒洋洋地初始化静态字段并从 Runtime.addShutdownHook()停止服务器 - 不太好!

Right now, all I can think of is lazily initialising a static field and stopping the server from a Runtime.addShutdownHook() -- not nice!

推荐答案

有两个选项,一个maven解决方案和一个万无一失的解决方案。耦合最少的解决方案是在预集成测试集成后测试阶段执行插件。请参阅构建生命周期简介 - 生命周期参考 。我不熟悉灰熊,但这是一个使用码头的例子:

There are two options, a maven solution and a surefire solution. The least coupled solution is to execute a plugin in the pre-integration-test and post-integration-test phase. See Introduction to the Build Lifecycle - Lifecycle Reference. I'm not familiar with grizzly, but here is an example using jetty:

 <build>
  <plugins>
   <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
     <contextPath>/xxx</contextPath>
    </configuration>
    <executions>
     <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
       <goal>run</goal>
      </goals>
      <configuration>
      </configuration>
     </execution>
     <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals>
       <goal>stop</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

请注意 start 的阶段是预集成测试停止 post-integration-test 。我不确定是否有一个灰熊maven插件,但你可以使用 maven-反而是antrun-plugin

Note that the phase for start is pre-integration-test and stop is post-integration-test. I'm not sure if there is a grizzly maven plugin, but you could use the maven-antrun-plugin instead.

第二个选项是使用JUnit RunListener RunListener 侦听测试事件,例如测试开始,测试结束,测试失败,测试成功等。

The second option is to use a JUnit RunListener. RunListener listens to test events, such as test start, test end, test failure, test success etc.

public class RunListener {
    public void testRunStarted(Description description) throws Exception {}
    public void testRunFinished(Result result) throws Exception {}
    public void testStarted(Description description) throws Exception {}
    public void testFinished(Description description) throws Exception {}
    public void testFailure(Failure failure) throws Exception {}
    public void testAssumptionFailure(Failure failure) {}
    public void testIgnored(Description description) throws Exception {}
}

所以你可以听RunStarted和RunFinished。这些将启动/停止您想要的服务。然后,在surefire中,您可以使用以下命令指定自定义侦听器:

So you could listen for RunStarted and RunFinished. These would start/stop the services you want. Then, in surefire, you can specify a custom listener, using:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <properties>
      <property>
        <name>listener</name>
        <value>com.mycompany.MyResultListener,com.mycompany.MyResultListener2</value>
      </property>
    </properties>
  </configuration>
</plugin>

这是来自 Maven Surefire插件,使用JUnit,使用自定义侦听器和记者

这篇关于在surefire执行中的所有测试之前和之后运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆