TestNG is a testing framework for Java, mostly focused on unit testing but not only.
Similar to JUnit, TestNG is also used for writing integration and acceptance tests, making use of other libraries such as Selenium.
TestNG XML reports are normally creating in the context of Java applications; however, so other languages and test automation frameworks may also be able to generate this kind of reports.
TestNG is quite feature-complete as may be seen in its online documentation.
Xray supports TestNG 6.14 XML reports.
TestNG Basic Concepts
In TestNG, you have Test methods, Parameterized Test methods, Test classes, Test groups, suites.
- A Test method is a test case;
- Parameterized Tests are a way of specifying input values for a given Test (similar to an example in a Cucumber Scenario Outline). Parameters may come from the XML configuration file used by TestNG or from a data provider (i.e. a method that generates a set of values);
- Tests code is implemented within Test classes;
- Test groups are somehow similar to using Jira labels in Jira issues, in the way that they're used to mark the Test methods as belonging to some "category"; these groups can be used afterwards to more easily select the Test to be run;
- A suite is a configuration file (i.e. a XML file) that is used to enumerate all the Tests to be run, based on the Test classes, Test groups, packages, Test methods.
TestNG uses "attributes" in order to ascribe behavior/characteristics to certain parts of your automated test code; these attributes can be set in the Test method's code.
Learn in practice
Please look at the basic Java example: Testing using TestNG in Java.
Importing TestNG XML reports
Below is a simplified example of a TestNG XML report containing a Test Suite with some Test Cases.
Test Cases are imported to Xray’s Generic Test issues. The “classname” and “methodname” attributes are concatenated and mapped to the Generic Test Definition field of the Generic Test.
If a Test already exists with the same Generic Test Definition, then it is not created again.
Test Cases are imported to a new (or a user-specified) Test Execution in the context of some project, along with their respective execution results.
Parameterized Tests are imported to the same Test Run, where each set of parameters is identified by a different context.
The context (shown on the left side of the execution screen details) is with the name of the "suite" plus the name of the "test" as defined in the XML configuration file used by TestNG. If the Test method corresponds to a parameterized test, then the values used for that specific run are appended to the context.
Mapping of fields from the report to the Test issue
|TestNG XML report||Test in Jira|
"name" attribute of <test-method> element
"summary" attribute under the <attributes> tag, within some <test-method>
This overrides the "name" attribute of <test-method> element as the summary.
|"description" attribute under the <attributes> tag||Description field|
"name" attribute of <class> element + "." + "name" attribute of <test-method> element
|Generic Test Definition custom field|
|"labels" attribute under the <attributes> tag, within some <test-method>, containing one or more labels delimited by space||labels|
|"test" attribute under the <attributes> tag, within some <test-method>, containing a Jira key of the Test issue||identification of Test issue key in Jira|
|"requirement" attribute under the <attributes> tag, within some <test-method>, containing a Jira key of requirement(s)||link to requirement(s)|
- linked requirements may be identified by setting an attribute named "requirement" with the respective issue key at the Test method level, using the ITestResult object"; multiple requirements may be specified by using the space delimiter
- the Test to report results against to may be identified by using and attribute named "test";
- if the "Test" attribute is used explicitly, then the Test must exist or else it will not be imported.
Consider running some Tests implemented in the following Java class.
In the first Test method, we can see an example of a parameterized test using a data provider. Xray would try to find a Test with the same Generic Test Definition, if it does not find one then it will create a Test issue.
In the second Test method, Xray will try to find an existing Test having the issue key CALC-2. If it does not find one and no Test with same Generic Test Definition already exists then a Generic Test with the summary "CanAddNumbers" would be created.
If there is a requirement with the key "CALC-1234", it would create the link to that requirement.
Additionaly, "core" and "addition" would be added to the Test as labels.
The status of the Test Run will be set based on the Test case result:
|value of "status" attribute of <test-method> in XML report|
Test status in Xray
(*) should not appear though
Note: Test cases with the status FAIL may have a failure message displayed in the Test Run screen, under the Results section.
If the same Test case has been executed multiple times, then the result for each context will be shown. A parameterized Test will also appear with its own specific contexts.
Whenever a Test Case is executed in multiple contexts, the overall status of the Test Run will be calculated as a joint value.
Overall status of the Test Run
If all the mapped results of the Test Case was PASS
If any of the mapped results of the Test Case was FAIL
Notes and Limitations
- attachments (e.g. screenshots and other files) are not supported/imported as they are not embedded in the XML report; there is no official way for referring or embedding attachments (request on GitHub)