About NUnit
NUnit is a testing framework for C# (C sharp), mostly focused on unit testing.
Similar to JUnit, NUnit is also used for writing integration and acceptance tests, making use of other libraries such as Selenium.
NUnit XML reports may be created by many different testing frameworks for C#, Javascript or any other language.
NUnit is quite feature-complete as may be seen in its online documentation.
Supported versions
Xray supports NUnit v2.6 and v3.0 XML reports.
Nunit Basic Concepts
In NUnit, you have Tests, Parameterized Tests, (Test) Suites and TestFixtures, among other things.
- A Test 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).
- A Suite is a way of aggregating a group of tests for running.
- A TestFixture corresponds to a "class" containing multiple Tests.
NUnit uses "attributes" in order to ascribe behavior/characteristics to certain parts of your automated test code.
Learn in practice
Please look at the basic C# example: Testing using NUnit in C#.
Importing NUnit XML reports
Below is a simplified example of an NUnit 3.0 XML report containing a Test Suite with some Test Cases.
The simplified tags hierarchy of these reports are shown in the following diagram:
Below is an NUnit 2.6 XML report containing a Test Suite with some Test Cases.
The simplified tags hierarchy of these reports are shown in the following diagram:
Entities
Test Cases are imported to Xray’s Generic Test issues. For NUnit 3.0, 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.
NUnit’s Test Suites are not mapped to any special entity. However, the execution details screen will show the Test Suite related to a specific test result.
Mapping of fields from the report to the Test issue
NUnit XML report | Test in Jira |
---|---|
| Summary field |
| Generic Test Definition custom field |
categories | labels (except for the ones that match with Tests or requirements) |
"Test" property or category containing a Jira key of a Test | identification of Test issue key in Jira |
"Requirement" property or category containing a Jira key of a requirement | link to requirement |
Notes:
- requirements may be identified by using either NUnit's categories or a property named "Requirement";
- Tests may be identified by using either Nunit's categories or a property named "Test";
- if categories contain multiple references to Test entities, only the first instance is considered;
- if the "Test" property is used explicitly, then the Test must exist or else it will not be imported.
- If the category references a requirement or a Test issue key, then the issue key should be represented with underscore instead of hyphen, e.g., "CALC_1" (due to the character limitation in NUnit).
Examples
Consider running some Tests implemented in the following C# class.
namespace x { [TestFixture] public class CalculatorTests { [TestCase(1, 1, 2)] [TestCase(-1, -1, -2)] [TestCase(100, 5, 105)] [Category("CALC_1")] [Category("fast")] [Property("Requirement", "CALC-69")] [Property("Test", "CALC-10")] public void CanAddNumbers(int a, int b, int expected) { Assert.That(Calculator.Add(a, b), Is.EqualTo(expected)); } [TestCase(1, 1, 0)] [TestCase(-1, -1, 0)] [TestCase(100, 5, 95)] [Category("CALC_2")] public void CanSubtract(int x, int y, int expected) { Assert.That(Calculator.Subtract(x, y), Is.EqualTo(expected)); } ... } }
In the first Test, Xray would try to find a Test with the key "CALC-10". If it does not exist, the Test won't be created.
The test would be linked to the Requirement with key "CALC-69", using the "tests" association, if the requirement exists.
If there is a requirement with the key "CALC-1", it would create the link to that requirement. If "CALC-1" corresponds to a Test issue, then "CALC_1" is ignored.
If the Category "CALC_1" does not map to either a requirement or a Test, it would be added to the Test as a label.
In the second Test, a Generic Test with the summary "CanSubtract" would be created.
The Test is only created if no Test with same Generic Test Definition already exists or if "CALC-2" does not correspond to a Test issue.
If there is a requirement with the key "CALC-2", it would create the link to that requirement.
If the Category "CALC_2" does not map to either a requirement or a Test, it would be added to the Test as a label.
Status
The status of the Test Run will be set based on the Test case result:
Test Cases (NUnit3.0) | Test Cases (NUnit2.6) | Test status |
---|---|---|
Failed | Error | FAIL |
Passed | Success | PASS |
Other Cases | Other Cases | TODO |
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 in multiple Test Suites, then the result for each Test Suite will be shown. A parameterized Test will also appear with its own specific context.
Whenever a Test Case is executed in multiple Test Suites, the overall status of the Test Run will be calculated as a joint value.
Condition | Overall status of the Test Run |
---|---|
If all the mapped results of the Test Case was PASS | PASS |
If any of the mapped results of the Test Case was FAIL | FAIL |
Other cases | TODO |
Notes and Limitations
- attachments (e.g. screenshots and other files) are not supported/imported as they are not embedded in the XML report; the XML report only contains references to their local paths