在junit中执行测试套件的订单 [英] Execute order for test suite in junit

查看:97
本文介绍了在junit中执行测试套件的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的测试套件

I am having a test suite which is having the following structure

TestClass1
      - testmethod1()
      - testmethod2()
      - testmethod3()
      - testmethod4() 
TestClass2
          - testmethod11()
          - testmethod22()
          - testmethod33()
          - testmethod44()

在上面的结构中,我想执行testmethod4()作为最后一个.即)最后执行. 有一个@FixMethodOrder注释,它按顺序执行方法而不是testclass.是否有任何机制可以同时维护测试类和测试方法中的顺序.借助@FixMethodOrder,我可以通过重命名测试方法的名称来执行该方法,但是我无法指示junit将测试类作为最后一个(最后一个)执行.

In the above structure i want to execute the testmethod4() as the final one. ie) executed at last. There is a annotation @FixMethodOrder which executes a method in order not the testclass. Is there any mechanism to maintain order in test class and testmethod together. With the @FixMethodOrder i can execute the method by renaming the name of the test method but i can't instruct junit to execute the test class as the final one(last one).

推荐答案

尽管再次引用@Andy-

Though quoting @Andy again -

您不必关心测试顺序.如果很重要,您已经 测试之间的相互依赖性,因此您正在测试行为+ 相互依存,而不仅仅是行为.您的测试应该可以工作 以任何顺序执行时都是相同的.

You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order.

但是,如果需要这样做,您可以尝试 Suite

But if the need be to do so, you can try out Suite

@RunWith(Suite.class)

@Suite.SuiteClasses({
        TestClass2.class,
        TestClass1.class
})
public class JunitSuiteTest {
}

您可以在其中指定

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {

    @AfterClass
    public void testMethod4() {

,然后小心地命名您的方法testMethod4,使其在末尾执行,或者您也可以使用

and then take care to name your method testMethod4 as such to be executed at the end OR you can also use @AfterClass which could soon be replaced by @AfterAll in Junit5.

看看由Alan Harder控制JUnit测试的顺序

这篇关于在junit中执行测试套件的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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