Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
titlebuild.gradle
collapsetrue
apply plugin: 'java'

repositories {
    mavenCentral()
}

jar {
    archiveBaseName = 'tutorial-gradle-testng-basic'
    archiveVersion =  '0.1.0'
}

sourceCompatibility = 11
targetCompatibility = 11

test {
    useTestNG()
    reports {
	    // destination = file('build/test-results/folder')
        junitXml.required = false
        html.required = false
    }
    ignoreFailures = true
}

repositories {
    mavenLocal()
    maven {
        url = uri('https://maven.xpand-it.com/artifactory/releases')
    }

    maven {
        url = uri('https://maven.pkg.github.com/bitcoder/*')
    }

    maven {
        url = uri('https://repo.maven.apache.org/maven2/')
    }
}

dependencies {
    testImplementation 'org.testng:testng:7.6.1'
    testImplementation 'com.xpandit.xray:xray-testng-extensions:1.1.0'
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
}

task testngTest(type: JavaExec, dependsOn: [classes]) {
    group 'Verification'
    description 'Run TestNG tests'
    ignoreExitValue = true // to not throw exception if test fails
    mainClass = 'org.testng.TestNG'
    args('testng.xml', '-reporter',
            'org.testng.reporters.XMLReporter:generateTestResultAttributes=true,generateGroupsAttribute=true'
    )
    classpath = sourceSets.test.runtimeClasspath
}

task importTestNGResultsToXrayCloud() {
	description 'Imports TestNG test results to Xray Test Management for Jira Cloud.'
	dependsOn 'testngTest' 

    def xrayApiBaseUrl = 'https://xray.cloud.getxray.app/api/v2'
    // the following variables can be defined locally or in gradle.properties
    //  - clientId, clientSecret
    //  - reportFormat, projectKey version, revision, testPlanKey, testExecKey, testEnvironment

	def reportFile = "test-output/testng-results.xml"

    doLast {
        new ByteArrayOutputStream().withStream { os ->
            def result = exec {
                ignoreExitValue = true
                commandLine 'curl', '--silent', '--fail-with-body', '-H','Content-Type: application/json', '-X','POST', '--data', "{ \"client_id\": \"${clientId}\",\"client_secret\": \"${clientSecret}\" }", "${xrayApiBaseUrl}/authenticate"
                standardOutput = os
                }
                if (result.getExitValue() != 0) {
                        println "ERROR: problem authenticating"
                } else {
                        def token = os.toString().replaceAll("\"","")
                        println "Authenticated with Xray!"

                            new ByteArrayOutputStream().withStream { os2 ->
                                def result2 = exec {
                                ignoreExitValue = true

                                def url = "${xrayApiBaseUrl}/import/execution/${reportFormat}?"
                                if (projectKey?.trim()) {
                                    url += "&projectKey=${projectKey}"
                                }
                                if (version?.trim()) {
                                    url += "&fixVersion=${version}"
                                }
                                if (revision?.trim()) {
                                    url += "&revision=${revision}"
                                }
                                if (testPlanKey?.trim()) {
                                    url += "&testPlanKey=${testPlanKey}"
                                }
                                if (testExecKey?.trim()) {
                                    url += "&testExecKey=${testExecKey}"
                                }
                                if (testEnvironment?.trim()) {
                                    url += "&testEnvironments=${testEnvironment}"
                                }

                                commandLine 'curl', '--silent', '--fail-with-body', '-H','Content-Type: application/xml', '-X','POST', '-H', "Authorization: Bearer ${token}", '--data', "@${reportFile}", url
                                standardOutput = os2
                                }
                                if (result2.getExitValue() != 0) {
                                        println "ERROR: problem importing results to Xray"
                                        println os2.toString()
                                } else {
                                        println "Resuls imported to Xray!"
                                        println os2.toString()
                                }
                        }
                }
        }

    }
}

...