【问题标题】:JUnit Listener Configuration In GradleGradle 中的 JUnit 监听器配置
【发布时间】:2019-03-26 20:07:38
【问题描述】:

我是 Gradle 的新手。有自定义 JUnit 监听器,它读取自定义注释数据并生成报告,需要将其配置为 Gradle 的一部分。无论如何在 Gradle 4.4 中配置下面的surefire插件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <properties>
            <property>
                <name>listener</name>
                <value>my.company.MyRunListener</value>
            </property>
        </properties>
    </configuration>
</plugin>

我了解,可能无法像在 gradle 中那样使用 maven 插件。我检查了TestListener,它不支持读取注释来继续。

我想了解如何在 Gradle 中配置我的 JUnit 侦听器。

【问题讨论】:

    标签: gradle junit maven-surefire-plugin


    【解决方案1】:

    恐怕,Gradle 中目前不支持 JUnit RunListeners。只有一个请求该功能的开放票:https://github.com/gradle/gradle/issues/1330

    由于有人在那张票上有 mentioned in the comments,“主要问题 [...] 是 Gradle 中没有 TestDescriptor.getAnnotations()”;否则你可能已经能够将你的 RunListener 重写为 Gradle TestListener。因此,除非我在浏览门票时错过了什么,否则您目前似乎大多不走运:-(

    【讨论】:

    • 谢谢。@Chriki,根据链接,Gradle 中不支持 JUnit 侦听器。如果团队决定不支持 JUnit 侦听器,Tbink Gradle 团队可以在他们的TestDescriptor 中解决所有类型的需求。让我试试 baev 建议的Aspects 方式,
    • @skc:听起来不错,我很乐意在这里阅读您的实验结果!另外,如果您觉得我的回答有用,请考虑投票 :-)
    • @skc:JUnit Foundation 库使您能够通过 ServiceLoader 附加 JUnit 4 RunListener 类,就像使用 JUnit 5 一样。有关详细信息,请参阅下面的答案。
    【解决方案2】:

    JUnit Foundation 库使您能够在服务提供者配置文件中声明您的侦听器,然后自动附加这些侦听器 - 无论执行环境如何。详情可见here

    JUnit Foundation 的 Gradle 配置

    // build.gradle
    ...
    apply plugin: 'maven'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    repositories {
        mavenLocal()
        mavenCentral()
        ...
    }
    dependencies {
        ...
        compile 'com.nordstrom.tools:junit-foundation:12.2.0'
    }
    ext {
        junitFoundation = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { it.name == 'junit-foundation' }
    }
    test.doFirst {
        jvmArgs "-javaagent:${junitFoundation.file}"
    }
    test {
    //  debug true
        // not required, but definitely useful
        testLogging.showStandardStreams = true
    }
    

    ServiceLoader 提供者配置文件

    # src/main/resources/META-INF/services/com.nordstrom.automation.junit.JUnitWatcher
    com.example.MyRunListener
    

    使用此配置,由 MyRunListener 实现的侦听器将自动附加到提供给 JUnit 运行器的 run() 方法的 RunNotifier。此功能消除了各种测试执行环境(如 Maven、Gradle 和本机 IDE 测试运行器)之间的行为差​​异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 2018-05-17
      • 2013-09-08
      • 2011-07-16
      • 2017-03-02
      • 2020-01-27
      • 1970-01-01
      相关资源
      最近更新 更多