【问题标题】:Configure Gradle test execution order配置 Gradle 测试执行顺序
【发布时间】:2021-03-15 12:02:11
【问题描述】:

在我的 Spring Boot 应用程序中,我有一套 Gradle 测试,可以使用 ./gradlew test 运行。有没有一种方法可以让我在所有其他测试包之前运行一个测试包,例如com/example/myapp/firstpackage/**.

我知道我可以仅使用以下配置运行这些测试

test {
  useJUnitPlatform()
  include 'com/example/myapp/firstpackage/**'
}

但我真正想做的是在任务开始时运行这个包中的测试,然后运行其余的。

【问题讨论】:

    标签: java gradle junit junit5


    【解决方案1】:

    在 Junit 5 中无法做到这一点。

    你必须切换到 junit 4 - 执行明智。

    一种方法是

    1. 创建一个 TestSuite 类 - 测试类的基本列表
      见https://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner-test-suite
    @RunWith(JUnitPlatform.class)
    @SuiteDisplayName("JUnit Platform Ordered Suite ")
    @SelectClasses({Test1.class, Test2.class, Test3.class, Test4.class})
    public class TestOrderedSuite {
    }
    
    
    1. 将 gradle 测试从 useJUnitPlatform 更改为 useJunit
    dependencies {
        testCompileOnly 'junit:junit:4.13'
        testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0'
        testImplementation 'org.junit.platform:junit-platform-suite-api:1.7.0'
        testImplementation 'org.junit.platform:junit-platform-runner:1.7.0'
    
        testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.7.0'
    }
    
    test {
    //    useJUnitPlatform()
        useJUnit()
    }
    
    

    【讨论】:

    • 我对 JUnit 5 的依赖太多,在我的情况下不可行
    【解决方案2】:

    添加一个测试任务(我们称之为冒烟测试)并让默认的测试任务依赖它

    task smokeTest(type: Test) {
      useJUnitPlatform()
      include 'com/example/myapp/firstpackage/**'
    }
    
    test {
       useJUnitPlatform()
       exclude 'com/example/myapp/firstpackage/**'
    }
    

    【讨论】:

    • 这不起作用。 com/example/myapp/firstpackage/** 中的类将运行两次,首先是 smokeTest,然后是 test。在后一种情况下它们将失败。
    • 不幸的是,它仍然没有真正起作用,因为 firstpackage 中的测试是在一个单独的任务中运行的。这将产生 2 个独立的 Junit 测试报告、代码覆盖率报告等。所以我需要在同一个任务中运行所有测试,但首先在 firstpackage 中运行这些测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2021-06-22
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多