【问题标题】:pitest not able to locate junit testPitest 无法找到 junit 测试
【发布时间】:2016-01-22 14:01:03
【问题描述】:

我的 gradle ptest 无法给我正确的结果。它似乎无法找到我的测试文件。

我有以下 build.gradle 文件:

apply plugin: "java" apply plugin: "maven" apply plugin: "info.solidsoft.pitest"

group = "myorg" version = 1.0

repositories {
    mavenCentral() }

sourceSets.all { set ->
    def jarTask = task("${set.name}Jar", type: Jar) {
        baseName = baseName + "-$set.name"
        from set.output
    }

    artifacts {
        archives jarTask
    } }

sourceSets {
    api
    impl    main{       java {          srcDir 'src/api/java'           srcDir 'src/impl/java'      }   }   test {      java {          srcDir 'src/test/java'      }   } }

buildscript {
    repositories {
        mavenCentral()
        //Needed only for SNAPSHOT versions
        //maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.1.6'
    } }

dependencies {
    apiCompile 'commons-codec:commons-codec:1.5'

    implCompile sourceSets.api.output
    implCompile 'commons-lang:commons-lang:2.6'

    testCompile 'junit:junit:4.9'
    testCompile sourceSets.api.output
    testCompile sourceSets.impl.output
    runtime configurations.apiRuntime
    runtime configurations.implRuntime }

jar {
    from sourceSets.api.output
    from sourceSets.impl.output }

pitest { println sourceSets.main

    targetClasses = ['doubler.*']       targetTests  = ['doubler.*']    verbose="on" }

输出存储在正确的文件夹中。当我运行 gradle test 时,它也运行良好。

【问题讨论】:

  • 你有多少个测试班?他们看起来怎么样。输出表明 Pitest 找到了一个测试类,但它没有提供覆盖。

标签: java gradle junit pitest


【解决方案1】:

pitest 用户组中提供了有关此问题的一些附加信息。

https://groups.google.com/forum/#!topic/pitusers/8C7BHh-Vb6Y

正在运行的测试如下所示。

@Test
public void testIt2() {
    assert new DoublerImpl().testIt(1) == 2;
}

Pitest 正确报告这些测试提供了 0% 的类覆盖率。由于使用了assert 关键字,因此没有覆盖范围。

除非在运行测试的 JVM 中设置了 -ea 标志,否则将禁用测试断言。编译器生成的这段代码周围基本上隐藏了if块

@Test
public void testIt2() {
    if (assertionsEnabled) {
      assert new DoublerImpl().testIt(1) == 2;
    }
}

由于未启用断言,因此不执行任何代码。

要解决此问题,请改用内置的 JUnit 断言。

http://junit.sourceforge.net/javadoc/org/junit/Assert.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2018-08-16
    • 2011-03-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多