java - 在junit中执行测试套件的命令

标签 java junit

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

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 中。

请查看Controlling the Order of the JUnit test by Alan Harder

关于java - 在junit中执行测试套件的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353606/

相关文章:

java - Junit 用于可运行类和队列

android - 使用 Robolectric 在两个(或更多)Android 版本上运行 Android 测试

java - 在 Junit 中测试异常时忽略堆栈跟踪

java - SimGrid。同时接收两个任务

java - WebDriver 中的 get() 和 navigate()

java - Lucene:首先不显示精确匹配

java - 无法使用 Maven 运行 JUnit5 测试

java - 按一次主页按钮后游戏不会重新开始。

java - IntelliJ - 所选目录不是 JDK 的主目录

java - 用于忽略 JUnit 测试的 FindBugs 过滤器文件