Skip to content

Commit b1c2ad5

Browse files
committed
Merge pull request #1069 from nunit/testcaseupdates
Updated testcase documentation d6e7616
1 parent 31ac859 commit b1c2ad5

File tree

6 files changed

+791
-724
lines changed

6 files changed

+791
-724
lines changed

articles/nunit/writing-tests/attributes/testcase.html

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,54 @@ <h5>Note</h5>
133133
but may serve a purpose for the test author)</li>
134134
<li><strong>TypeArgs</strong> specifies the <code>Type</code>s to be used when targeting a generic test method. (<em>NUnit 4.1+</em>)</li>
135135
</ul>
136-
<h2 id="order-of-execution">Order of Execution</h2>
136+
<h2 id="be-aware-of-mixing-the-syntax-for-named-parameters-and-attributes-with-the-same-name">Be aware of mixing the syntax for named parameters and attributes with the same name</h2>
137+
<h3 id="correct-ignore-attribute-usage-by-example">Correct <code>Ignore</code> Attribute Usage, by Example</h3>
138+
<div class="WARNING">
139+
<h5>Warning</h5>
140+
<p>When using the <code>Ignore</code> parameter (and others, see below), note that this has to be a named parameter. It is easy to accidentally add another <code>Ignore</code> attribute after the <code>TestCase</code> 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 <code>Explicit</code> and <code>Category</code> parameters.</p>
141+
</div>
142+
<p>Correct example usage:</p>
143+
<pre><code class="lang-csharp" name="TestCaseWithIgnore">[TestCase(1, 1)]
144+
[TestCase(0, 0, Ignore = &quot;Only ignore this&quot;)]
145+
[TestCase(1, 3)]
146+
public void AddTestWithIgnore(int a, int b)
147+
{
148+
var result = a + b;
149+
Assert.That(result, Is.GreaterThan(1));
150+
}
151+
</code></pre>
152+
<p><img src="../../../../images/TestCaseIgnoreDoneCorrect.png" alt="TestCaseIgnoreDoneCorrect"></p>
153+
<div class="WARNING">
154+
<h5>Warning</h5>
155+
<p><strong>Wrong way!</strong> Below, we demonstrate an incorrect approach.</p>
156+
<p>(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).</p>
157+
</div>
158+
<p><img src="../../../../images/TestCaseIgnoreGoneWrong.png" alt="TestCaseIgnoreGoneWrong"></p>
159+
<!-- cspell:disable-next-line -->
160+
<p><em>Thanks to <a href="https://github.com/GeirMG">Geir Marius Gjul</a> for raising this question again.</em></p>
161+
<h3 id="correct-explicit-attribute-usage-by-example">Correct <code>Explicit</code> Attribute Usage, by Example</h3>
162+
<p><code>Explicit</code>, used correctly, looks like the following:</p>
163+
<pre><code class="lang-csharp" name="TestCaseWithExplicit">[TestCase(1, 1)]
164+
[TestCase(0, 0, Explicit = true, Reason = &quot;This will fail so only run explicitly&quot;)]
165+
[TestCase(1, 3)]
166+
public void AddTestWithExplicit(int a, int b)
167+
{
168+
var result = a + b;
169+
Assert.That(result, Is.GreaterThan(1));
170+
}
171+
</code></pre>
172+
<p>Note that adding the <code>Reason</code> is optional, and Visual Studio TestExplorer will not even show it.</p>
173+
<h3 id="correct-category-attribute-usage-by-example">Correct <code>Category</code> Attribute Usage, by Example</h3>
174+
<p>Categories can be applied to a single <code>TestCase</code> the same way, as a named parameter. Otherwise, it will apply to the whole fixture. Be sure what you're asking for!</p>
175+
<pre><code class="lang-csharp" name="TestCaseWithCategory">[TestCase(1, 1)]
176+
[TestCase(2, 0, Category = &quot;Crazy&quot;)]
177+
[TestCase(1, 3)]
178+
public void AddTestWithCategory(int a, int b)
179+
{
180+
var result = a + b;
181+
Assert.That(result, Is.GreaterThan(1));
182+
}
183+
</code></pre><h2 id="order-of-execution">Order of Execution</h2>
137184
<p>Individual test cases are executed in the order in which NUnit discovers them. This order does <strong>not</strong> necessarily
138185
follow the lexical order of the attributes and will often vary between different compilers or different versions of the
139186
CLR.</p>

images/TestCaseIgnoreDoneCorrect.png

15.2 KB
Loading

images/TestCaseIgnoreGoneWrong.png

128 KB
Loading

index.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3167,7 +3167,7 @@
31673167
"articles/nunit/writing-tests/attributes/testcase.html": {
31683168
"href": "articles/nunit/writing-tests/attributes/testcase.html",
31693169
"title": "TestCase | NUnit Docs",
3170-
"summary": "TestCase TestCaseAttribute serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method. Here is an example of a test being run three times, with three different sets of data: [TestCase(12, 3, 4)] [TestCase(12, 2, 6)] [TestCase(12, 4, 3)] public void DivideTest(int n, int d, int q) { Assert.That(n / d, Is.EqualTo(q)); } Note Because arguments to .NET attributes are limited in terms of the Types that may be used, NUnit will make some attempt to convert the supplied values using Convert.ChangeType() before supplying it to the test. TestCaseAttribute may appear one or more times on a test method, which may also carry other attributes providing test data. The method may optionally be marked with the Test Attribute as well. By using the named parameter ExpectedResult this test set may be simplified further: [TestCase(12, 3, ExpectedResult = 4)] [TestCase(12, 2, ExpectedResult = 6)] [TestCase(12, 4, ExpectedResult = 3)] public int DivideTest(int n, int d) { return n / d; } In the above example, NUnit checks that the return value of the method is equal to the expected result provided on the attribute. TestCaseAttribute supports a number of additional named parameters: Author sets the author of the test. Category provides a comma-delimited list of categories for this test. Description sets the description property of the test. ExcludePlatform specifies a comma-delimited list of platforms on which the test should not run. ExpectedResult sets the expected result to be returned from the method, which must have a compatible return type. Explicit is set to true in order to make the individual test case Explicit. Use Reason to explain why. Ignore causes the test case to be ignored and specifies the reason. IgnoreReason causes this test case to be ignored and specifies the reason. IncludePlatform specifies a comma-delimited list of platforms on which the test should run. Reason specifies the reason for not running this test case. Use in conjunction with Explicit. TestName provides a name for the test. If not specified, a name is generated based on the method name and the arguments provided. See Template Based Test Naming. TestOf specifies the Type that this test is testing (this is not used within NUnit during test execution, but may serve a purpose for the test author) TypeArgs specifies the Types to be used when targeting a generic test method. (NUnit 4.1+) Order of Execution Individual test cases are executed in the order in which NUnit discovers them. This order does not necessarily follow the lexical order of the attributes and will often vary between different compilers or different versions of the CLR. As a result, when TestCaseAttribute appears multiple times on a method or when other data-providing attributes are used in combination with TestCaseAttribute, the order of the test cases is undefined."
3170+
"summary": "TestCase TestCaseAttribute serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method. Here is an example of a test being run three times, with three different sets of data: [TestCase(12, 3, 4)] [TestCase(12, 2, 6)] [TestCase(12, 4, 3)] public void DivideTest(int n, int d, int q) { Assert.That(n / d, Is.EqualTo(q)); } Note Because arguments to .NET attributes are limited in terms of the Types that may be used, NUnit will make some attempt to convert the supplied values using Convert.ChangeType() before supplying it to the test. TestCaseAttribute may appear one or more times on a test method, which may also carry other attributes providing test data. The method may optionally be marked with the Test Attribute as well. By using the named parameter ExpectedResult this test set may be simplified further: [TestCase(12, 3, ExpectedResult = 4)] [TestCase(12, 2, ExpectedResult = 6)] [TestCase(12, 4, ExpectedResult = 3)] public int DivideTest(int n, int d) { return n / d; } In the above example, NUnit checks that the return value of the method is equal to the expected result provided on the attribute. TestCaseAttribute supports a number of additional named parameters: Author sets the author of the test. Category provides a comma-delimited list of categories for this test. Description sets the description property of the test. ExcludePlatform specifies a comma-delimited list of platforms on which the test should not run. ExpectedResult sets the expected result to be returned from the method, which must have a compatible return type. Explicit is set to true in order to make the individual test case Explicit. Use Reason to explain why. Ignore causes the test case to be ignored and specifies the reason. IgnoreReason causes this test case to be ignored and specifies the reason. IncludePlatform specifies a comma-delimited list of platforms on which the test should run. Reason specifies the reason for not running this test case. Use in conjunction with Explicit. TestName provides a name for the test. If not specified, a name is generated based on the method name and the arguments provided. See Template Based Test Naming. TestOf specifies the Type that this test is testing (this is not used within NUnit during test execution, but may serve a purpose for the test author) TypeArgs specifies the Types to be used when targeting a generic test method. (NUnit 4.1+) Be aware of mixing the syntax for named parameters and attributes with the same name Correct Ignore Attribute Usage, by Example Warning 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. Correct example usage: [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)); } Warning Wrong way! Below, we demonstrate an incorrect approach. (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). Thanks to Geir Marius Gjul for raising this question again. Correct Explicit Attribute Usage, by Example Explicit, used correctly, looks like the following: [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)); } Note that adding the Reason is optional, and Visual Studio TestExplorer will not even show it. Correct Category Attribute Usage, by Example 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! [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)); } Order of Execution Individual test cases are executed in the order in which NUnit discovers them. This order does not necessarily follow the lexical order of the attributes and will often vary between different compilers or different versions of the CLR. As a result, when TestCaseAttribute appears multiple times on a method or when other data-providing attributes are used in combination with TestCaseAttribute, the order of the test cases is undefined."
31713171
},
31723172
"articles/nunit/writing-tests/attributes/testcasesource.html": {
31733173
"href": "articles/nunit/writing-tests/attributes/testcasesource.html",

manifest.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9202,6 +9202,26 @@
92029202
},
92039203
"version": ""
92049204
},
9205+
{
9206+
"type": "Resource",
9207+
"source_relative_path": "images/TestCaseIgnoreDoneCorrect.png",
9208+
"output": {
9209+
"resource": {
9210+
"relative_path": "images/TestCaseIgnoreDoneCorrect.png"
9211+
}
9212+
},
9213+
"version": ""
9214+
},
9215+
{
9216+
"type": "Resource",
9217+
"source_relative_path": "images/TestCaseIgnoreGoneWrong.png",
9218+
"output": {
9219+
"resource": {
9220+
"relative_path": "images/TestCaseIgnoreGoneWrong.png"
9221+
}
9222+
},
9223+
"version": ""
9224+
},
92059225
{
92069226
"type": "Resource",
92079227
"source_relative_path": "images/TraceDebug1.png",

0 commit comments

Comments
 (0)