在模块中自定义 Junit 测试套件的执行

发布于 2024-10-16 02:36:45 字数 549 浏览 2 评论 0原文

我有一组 Junit 4 测试类,我想在具有不同 BeforeClass、AfterClass、Before 和 After 挂钩的多个模块中运行它们。通过 @Rule 注入,我只能得到测试方法的包装,但没有 BeforeClass 和 AfterClass 行为。

另外,我不想使用测试运行程序执行此操作,因为从那时起我已经修复了测试运行程序以用于大量测试。

对目标模块中的每个测试类进行子类化并在那里应用自定义似乎不是一个好的解决方案。

最好的方法是在目标模块中声明类似的内容

@RunWith(Suite.class)
@Suite.SuiteClasses({
  investmentTests.class,
  catalogTests.class,
  markerTests.class 
})
public class AllTests {
    // why on earth I need this class, I have no idea! 
}

,并使用一些环境挂钩来应用之前/之后的代码。

您遇到过这个问题的解决方案吗?

I have a set of Junit 4 test classes which I want to run in multiple modules with differing BeforeClass, AfterClass, Before and After hooks. With @Rule injections I get only wrapping of test methods, but no BeforeClass and AfterClass behaviour.

Also I don't want to do this with a test runner, since then I have fixed the test runner to be used for a large set of tests.

Subclassing each test class in the target modules and applying the customizations there doesn't seem to be a good solution.

The best would be to just declared something like this in the target modules

@RunWith(Suite.class)
@Suite.SuiteClasses({
  investmentTests.class,
  catalogTests.class,
  markerTests.class 
})
public class AllTests {
    // why on earth I need this class, I have no idea! 
}

and have some environmental hooks to apply the before/after code.

Have you come across a solution for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2024-10-23 02:36:45

正如 NamshubWriter 所评论的,org.junit.ClassRule 是在 JUnit4.9 中找到的。 @ClassRule 而不是 @RuleTestRule 而不是 MethodRule

查看示例:

public static class CustomCounter extends TestRule {
    public int count = 0;

    @Override
    protected Statement apply(final Statement base, Description description) {
        return new Statement() {                
            @Override
            public void evaluate() throws Throwable {
                count++;
                base.evaluate();
            }
        };
    }       
}

public static class ExampleTestWithCustomClassRule {
    @ClassRule
    public static CustomCounter counter= new CustomCounter();

    @Test
    public void firstTest() {
        assertEquals(1, counter.count);
    }

    @Test
    public void secondTest() {
        assertEquals(1, counter.count);
    }
}

As NamshubWriter commented, org.junit.ClassRule is found in JUnit4.9. @ClassRule instead of @Rule; TestRule instead of MethodRule.

See the sample:

public static class CustomCounter extends TestRule {
    public int count = 0;

    @Override
    protected Statement apply(final Statement base, Description description) {
        return new Statement() {                
            @Override
            public void evaluate() throws Throwable {
                count++;
                base.evaluate();
            }
        };
    }       
}

public static class ExampleTestWithCustomClassRule {
    @ClassRule
    public static CustomCounter counter= new CustomCounter();

    @Test
    public void firstTest() {
        assertEquals(1, counter.count);
    }

    @Test
    public void secondTest() {
        assertEquals(1, counter.count);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文