About Nunit
Nunit is a testing framework for C# (Csharp), mostly focused on unit testing.
In fact, similarly 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, either if they’re 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, among other things, you have Tests, Parameterized Tests, (Test) Suites and TestFixtures.
- 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 together for running
- A TestFixture corresponds to a "class" containing multiple Tests
Nunit makes use of "attributes" in order to attribute behavior/characteristics to certain parts of your automated test code.
Learn in practice
Please give a look at the basic C# example: Testing using Nunit in C#.
Importing Nunit XML reports
Bellow is a simplified example of a Nunit 3.0 XML report, containing a Test Suite with some Test Cases.
The simplified tags hierarchy of these reports can be represented in the following diagram:
A Nunit 2.6 XML report can be seen bellow, containing a Test Suite with some Test Cases.
The simplified tags hierarchy of these reports can be represented in the following diagram:
Entities
Test Cases are imported to Xray’s Generic Test issues, and, 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 with 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 matching 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 is considered;
- if the "Test" property used explicitly, then the Test must exist or else it will not be imported.
- Nunit's categories reference requirements or Test issue keys, then the issue key should be represented with underscore instead of hyphen, e.g. "CALC_1" (due to the characters limitation of 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)); } ... } }
Concerning 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 neither to a requirement nor to a Test, it would be added to the Test as a label.
Concerning the second Test, a Generic Test with the summary "CanSubtract" would be created.
In fact, 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 neither to a requirement nor to 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 (see next table for the mappings).
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 which can be seen in the Test Run screen, under the Results section.
If the same Test Case has been executed 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 the 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 |