jUnit 4.x 中的 Suite 执行挂钩之前和之后 [英] Before and After Suite execution hook in jUnit 4.x

查看:29
本文介绍了jUnit 4.x 中的 Suite 执行挂钩之前和之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一组集成测试执行设置和拆卸,使用 jUnit 4.4 来执行测试.拆卸需要可靠地运行.我在使用 TestNG 时遇到了其他问题,所以我希望移植回 jUnit.在运行任何测试之前和完成所有测试之后,哪些挂钩可用于执行?

I'm trying to preform setup and teardown for a set of integration tests, using jUnit 4.4 to execute the tests. The teardown needs to be run reliably. I'm having other problems with TestNG, so I'm looking to port back to jUnit. What hooks are available for execution before any tests are run and after all tests have completed?

注意:我们使用 maven 2 进行构建.我试过使用 maven 的 pre- &post-integration-test 阶段,但是,如果测试失败,maven 会停止并且不运行 post-integration-test,这无济于事.

Note: we're using maven 2 for our build. I've tried using maven's pre- & post-integration-test phases, but, if a test fails, maven stops and doesn't run post-integration-test, which is no help.

推荐答案

是的,可以在测试套件中的任何测试之前和之后可靠地运行设置和拆除方法.让我用代码来演示:

Yes, it is possible to reliably run set up and tear down methods before and after any tests in a test suite. Let me demonstrate in code:

package com.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class})
public class TestSuite {

    @BeforeClass
    public static void setUp() {
        System.out.println("setting up");
    }

    @AfterClass
    public static void tearDown() {
        System.out.println("tearing down");
    }

}

所以你的 Test1 类看起来像:

So your Test1 class would look something like:

package com.test;

import org.junit.Test;


public class Test1 {
    @Test
    public void test1() {
        System.out.println("test1");
    }

}

...你可以想象 Test2 看起来很相似.如果你运行 TestSuite,你会得到:

...and you can imagine that Test2 looks similar. If you ran TestSuite, you would get:

setting up
test1
test2
tearing down

所以你可以看到设置/拆卸只在所有测试之前和之后分别运行.

So you can see that the set up/tear down only run before and after all tests, respectively.

问题:这仅在您运行测试套件时有效,而不是将 Test1 和 Test2 作为单独的 JUnit 测试运行.您提到您正在使用 maven,并且 maven surefire 插件喜欢单独运行测试,而不是套件的一部分.在这种情况下,我建议创建一个每个测试类都扩展的超类.然后超类包含带注释的@BeforeClass 和@AfterClass 方法.虽然不如上面的方法干净,但我认为它对你有用.

The catch: this only works if you're running the test suite, and not running Test1 and Test2 as individual JUnit tests. You mentioned you're using maven, and the maven surefire plugin likes to run tests individually, and not part of a suite. In this case, I would recommend creating a superclass that each test class extends. The superclass then contains the annotated @BeforeClass and @AfterClass methods. Although not quite as clean as the above method, I think it will work for you.

对于测试失败的问题,您可以设置 maven.test.error.ignore 以便在失败的测试上继续构建.不建议将此作为持续实践,但它应该可以让您正常工作,直到您的所有测试都通过为止.有关更多详细信息,请参阅 maven surefire 文档.

As for the problem with failed tests, you can set maven.test.error.ignore so that the build continues on failed tests. This is not recommended as a continuing practice, but it should get you functioning until all of your tests pass. For more detail, see the maven surefire documentation.

这篇关于jUnit 4.x 中的 Suite 执行挂钩之前和之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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