Gradle编译但不运行TestNG测试

Gradle编译但不运行TestNG测试

本文介绍了Gradle编译但不运行TestNG测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚开始在我们的项目中使用Gradle和TestNG,所以我正在检查失败的任何测试是否实际上失败了构建。我很惊讶地发现它没有。测试被拾取并正确编译,所以我看到类文件。我也得到了运行的报告,但它说0测试(预期2)。运行 gradle clean test -i 给了我以下内容:

 :contentplatform -service:compileTestJava(Thread [Daemon worker Thread 7,5,main])
开始。
:contentplatform-service:compileTestJava
执行任务':contentplatform-service:compileTestJava'(最新检查花费
0.08秒),因为:
输出文件D: \Dev\contentplatform-service\build\classes\test已更改。
输出文件D:\Dev\contentplatform-service\build\dependency-cache已更改。

输出文件D:\Dev\contentplatform-service\build\classes\test\\\
l\xillio\conten
tplatform\service\SuperSimpleTest。班级已被删除。
所有输入文件被认为是过期的增量任务':contentplatfor
m-service:compileTestJava'。
使用JDK Java编译器API进行编译。
:contentplatform-service:compileTestJava(Thread [Daemon worker Thread 7,5,main])
完成。花了0.229秒。
:contentplatform-service:processTestResources(Thread [Daemon worker Thread 7,5,m
ain])启动。
:contentplatform-service:processTestResources
跳过任务':contentplatform-service:processTestResources',因为它没有source
e文件。
:contentplatform-service:processTestResources UP-TO-DATE
:contentplatform-service:processTestResources(Thread [Daemon worker Thread 7,5,m
ain])完成。花了0.001秒。
:contentplatform-service:testClasses(Thread [Daemon worker Thread 7,5,main])sta
rt。
:contentplatform-service:testClasses
跳过任务':contentplatform-service:testClasses',因为它没有动作。
:contentplatform-service:testClasses(Thread [Daemon worker Thread 7,5,main])com
pleted。花了0.001秒。
:contentplatform-service:test(线程[Daemon worker Thread 7,5,main])启动。
:contentplatform-service:test
执行任务':contentplatform-service:test'(最新检查耗时0.049秒
)由于:
输出文件D: \Dev\contentplatform-service\build\test-results\binary\test已更改

输出文件D:\Dev\contentplatform-service\build\测试结果已更改。
输出文件D:\Dev\contentplatform-service\build\reports\tests已更改。
生成测试XML结果(0.0秒)到:D:\Dev\contentplatform -ser
vice \ build \ test-results
生成HTML测试报告...
完成生成测试html结果(0.014秒)到:D:\Dev\contentplatform-
service\build\reports\tests
:contentplatform-service:test(Thread [守护进程工作线程7,5,主要])完成。
花了0.194秒。

SuperSimpleTest.java:

  package nl.xillio.contentplatform.service; 

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test
public class SuperSimpleTest {

@BeforeClass
public void setUp(){
//将在调用时调用的代码这个测试是实例化的
}

@Test
public void testTest(){
Assert.assertEquals(true,true);




build.gradle包含:

$
$ b

  test {
//启用TestNG支持(默认为JUnit)
useTestNG()
scanForTestClasses = false
包含'** / *'

testLogging {
showStandardStreams = true

//将结果记录到构建/测试结果目录
exceptionFormatfull
eventsstarted,passed,skipped,failed,standardOut,standardError
}
}

我已经看过,在那里我发现了使用的提示scanForTestClasses = false 作为解决方法(请参阅)。但是,这个问题似乎没有关系。我在这里做一些其他的noob错误?我怎么才能让SuperSimpleTest执行?



更新:我试着迫使gradle运行一个特定的测试,结果发现一个有趣的错误:

确实是一个愚蠢的noob错误。我们正在使用一个包含代码和测试的同一级别的主项目和多个文件夹的多项目设置。 build.gradle 我偶然使用了只配置主项目来使用TestNG。解决方案是将测试目标包含在 allprojects 子项目闭包中。这里解释:所有项目和子项目之间的区别



所以这里是工作 build.gradle

  def mainClassName ='nl.xillio.contentplatform.view.Run'


//加载所有项目的设置,包括主项目和子项目
allprojects {
apply plugin:'java'
apply plugin:'eclipse'

version'0.1'

repositories {
mavenCentral()
maven {
url'http://mvnrepository.com/maven2'
}
maven {
url'http://download.java .net / maven / 2'
}
}
}

//加载所有子项目(图层)的依赖关系
子项目{
依赖关系{
//要做的事:将这些移动到正确的子项目
编译'javax.inject:javax.inject:1'
compile'org.springframework:spring-context:4.1.4.RELEASE'
compile'org.springframework:spring-core:4.1.4。 RELEASE'
compile'org.springframework:spring-beans:4.1.4.RELEASE'
compile'c​​ommons-logging:commons-logging:1.2'
testCompile'org.springframework:spring- test:4.1.4.RELEASE'
testCompile'org.testng:testng:6.1.1'
}

test {
//启用TestNG支持(默认是JUnit)
useTestNG()
}
}

//解决各个项目的build.gradle文件中图层之间的依赖关系
// (':contentplatform-web'){
}

项目(':contentplatform-service')仅添加GLOBAL构建的特定项目行为:

项目'){
}

项目(':contentplatform-dao'){
}

//返回所有外部列表nal库
def getLibraries(){
return configurations.runtime.filter {!it.name.startsWith('contentplatform')}
}

//复制所有库到libs文件夹
任务copyLibraries(类型:复制){
'Content Platform'
描述'将所有外部库复制到/ libs文件夹'

destinationDir文件('./ build /')

转换为('libs /'){$ b $ from getLibraries()
}
}

//在构建大容器之前执行构建任务
jar {
'Content Platform'
描述'将所有图层和依赖关系打包成一个大容器。

//库需要构建
dependsOn(copyLibraries)

//最后一个大jar需要所有图层
dependencies {
编译项目(':contentplatform-web'),项目(':contentplatform-dao'),
项目(':contentplatform-service' ),project(':contentplatform-model')
}
//创建一个MANIFEST.MF文件,主要的类文件位于web层
manifest {
attributes' Implementation-Title':'Content Platform',
'Implementation-Version':version,
'Built-By':System.getProperty('user.name'),
'Built- Date':new Date(),
'Built-JDK':System.getProperty('java.version'),
'Class-Path':getLibraries()。collect {'../ libs /'+ it.getName()}。join(''),
'Main-Class':mainClassName
}

//在fat jar中包含图层
from(configurations.compile.filter {it.name.startsWith('contentplatform')}。collect {it.isDirectory()?它:zipTree(it)}){
排除META-INF / *。SF
排除META-INF / * .DSA
排除META-INF / * .RSA
}

//将fat jar保存在文件夹的根目录下,而不是build / libs
destinationDir文件('。')
}


We just started using Gradle and TestNG on our project, so I am checking whether failing any test actually fails the build. I was quite surprised to see it does not. The tests are picked up and compiled correctly, so I see the class files. Also I do get a report of the run, but it says 0 tests (expected 2). Running gradle clean test -i gives me the following:

:contentplatform-service:compileTestJava (Thread[Daemon worker Thread 7,5,main])
 started.
:contentplatform-service:compileTestJava
Executing task ':contentplatform-service:compileTestJava' (up-to-date check took
 0.08 secs) due to:
  Output file D:\Dev\contentplatform-service\build\classes\test has changed.
  Output file D:\Dev\contentplatform-service\build\dependency-cache has changed.

  Output file D:\Dev\contentplatform-service\build\classes\test\nl\xillio\conten
tplatform\service\SuperSimpleTest.class has been removed.
All input files are considered out-of-date for incremental task ':contentplatfor
m-service:compileTestJava'.
Compiling with JDK Java compiler API.
:contentplatform-service:compileTestJava (Thread[Daemon worker Thread 7,5,main])
 completed. Took 0.229 secs.
:contentplatform-service:processTestResources (Thread[Daemon worker Thread 7,5,m
ain]) started.
:contentplatform-service:processTestResources
Skipping task ':contentplatform-service:processTestResources' as it has no sourc
e files.
:contentplatform-service:processTestResources UP-TO-DATE
:contentplatform-service:processTestResources (Thread[Daemon worker Thread 7,5,m
ain]) completed. Took 0.001 secs.
:contentplatform-service:testClasses (Thread[Daemon worker Thread 7,5,main]) sta
rted.
:contentplatform-service:testClasses
Skipping task ':contentplatform-service:testClasses' as it has no actions.
:contentplatform-service:testClasses (Thread[Daemon worker Thread 7,5,main]) com
pleted. Took 0.001 secs.
:contentplatform-service:test (Thread[Daemon worker Thread 7,5,main]) started.
:contentplatform-service:test
Executing task ':contentplatform-service:test' (up-to-date check took 0.049 secs
) due to:
  Output file D:\Dev\contentplatform-service\build\test-results\binary\test has
changed.
  Output file D:\Dev\contentplatform-service\build\test-results has changed.
  Output file D:\Dev\contentplatform-service\build\reports\tests has changed.
Finished generating test XML results (0.0 secs) into: D:\Dev\contentplatform-ser
vice\build\test-results
Generating HTML test report...
Finished generating test html results (0.014 secs) into: D:\Dev\contentplatform-
service\build\reports\tests
:contentplatform-service:test (Thread[Daemon worker Thread 7,5,main]) completed.
 Took 0.194 secs.

SuperSimpleTest.java:

package nl.xillio.contentplatform.service;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test
public class SuperSimpleTest {

    @BeforeClass
    public void setUp() {
        // code that will be invoked when this test is instantiated
    }

    @Test
    public void testTest() {
        Assert.assertEquals(true, true);
    }
}

build.gradle contains:

test {
    // enable TestNG support (default is JUnit)
    useTestNG()
    scanForTestClasses = false
    include '**/*'

    testLogging {
        showStandardStreams = true

        // log results to "build/test-results" directory
        exceptionFormat "full"
        events "started", "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

I already took a look at other questions about this topic, and there I found the hint to use scanForTestClasses = false as a workaround (see https://issues.gradle.org/browse/GRADLE-1682). However, this problem seems unrelated. Am I making some other noob mistake here? How can I get SuperSimpleTest to just execute?

UPDATE: I tried forcing gradle to run a specific test with an interesting error as a result: Gradle complains about JUnit version on TestNG task

解决方案

It was a stupid noob mistake indeed. We are using a multi-project setup with a master project and several folders on the same level, which contain the code and the tests. The build.gradle I was using accidentally only configured the master project to use TestNG. The solution is to include the test target within the allprojects or subprojects closure. Which is explained here: What is the difference between allprojects and subprojects

So here is the working build.gradle:

def mainClassName = 'nl.xillio.contentplatform.view.Run'


// Load settings for all projects including master and subprojects
allprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'

    version '0.1'

    repositories {
        mavenCentral()
        maven {
            url 'http://mvnrepository.com/maven2'
        }
        maven {
            url 'http://download.java.net/maven/2'
        }
    }
}

// Load the dependencies for all subprojects (layers)
subprojects {
    dependencies {
    //to do: move these to the correct subprojects
        compile 'javax.inject:javax.inject:1'
        compile 'org.springframework:spring-context:4.1.4.RELEASE'
        compile 'org.springframework:spring-core:4.1.4.RELEASE'
        compile 'org.springframework:spring-beans:4.1.4.RELEASE'
        compile 'commons-logging:commons-logging:1.2'
        testCompile 'org.springframework:spring-test:4.1.4.RELEASE'
        testCompile 'org.testng:testng:6.1.1'
    }

    test {
        // enable TestNG support (default is JUnit)
        useTestNG()
    }
}

// Resolve the dependencies between the layers in the individual project's build.gradle files
// add ONLY specific per project behaviour of the GLOBAL build here:

project(':contentplatform-web') {
}

project(':contentplatform-service') {
}

project(':contentplatform-dao') {
}

// Return a list of all the external libraries
def getLibraries() {
    return configurations.runtime.filter{!it.name.startsWith('contentplatform')}
}

// Copy all the libraries to a libs folder
task copyLibraries(type: Copy) {
    group 'Content Platform'
    description 'Copy all the external libraries to the /libs folder.'

    destinationDir file('./build/')

    into('libs/') {
        from getLibraries()
    }
}

// Perform the build task before building the big jar
jar {
    group 'Content Platform'
    description 'Package all the layers and dependencies into a big jar.'

    // The libraries are required to build
    dependsOn(copyLibraries)

    // The final big jar needs all the layers
    dependencies {
        compile project(':contentplatform-web'), project(':contentplatform-dao'),
            project(':contentplatform-service'), project(':contentplatform-model')
    }
    // Create a MANIFEST.MF file, the main class file is in the web layer
    manifest {
        attributes 'Implementation-Title': 'Content Platform',
            'Implementation-Version': version,
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Built-JDK': System.getProperty('java.version'),
            'Class-Path': getLibraries().collect{'../libs/' + it.getName()}.join(' '),
            'Main-Class': mainClassName
    }

    // Include the layers in the fat jar
    from(configurations.compile.filter{it.name.startsWith('contentplatform')}.collect{it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }

    // Save the fat jar in the root of the folder instead of in build/libs
    destinationDir file('.')
}

这篇关于Gradle编译但不运行TestNG测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:10