从TestNG和Gradle的测试运行中排除文件夹

时间:2013-10-28 22:50:54

标签: java unit-testing selenium gradle gradlew

我正在尝试排除我为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/'
}

在第34行,您可以看到我添加的exclude '**/disabled/'。此文件夹与根文件夹相差无几。与exclude '**/tasks/'之前的相似内容已经在构建文件中,并且似乎与类似的目录结构一起正常工作。

当我运行构建时,/ disabled /文件夹中的测试仍在运行。我有什么问题吗?我假设使用该语法,scanForTestClasses将忽略名为'exclude'的几个级别的目录,默认情况下为true。不知道这里有什么?

我在Gradle测试报告中注意到的另一件事是,报告中列出的软件包名称为default-package,用于排除的测试不是“排除”,而其他要测试的测试是列出正确的包名称。 Java文件中的包名称正确匹配其文件夹结构,因此我不确定为什么会以这种方式报告。我已经检查了重复,拼写错误等,但我没有到达任何地方。

如果有人能够对此有所了解,那将会很好,因为运行这些不完整/已损坏的测试类会导致在更新这些测试之前应该忽略的失败。

在Linux上运行的测试CI(Jenkins)框中使用Gradle包装器生成的bash脚本运行这些测试。

1 个答案:

答案 0 :(得分:1)

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

使用excludeSpec(请参阅Gradle Test task DSL)应该可以正常工作:

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

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