使用gradle测试时,未发现给定的测试包括

时间:2015-03-18 10:32:15

标签: testing groovy gradle

以下是我的代码:

package ro

import org.junit.Test

/**
 * Created by roroco on 3/18/15.
 */
class TryTest extends GroovyTestCase {

    @Test
    def testSmth() {
        assert 1 == 1
    }

}

然后我用'gradle test --tests ro.TryTest'运行它,它引发:

ro.TryTest > junit.framework.TestSuite$1.warning FAILED
    junit.framework.AssertionFailedError at TestSuite.java:97

1 test completed, 1 failed
:test FAILED

这里是source

1 个答案:

答案 0 :(得分:3)

测试需要为void返回GroovyTestCase,因此您的测试类应为:

package ro

/**
 * Created by roroco on 3/18/15.
 */
class TryTest extends GroovyTestCase {
    void testSmth() {
        assert 1 == 1
    }
}

此外,您的build.gradle文件根据定义不需要java AND groovy插件,groovy导入java,因此您的文件可以是:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.1'
    testCompile 'junit:junit:4.12'
}

作为一个无关的方面,我倾向于使用Spock代替GroovyTestCase这些天,所以如果你添加:

testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'

到您的依赖项,然后您可以编写Spock测试(这将进入src/test/groovy/ro/TrySpec.groovy

package ro

class TrySpec extends spock.lang.Specification {
    def 'a simple test'() {
        when: 'I have a number'
            def number = 1

        then: 'It should equal 1'
            number == 1
    }
}