Skip to content

Commit d6e7616

Browse files
authored
Merge pull request #1069 from nunit/testcaseupdates
Updated testcase documentation
2 parents 02df423 + 5c72e59 commit d6e7616

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

docs/articles/nunit/writing-tests/attributes/testcase.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ TestCaseAttribute supports a number of additional named parameters:
4242
but may serve a purpose for the test author)
4343
* **TypeArgs** specifies the `Type`s to be used when targeting a generic test method. (_NUnit 4.1+_)
4444

45+
## Be aware of mixing the syntax for named parameters and attributes with the same name
46+
47+
### Correct `Ignore` Attribute Usage, by Example
48+
49+
> [!WARNING]
50+
> When using the `Ignore` parameter (and others, see below), note that this has to be a named parameter. It is easy to accidentally add another `Ignore` attribute after the `TestCase` attribute. That will be the same as adding it separately, and it will apply to the complete fixture. This may apply to other named parameters, with names equal to other attributes, like the `Explicit` and `Category` parameters.
51+
52+
Correct example usage:
53+
54+
[!code-csharp[TestCaseWithIgnore](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithIgnore)]
55+
56+
![TestCaseIgnoreDoneCorrect](../../../../images/TestCaseIgnoreDoneCorrect.png)
57+
58+
> [!WARNING]
59+
> **Wrong way!** Below, we demonstrate an incorrect approach.
60+
>
61+
> (1) Adding it on the same line is the same as adding it on a separate line (3), both results in the fixture being ignored (2).
62+
63+
![TestCaseIgnoreGoneWrong](../../../../images/TestCaseIgnoreGoneWrong.png)
64+
65+
<!-- cspell:disable-next-line -->
66+
_Thanks to [Geir Marius Gjul](https://github.com/GeirMG) for raising this question again._
67+
68+
### Correct `Explicit` Attribute Usage, by Example
69+
70+
`Explicit`, used correctly, looks like the following:
71+
72+
[!code-csharp[TestCaseWithExplicit](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithExplicit)]
73+
74+
Note that adding the `Reason` is optional, and Visual Studio TestExplorer will not even show it.
75+
76+
### Correct `Category` Attribute Usage, by Example
77+
78+
Categories can be applied to a single `TestCase` the same way, as a named parameter. Otherwise, it will apply to the whole fixture. Be sure what you're asking for!
79+
80+
[!code-csharp[TestCaseWithCategory](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithCategory)]
81+
4582
## Order of Execution
4683

4784
Individual test cases are executed in the order in which NUnit discovers them. This order does **not** necessarily
15.2 KB
Loading
128 KB
Loading

docs/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
using NUnit.Framework;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
72

83
namespace Snippets.NUnit.Attributes
94
{
@@ -28,5 +23,38 @@ public int DivideTest(int n, int d)
2823
return n / d;
2924
}
3025
#endregion
26+
27+
#region TestCaseWithIgnore
28+
[TestCase(1, 1)]
29+
[TestCase(0, 0, Ignore = "Only ignore this")]
30+
[TestCase(1, 3)]
31+
public void AddTestWithIgnore(int a, int b)
32+
{
33+
var result = a + b;
34+
Assert.That(result, Is.GreaterThan(1));
35+
}
36+
#endregion
37+
38+
#region TestCaseWithExplicit
39+
[TestCase(1, 1)]
40+
[TestCase(0, 0, Explicit = true, Reason = "This will fail so only run explicitly")]
41+
[TestCase(1, 3)]
42+
public void AddTestWithExplicit(int a, int b)
43+
{
44+
var result = a + b;
45+
Assert.That(result, Is.GreaterThan(1));
46+
}
47+
#endregion
48+
49+
#region TestCaseWithCategory
50+
[TestCase(1, 1)]
51+
[TestCase(2, 0, Category = "Crazy")]
52+
[TestCase(1, 3)]
53+
public void AddTestWithCategory(int a, int b)
54+
{
55+
var result = a + b;
56+
Assert.That(result, Is.GreaterThan(1));
57+
}
58+
#endregion
3159
}
3260
}

0 commit comments

Comments
 (0)