我有一个具有以下结构的测试套件
TestClass1
- testmethod1()
- testmethod2()
- testmethod3()
- testmethod4()
TestClass2
- testmethod11()
- testmethod22()
- testmethod33()
- testmethod44()
在上面的结构中,我想执行 testmethod4() 作为最后一个。即)最后执行。 有一个注释@FixMethodOrder,它按顺序而不是测试类执行方法。是否有任何机制可以一起维护测试类和测试方法中的顺序。使用@FixMethodOrder,我可以通过重命名测试方法的名称来执行该方法,但我无法指示junit将测试类作为最后一个(最后一个)执行。
最佳答案
尽管再次引用@Andy -
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
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestClass2.class,
TestClass1.class
})
public class JunitSuiteTest {
}
您可以在其中指定
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {
@AfterClass
public void testMethod4() {
然后注意将您的方法命名为testMethod4
,以便在最后执行,或者您也可以使用 @AfterClass
很快就会被 @AfterAll
取代在 Junit5 中。
关于java - 在junit中执行测试套件的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353606/