Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/articles/nunit/writing-tests/attributes/testcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ TestCaseAttribute supports a number of additional named parameters:
but may serve a purpose for the test author)
* **TypeArgs** specifies the `Type`s to be used when targeting a generic test method. (_NUnit 4.1+_)

## Be aware of mixing the syntax for named paramaters and attributes with the same name

### Ignore done right and wrong

[!WARNING]
When using the **Ignore** parameter (and others, see below), note that this has to be a named parameter. It is easy to 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. Note, that this might apply to more of these named parameters, with names equal to other attributes, like the Explicit and Category parameters.

[!TIP]
Doing it right!

[!code-csharp[TestCaseWithIgnore](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithIgnore)]

![TestCaseIgnoreDoneCorrect](../../../../images/TestCaseIgnoreDoneCorrect.png)

[!WARNING]
Wrong way! (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).

![TestCaseIgnoreGoneWrong](../../../../images/TestCaseIgnoreGoneWrong.png)

_Thanks to [Geir Marius Gjul](https://github.com/GeirMG) for raising this question again._

### Explicit done correct

Explicit is another one, and done correctly as:

[!code-csharp[TestCaseWithExplicit](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithExplicit)]

Note that adding the Reason is optional, and Visual Studio TestExplorer will not even show it.

### Category done correct

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!

[!code-csharp[TestCaseWithCategory](~/snippets/Snippets.NUnit/Attributes/TestCaseAttributeExamples.cs#TestCaseWithCategory)]

## Order of Execution

Individual test cases are executed in the order in which NUnit discovers them. This order does **not** necessarily
Expand Down
Binary file added docs/images/TestCaseIgnoreDoneCorrect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/TestCaseIgnoreGoneWrong.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snippets.NUnit.Attributes
{
Expand All @@ -28,5 +23,38 @@ public int DivideTest(int n, int d)
return n / d;
}
#endregion

#region TestCaseWithIgnore
[TestCase(1, 1)]
[TestCase(0, 0, Ignore = "Only ignore this")]
[TestCase(1, 3)]
public void AddTestWithIgnore(int a, int b)
{
var result = a + b;
Assert.That(result, Is.GreaterThan(1));
}
#endregion

#region TestCaseWithExplicit
[TestCase(1, 1)]
[TestCase(0, 0, Explicit = true, Reason = "This will fail so only run explicitly")]
[TestCase(1, 3)]
public void AddTestWithExplicit(int a, int b)
{
var result = a + b;
Assert.That(result, Is.GreaterThan(1));
}
#endregion

#region TestCaseWithCategory
[TestCase(1, 1)]
[TestCase(2, 0, Category = "Crazy")]
[TestCase(1, 3)]
public void AddTestWithCategory(int a, int b)
{
var result = a + b;
Assert.That(result, Is.GreaterThan(1));
}
#endregion
}
}
Loading