Xray-App / xray-junit-extensions

Enhanced JUnit 5 integration with Xray Test Management for Jira
Eclipse Public License 2.0
16 stars 8 forks source link

xrayReporter.addComment() not adding a comment on Jira Cloud #35

Closed DominikTobaben1061 closed 1 year ago

DominikTobaben1061 commented 1 year ago

Hi, I have a question if you could help me adding Comments to XRay TestExecutions.

Because I can create and map tests and report the FAIL/PASS Status, but it does not add the Comments.

The important build.gradle parts:

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'app.getxray:xray-maven-plugin:0.5.0'
    testImplementation 'app.getxray:xray-junit-extensions:0.6.2'
}

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

task importJunitResultsToXrayCloud() {
    description 'Imports Junit test results to Xray Test Management for Jira Cloud.'
    dependsOn 'test'

    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 = "build/test-results/xray-tests/custom-report-junit.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}"
                        }
                        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()
                    }
                }
            }
        }

    }
}

My gradle.properties

# Jira Cloud specifics
clientId=<working clientId>
clientSecret=<working secret>

reportFormat=junit
projectKey=ABC
version=v1.0
testPlanKey=
testExecKey=
testEnvironment=

My resources/xray-junit-extensions.properties

report_filename=custom-report-junit
report_directory=build/test-results/xray-tests
add_timestamp_to_report_filename=false

My META-INF/services/org.junit.platform.launcher.TestExecutionListener

app.getxray.xray.junit.customjunitxml.EnhancedLegacyXmlReportGeneratingListener

My Test XRaySecondIntegrationTest

@ExtendWith(XrayTestReporterParameterResolver.class)
public class XRaySecondIntegrationTest{

    @Test
    @Requirement("ABC-944")
    @XrayTest(key = "ABC-1159")
    void testXRayAutomaticStoryMapping(XrayTestReporter xrayReporter){
        xrayReporter.addComment("Test Comment");
        Assertions.assertTrue(true);
    }
}

My gradle Call:

./gradlew test --tests "XRay*" importJunitResultsToXrayCloud

The JUnit Report which does show the comment (I removed the porperties by hand):

<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="JUnit Jupiter" tests="1" skipped="0" failures="0" errors="0" time="0" hostname="DESKTOP" timestamp="2023-06-09T10:32:34">
<properties>
<property name="file.encoding" value="UTF-8"/>
<property name="file.separator" value="/"/>
</properties>
<testcase name="testXRayAutomaticStoryMapping" classname="de.xray.XRaySecondIntegrationTest" time="0" started-at="2023-06-09T08:32:34.449810131" finished-at="2023-06-09T08:32:34.470056349">
<system-out><![CDATA[
unique-id: [engine:junit-jupiter]/[class:de.xray.XRaySecondIntegrationTest]/[method:testXRayAutomaticStoryMapping(app.getxray.xray.junit.customjunitxml.XrayTestReporter)]
display-name: testXRayAutomaticStoryMapping(XrayTestReporter)
]]></system-out>
<system-out><![CDATA[

]]></system-out>
<properties>
<property name="testrun_comment"><![CDATA[Test Comment]]></property>

<property name="requirements" value="ABC-944"/>
<property name="test_key" value="ABC-1159"/>
<property name="testrun_evidence">
</property>
<property name="_dummy_" value=""/>
</properties>
</testcase>
<system-out><![CDATA[
unique-id: [engine:junit-jupiter]
display-name: JUnit Jupiter
]]></system-out>
</testsuite>

The Task output:

> Task :importJunitResultsToXrayCloud
Authenticated with Xray!
Resuls imported to Xray!
{"id":"123456","key":"ABC-1167","self":"https://xxx.atlassian.net/rest/api/2/issue/123456"}

Tests get created automatically when I use only Requirement Anntotation Execution Results are correctly shown with status of the JUNIT Test (Fail and Passed works)

But comments are not shown and https://xxx.atlassian.net/rest/api/2/issue/123456 shows an empty comments block.

bitcoder commented 1 year ago

The comment will not go into the Test Execution issue; it will go into the Test Run, i.e., the result of a Test in the context of the Test Execution. To see it, you have to see the execution details of the Test whenever you open the Test Execution issue. To obtain it by API, you need to use the GraphQL API from Xray; the API you mentioned is the REST API of Jira Cloud, which doesn't have access to Xray internal data, such as the comments and the reported test run results.

DominikTobaben1061 commented 1 year ago

Found it, thanks!

This maybe help other beginners: Within Test Issue > Test Details Section > Test Runs There is a "Burger Menu with a Box" Button (like this =[ ] )between "Status" and "Actions" for the Test Execution. Within the Findings Section you find the Comment.

Thanks a lot for the quick reply!