Run a specific TestNG XML test suite with Gradle from command line?

∥☆過路亽.° 提交于 2020-01-31 19:17:33

问题


I am using Gradle with TestNG. I have this build.gradle:

useTestNG() {
        useDefaultListeners = true
        suites "src/test/resources/tests1.xml"
        suites "src/test/resources/tests2.xml"
   }
}

How can I run for example only tests1.xml from command line?


回答1:


you can use project properties to add/change/... different suites. In you example you are probably running

gradlew test

which run both suites. If you modify test task in your build.gradle

def suite1 = project.hasProperty("suite1")
def suite2 = project.hasProperty("suite2")

test {
    useTestNG() {
        dependsOn cleanTest
        useDefaultListeners = true

        if(suite1) {
            suites "src/test/resources/simpleSuite.xml"
        }

        if(suite2) {
            suites "src/test/resources/advancedSuite.xml"
        }
    }
}

you can choose suite in this way

gradlew test -Psuite1
gradlew test -Psuite2
gradlew test -Psuite1 -Psuite2



回答2:


you can specify variable let say suiteFile with default value and use it in testNG section. For example:

ext{

    set suiteFile, default is 'testrun_config.xml'
    if (!project.hasProperty('suiteFile')) {
        suiteFile = 'testrun_config.xml'
    }
}

test {
    useTestNG() {
        dependsOn cleanTest
        useDefaultListeners = true
        suites "src/test/resources/"+suiteFile

    }
} 

Refer qaf gradle build file

If you want to pass through command line

gradlew test -PsuiteFile=test.xml



回答3:


In build.gradle update:

test {
    useTestNG() {
        if (project.hasProperty('suite1')) { suites './src/test/suite1.xml' }
        if (project.hasProperty('suite2')) { suites './src/test/suite2.xml' }
    }
}

Use command gradlew test -Psuite1 to run suite1 and similarly update for suite2 as well.



来源:https://stackoverflow.com/questions/43606992/run-a-specific-testng-xml-test-suite-with-gradle-from-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!