Java 使用TestNG和Gradle从测试运行中排除文件夹

Java 使用TestNG和Gradle从测试运行中排除文件夹,java,unit-testing,selenium,gradle,gradlew,Java,Unit Testing,Selenium,Gradle,Gradlew,我正在尝试排除为需要更新且不希望运行的Selenium测试设置的“隔离”文件夹。我知道一个解决方案是为这些类中的测试设置和分配测试组,但是考虑到这里将要进行的测试的规模和数量,我宁愿使用Ant风格的过滤器 下面是我的build.gradle文件的一个片段: apply plugin: 'java' apply plugin: 'idea' apply plugin: 'eclipse' repositories { mavenCentral() } dependencies {

我正在尝试排除为需要更新且不希望运行的Selenium测试设置的“隔离”文件夹。我知道一个解决方案是为这些类中的测试设置和分配测试组,但是考虑到这里将要进行的测试的规模和数量,我宁愿使用Ant风格的过滤器

下面是我的
build.gradle
文件的一个片段:

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.seleniumhq.selenium:selenium-java:2.35.0"
    compile "org.testng:testng:5.14.10"
    testCompile('org.uncommons:reportng:1.1.2') {
        exclude group: 'org.testng'
    }
    testCompile "junit:junit:4.8.2"
    compile "com.jayway.restassured:rest-assured:1.8.1"
}

//initialize thread count variable for parallel testing and default to 1
def threadCount = System.getProperty("MAXTHREADS", "1")

tasks.withType(Test) {
    maxParallelForks = 1
    forkEvery = 1000
    ignoreFailures = false

    // Pass all system properties to the tests
    systemProperties = System.getProperties()

    // Makes the standard streams (err and out) visible at console when running tests
    testLogging.showStandardStreams = true

    exclude '**/tasks/'
    exclude '**/disabled/'

    classpath += configurations.testCompile
}

task firefox(type: Test) {
    maxParallelForks = Integer.valueOf(threadCount) //default is 1 if not specified
    testLogging.events "started"
    testLogging {
        events "started", "passed", "skipped", "failed", "standardOut", "standardError"
        exceptionFormat "full" // default is "short"
    }
    useTestNG() {
        excludeGroups 'chrome'
        useDefaultListeners = false
        listeners << 'org.uncommons.reportng.HTMLReporter'
        listeners << 'org.uncommons.reportng.JUnitXMLReporter'
        listeners << 'com.xmatters.testng.Listener'
    }

    testResultsDir = file("${buildDir}/test-results/firefox")
    testReportDir = file("${reporting.baseDir}/firefox")

    systemProperties.BROWSER = System.getProperty('BROWSER', 'firefox')

    exclude '**/selenium/'
    exclude '**/setupscripts/'
}

task chrome(type: Test) {
    maxParallelForks = Integer.valueOf(threadCount) //default is 1 if not specified
    testLogging.events "started"
    useTestNG() {
        useDefaultListeners = false;
        listeners << 'org.uncommons.reportng.HTMLReporter'
        listeners << 'org.uncommons.reportng.JUnitXMLReporter'
        listeners << 'com.xmatters.testng.Listener'
    }

    testResultsDir = file("${buildDir}/test-results/chrome")
    testReportDir = file("${reporting.baseDir}/chrome")

    systemProperties.BROWSER = System.getProperty('BROWSER', 'chrome')

    exclude '**/selenium/'
    exclude '**/setupscripts/'
}
apply插件:“java”
应用插件:“创意”
应用插件:“eclipse”
存储库{
mavenCentral()
}
依赖关系{
编译“org.seleniumhq.selenium:seleniumjava:2.35.0”
编译“org.testng:testng:5.14.10”
testCompile('org.uncommons:reportng:1.1.2'){
排除组:“org.testng”
}
testCompile“junit:junit:4.8.2”
编译“com.jayway.restassed:rest-assured:1.8.1”
}
//初始化并行测试的线程计数变量,默认值为1
def threadCount=System.getProperty(“MAXTHREADS”,“1”)
任务。withType(测试){
maxParallelForks=1
forkEvery=1000
忽略失败=错误
//将所有系统属性传递给测试
systemProperties=System.getProperties()
//使标准流(err和out)在运行测试时在控制台上可见
testLogging.showStandardStreams=true
排除'**/tasks/'
排除'**/disabled/'
classpath+=configurations.testCompile
}
任务firefox(类型:测试){
maxParallelForks=Integer.valueOf(threadCount)//如果未指定,则默认值为1
testLogging.events“已启动”
测试记录{
事件“已启动”、“已通过”、“已跳过”、“失败”、“标准输出”、“标准错误”
exceptionFormat“full”//默认为“short”
}
useTestNG(){
排除组“chrome”
useDefaultListeners=false

侦听器看起来排除模式应用于文件的相对路径(即相对于根文件夹),这解释了为什么它适用于根文件夹下的文件夹

使用excludeSpec(请参见)应该可以正常工作:

exclude { it.file.canonicalPath.contains('/disabled/')}

当然,根据您的操作系统,请注意/vs\。

看起来排除模式应用于文件的相对路径(即相对于根文件夹),这解释了为什么它适用于根文件夹下的文件夹

使用excludeSpec(请参见)应该可以正常工作:

exclude { it.file.canonicalPath.contains('/disabled/')}
当然,根据您的操作系统,请注意/vs\