Skip to content

Commit 3704af7

Browse files
committed
Convert some docs to snippets (#1065)
* Initial claude commit * formatting fixes * Allow claude to run build and test * Generate snippets from docs * allow build of docfx * Allow some more commands * Refer to the snippets * Add snippet to docs * fix sample name * use constraint example snippet * Use constraint example * fix constraint snippet * use snippet * Update docs/snippets/Snippets.NUnit/ClassicAssertExamples.cs * R# cleanup * more R# cleanup * More R# cleanup * More R# cleanup * Use SetUpTearDownExample instead * Remove redundant snippet ecd21e7
1 parent 0c17e4f commit 3704af7

28 files changed

+1084
-947
lines changed

articles/nunit/writing-tests/assertions/classic-assertions/Assert.AreEqual.html

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,48 @@ <h1 id="classicassertareequal">ClassicAssert.AreEqual</h1>
9898
<h2 id="comparing-numerics-of-different-types">Comparing Numerics of Different Types</h2>
9999
<p>The method overloads that compare two objects make special provision so that numeric
100100
values of different types compare as expected. This assert succeeds:</p>
101-
<pre><code class="lang-csharp">ClassicAssert.AreEqual(5, 5.0);
102-
</code></pre>
103-
<h2 id="comparing-floating-point-values">Comparing Floating Point Values</h2>
101+
<pre><code class="lang-csharp" name="AreEqualExamples">[Test]
102+
public void AreEqual_Examples()
103+
{
104+
// Comparing numerics of different types
105+
ClassicAssert.AreEqual(5, 5.0);
106+
107+
// Floating point special values
108+
ClassicAssert.AreEqual(double.PositiveInfinity, double.PositiveInfinity);
109+
ClassicAssert.AreEqual(double.NegativeInfinity, double.NegativeInfinity);
110+
ClassicAssert.AreEqual(double.NaN, double.NaN);
111+
112+
// Basic equality
113+
ClassicAssert.AreEqual(&quot;expected&quot;, &quot;expected&quot;);
114+
ClassicAssert.AreEqual(42, 42);
115+
116+
// With tolerance for floating point
117+
ClassicAssert.AreEqual(2.1 + 1.2, 3.3, 0.001);
118+
}
119+
</code></pre><h2 id="comparing-floating-point-values">Comparing Floating Point Values</h2>
104120
<p>Values of type float and double are compared using an additional
105121
argument that indicates a tolerance within which they will be considered
106122
as equal.</p>
107123
<p>Special values are handled so that the following Asserts succeed:</p>
108-
<pre><code class="lang-csharp">ClassicAssert.AreEqual(double.PositiveInfinity, double.PositiveInfinity);
109-
ClassicAssert.AreEqual(double.NegativeInfinity, double.NegativeInfinity);
110-
ClassicAssert.AreEqual(double.NaN, double.NaN);
111-
</code></pre>
112-
<h2 id="comparing-arrays-and-collections">Comparing Arrays and Collections</h2>
124+
<pre><code class="lang-csharp" name="AreEqualExamples">[Test]
125+
public void AreEqual_Examples()
126+
{
127+
// Comparing numerics of different types
128+
ClassicAssert.AreEqual(5, 5.0);
129+
130+
// Floating point special values
131+
ClassicAssert.AreEqual(double.PositiveInfinity, double.PositiveInfinity);
132+
ClassicAssert.AreEqual(double.NegativeInfinity, double.NegativeInfinity);
133+
ClassicAssert.AreEqual(double.NaN, double.NaN);
134+
135+
// Basic equality
136+
ClassicAssert.AreEqual(&quot;expected&quot;, &quot;expected&quot;);
137+
ClassicAssert.AreEqual(42, 42);
138+
139+
// With tolerance for floating point
140+
ClassicAssert.AreEqual(2.1 + 1.2, 3.3, 0.001);
141+
}
142+
</code></pre><h2 id="comparing-arrays-and-collections">Comparing Arrays and Collections</h2>
113143
<p>NUnit is able to compare single-dimensioned arrays, multi-dimensioned arrays,
114144
nested arrays (arrays of arrays) and collections. Two arrays or collections are considered equal
115145
if they have the same dimensions and if each pair of corresponding elements is equal.</p>

articles/nunit/writing-tests/assertions/classic-assertions/Assert.True.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ <h1 id="classicasserttrue">ClassicAssert.True</h1>
9797
</code></pre>
9898
<p>You can also use <strong>Assert.That</strong> with a Boolean argument to achieve the
9999
same result.</p>
100-
<pre><code class="lang-csharp">Assert.That(condition);
101-
</code></pre>
102-
<h2 id="see-also">See Also</h2>
100+
<pre><code class="lang-csharp" name="TrueExamples">[Test]
101+
public void True_Examples()
102+
{
103+
ClassicAssert.True(2 + 2 == 4);
104+
ClassicAssert.IsTrue(true);
105+
ClassicAssert.True(&quot;Hello&quot;.StartsWith(&quot;H&quot;));
106+
107+
}
108+
</code></pre><h2 id="see-also">See Also</h2>
103109
<ul>
104110
<li><a class="xref" href="../../constraints/Constraints.html#condition-constraints">Condition Constraints</a></li>
105111
</ul>

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,48 +96,42 @@ <h5>Warning</h5>
9696
ignored by NUnit and has no effect in current releases.</p>
9797
</div>
9898
<h2 id="test-fixture-syntax">Test Fixture Syntax</h2>
99-
<pre><code class="lang-csharp">namespace NUnit.Tests
99+
<pre><code class="lang-csharp" name="CategoryTestFixtureExample">[Test]
100+
public void Test_InLongRunningCategory()
100101
{
101-
using System;
102-
using NUnit.Framework;
103-
104-
[TestFixture]
105-
[Category(&quot;LongRunning&quot;)]
106-
public class LongRunningTests
107-
{
108-
// ...
109-
}
102+
Assert.Pass(&quot;This test is in the LongRunning category&quot;);
110103
}
111-
</code></pre>
112-
<h2 id="test-syntax">Test Syntax</h2>
113-
<pre><code class="lang-csharp">namespace NUnit.Tests
104+
</code></pre><h2 id="test-syntax">Test Syntax</h2>
105+
<pre><code class="lang-csharp" name="CategoryTestExample">[Test]
106+
[Category(&quot;Fast&quot;)]
107+
public void FastTest()
114108
{
115-
using System;
116-
using NUnit.Framework;
109+
Assert.Pass(&quot;This is a fast test&quot;);
110+
}
117111

118-
[TestFixture]
119-
public class SuccessTests
120-
{
121-
[Test]
122-
[Category(&quot;Long&quot;)]
123-
public void VeryLongTest()
124-
{ /* ... */ }
112+
[Test]
113+
[Category(&quot;Slow&quot;)]
114+
[Category(&quot;Database&quot;)]
115+
public void SlowDatabaseTest()
116+
{
117+
Assert.Pass(&quot;This test has multiple categories&quot;);
125118
}
126-
</code></pre>
127-
<h3 id="custom-category-attributes">Custom Category Attributes</h3>
119+
</code></pre><h3 id="custom-category-attributes">Custom Category Attributes</h3>
128120
<p>Custom attributes that derive from <strong>CategoryAttribute</strong> will be recognized by NUnit. The default protected constructor
129121
of CategoryAttribute sets the category name to the name of your class.</p>
130122
<p>Here's an example that creates a category of Critical tests. It works just like any other category, but has a simpler
131123
syntax. A test reporting system might make use of the attribute to provide special reports.</p>
132-
<pre><code class="lang-csharp">[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
124+
<pre><code class="lang-csharp" name="CustomCategoryAttribute">[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
133125
public class CriticalAttribute : CategoryAttribute
134126
{ }
135-
</code></pre>
136-
<pre><code class="lang-csharp">[Test, Critical]
137-
public void MyTest()
138-
{ /*...*/ }
139-
</code></pre>
140127

128+
[Test]
129+
[Critical]
130+
public void CriticalTest()
131+
{
132+
Assert.Pass(&quot;This test uses a custom category attribute&quot;);
133+
}
134+
</code></pre>
141135
</article>
142136
</div>
143137

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,11 @@ <h1 id="description">Description</h1>
8989
<p>The Description attribute is used to apply descriptive text to a Test, TestFixture or Assembly. The text appears in the
9090
XML output file.</p>
9191
<h2 id="example">Example</h2>
92-
<pre><code class="lang-csharp">[assembly: Description(&quot;Assembly description here&quot;)]
93-
94-
namespace NUnit.Tests
92+
<pre><code class="lang-csharp" name="DescriptionTestExample">[Test]
93+
[Description(&quot;This test should always pass&quot;)]
94+
public void AlwaysPassingTest()
9595
{
96-
using System;
97-
using NUnit.Framework;
98-
99-
[TestFixture, Description(&quot;Fixture description here&quot;)]
100-
public class SomeTests
101-
{
102-
[Test, Description(&quot;Test description here&quot;)]
103-
public void OneTest()
104-
{ /* ... */ }
105-
}
96+
Assert.Pass(&quot;Test passed as expected&quot;);
10697
}
10798
</code></pre>
10899
<div class="NOTE">

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,13 @@ <h2 id="test-fixture-syntax">Test Fixture Syntax</h2>
107107
}
108108
</code></pre>
109109
<h2 id="test-syntax">Test Syntax</h2>
110-
<pre><code class="lang-csharp">namespace NUnit.Tests
110+
<pre><code class="lang-csharp" name="IgnoreTestExample">[Test]
111+
[Ignore(&quot;Test is not ready yet&quot;)]
112+
public void IgnoredTest()
111113
{
112-
using System;
113-
using NUnit.Framework;
114-
115-
[TestFixture]
116-
public class SuccessTests
117-
{
118-
[Test]
119-
[Ignore(&quot;Ignore a test&quot;)]
120-
public void IgnoredTest()
121-
{ /* ... */ }
122-
}
114+
Assert.Fail(&quot;This test is ignored and should not run&quot;);
123115
}
124-
</code></pre>
125-
<h2 id="ignore-until">Ignore Until</h2>
116+
</code></pre><h2 id="ignore-until">Ignore Until</h2>
126117
<p>The <code>Until</code> named parameter allows you to ignore a test for a specific period of time, after which the test will run
127118
normally. The until date must be a string that can be parsed to a date.</p>
128119
<pre><code class="lang-csharp">[TestFixture]

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ <h1 id="range">Range</h1>
104104
</code></pre>
105105
<h2 id="example">Example</h2>
106106
<p>The following test will be executed nine times.</p>
107-
<pre><code class="lang-csharp">[Test]
108-
public void MyTest(
107+
<pre><code class="lang-csharp" name="RangeAttributeExample">[Test]
108+
public void RangeAttribute_Example(
109109
[Values(1, 2, 3)] int x,
110110
[Range(0.2, 0.6, 0.2)] double d)
111111
{
112-
/* ... */
112+
Assert.That(x, Is.GreaterThan(0));
113+
Assert.That(d, Is.GreaterThan(0.0));
113114
}
114115
</code></pre>
115116
<p>The MyTest method is called nine times, as follows:</p>

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,38 @@ <h1 id="setup">SetUp</h1>
9292
multiple SetUp methods are only defined at different levels of an inheritance hierarchy, as explained below.</p>
9393
<p>If a SetUp method fails or throws an exception, the test is not executed and a failure or error is reported.</p>
9494
<h2 id="example">Example</h2>
95-
<pre><code class="lang-csharp">namespace NUnit.Tests
95+
<pre><code class="lang-csharp" name="SetUpTearDownExample">[TestFixture]
96+
public class SetupTeardownExamples
9697
{
97-
using System;
98-
using NUnit.Framework;
98+
private int _counter;
9999

100-
[TestFixture]
101-
public class SuccessTests
102-
{
103-
[SetUp] public void Init()
104-
{ /* ... */ }
100+
[SetUp]
101+
public void Init()
102+
{
103+
_counter = 0;
104+
Console.WriteLine(&quot;SetUp method called before each test&quot;);
105+
}
106+
[TearDown]
107+
public void Cleanup()
108+
{
109+
Console.WriteLine(&quot;TearDown method called after each test&quot;);
110+
}
105111

106-
[TearDown] public void Cleanup()
107-
{ /* ... */ }
112+
[Test]
113+
public void TestMethod1()
114+
{
115+
_counter++;
116+
Assert.That(_counter, Is.EqualTo(1));
117+
}
108118

109-
[Test] public void Add()
110-
{ /* ... */ }
111-
}
119+
[Test]
120+
public void TestMethod2()
121+
{
122+
_counter++;
123+
Assert.That(_counter, Is.EqualTo(1));
124+
}
112125
}
113-
</code></pre>
114-
<h2 id="inheritance">Inheritance</h2>
126+
</code></pre><h2 id="inheritance">Inheritance</h2>
115127
<p>The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method
116128
will be called before each test method in the derived class.</p>
117129
<p>You may define a SetUp method in the base class and another in the derived class. NUnit will call base class SetUp

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ <h1 id="values">Values</h1>
9696
This default may be modified by use of specific attributes on the
9797
test method itself.</p>
9898
<h2 id="example">Example</h2>
99-
<pre><code class="lang-csharp">[Test]
100-
public void MyTest([Values(1, 2, 3)] int x, [Values(&quot;A&quot;, &quot;B&quot;)] string s)
99+
<pre><code class="lang-csharp" name="ValuesAttributeExample">[Test]
100+
public void ValuesAttribute_BasicExample([Values(1, 2, 3)] int x, [Values(&quot;A&quot;, &quot;B&quot;)] string s)
101101
{
102-
/* ... */
102+
Assert.That(x, Is.GreaterThan(0));
103+
Assert.That(s, Is.Not.Null);
103104
}
104105
</code></pre>
105106
<p>The above test will be executed six times, as follows:</p>
@@ -114,21 +115,28 @@ <h2 id="values-with-enum-or-boolean">Values with Enum or Boolean</h2>
114115
<p>The values attribute works in a special way with Enums and Boolean parameters.</p>
115116
<p>When used without any arguments, the <strong>[Values]</strong> attribute on an enum parameter
116117
will automatically include all possible values of the enumeration.</p>
117-
<pre><code class="lang-csharp">[Test]
118-
public void MyEnumTest([Values]MyEnumType myEnumArgument)
118+
<pre><code class="lang-csharp" name="ValuesAttributeEnumExample">public enum MyEnumType
119119
{
120-
//...
120+
Value1,
121+
Value2,
122+
Value3
123+
}
124+
125+
[Test]
126+
public void ValuesAttribute_EnumExample([Values] MyEnumType myEnumArgument)
127+
{
128+
Assert.That(myEnumArgument, Is.TypeOf&lt;MyEnumType&gt;());
121129
}
122130
</code></pre>
123131
<p>There is the same support for Boolean values. Add the <strong>[Values]</strong> attribute to a bool
124132
and the method will be run with true and false.</p>
125-
<pre><code class="lang-csharp">[Test]
126-
public void MyBoolTest([Values]bool value)
133+
<pre><code class="lang-csharp" name="ValuesAttributeBoolExample">[Test]
134+
public void ValuesAttribute_BoolExample([Values] bool value)
127135
{
128-
//...
136+
// This test will run twice: once with true, once with false
137+
Assert.That(value is true or false);
129138
}
130-
</code></pre>
131-
<h2 id="see-also">See also</h2>
139+
</code></pre><h2 id="see-also">See also</h2>
132140
<ul>
133141
<li><a href="range.html">Range Attribute</a></li>
134142
<li><a href="random.html">Random Attribute</a></li>

articles/nunit/writing-tests/constraints/AndConstraint.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ <h2 id="syntax">Syntax</h2>
9494
<pre><code class="lang-csharp">&lt;Constraint&gt;.And.&lt;Constraint&gt;
9595
</code></pre>
9696
<h2 id="examples-of-use">Examples of Use</h2>
97-
<pre><code class="lang-csharp">Assert.That(2.3, Is.GreaterThan(2.0).And.LessThan(3.0));
98-
</code></pre>
99-
<h2 id="evaluation-order-and-precedence">Evaluation Order and Precedence</h2>
97+
<pre><code class="lang-csharp" name="AndConstraintExamples">[Test]
98+
public void AndConstraint_Examples()
99+
{
100+
Assert.That(2.3, Is.GreaterThan(2.0).And.LessThan(3.0));
101+
102+
int? i = 10;
103+
Assert.That(i, Is.Not.Null.And.GreaterThan(9));
104+
}
105+
</code></pre><h2 id="evaluation-order-and-precedence">Evaluation Order and Precedence</h2>
100106
<p>Note that the constraint evaluates the sub-constraints left to right, meaning that <code>Assert.That(i, Is.Not.Null.And.GreaterThan(9));</code> where <code>i</code> is a nullable <code>int</code> will work for <code>10</code>, but fail for <code>null</code> with the message
101107
<code>Expected: not null and greater than 9. But was: null</code>. <code>Assert.That(i, Is.GreaterThan(9).And.Not.Null);</code> will also
102108
succeed for <code>10</code>, but throw an exception for <code>null</code>, as <code>null</code> cannot be compared to <code>9</code>.</p>

articles/nunit/writing-tests/constraints/CollectionContainsConstraint.html

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,19 @@ <h2 id="modifiers">Modifiers</h2>
102102
...Using&lt;T&gt;(Comparison&lt;T&gt; comparer)
103103
</code></pre>
104104
<h2 id="examples-of-use">Examples of Use</h2>
105-
<pre><code class="lang-csharp">int[] iarray = new int[] { 1, 2, 3 };
106-
string[] sarray = new string[] { &quot;a&quot;, &quot;b&quot;, &quot;c&quot; };
107-
Assert.That(iarray, Has.Member(3));
108-
Assert.That(sarray, Has.Member(&quot;b&quot;));
109-
Assert.That(sarray, Contains.Item(&quot;c&quot;));
110-
Assert.That(sarray, Has.No.Member(&quot;x&quot;));
111-
Assert.That(iarray, Does.Contain(3));
112-
</code></pre>
113-
<h2 id="note">Note</h2>
105+
<pre><code class="lang-csharp" name="CollectionContainsExamples">[Test]
106+
public void CollectionContains_Examples()
107+
{
108+
int[] intArray = [1, 2, 3];
109+
string[] stringArray = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
110+
111+
Assert.That(intArray, Has.Member(3));
112+
Assert.That(stringArray, Has.Member(&quot;b&quot;));
113+
Assert.That(stringArray, Contains.Item(&quot;c&quot;));
114+
Assert.That(stringArray, Has.No.Member(&quot;x&quot;));
115+
Assert.That(intArray, Does.Contain(3));
116+
}
117+
</code></pre><h2 id="note">Note</h2>
114118
<p><code>Has.Member()</code>, <code>Contains.Item()</code> and <code>Does.Contain()</code> work the same as <code>Has.Some.EqualTo()</code>. The last statement
115119
generates a <a href="SomeItemsConstraint.html">SomeItemsConstraint</a> based on an <a href="EqualConstraint.html">EqualConstraint</a> and offers
116120
additional options such as ignoring case or specifying a tolerance. The syntax on this page may be viewed as a shortcut

0 commit comments

Comments
 (0)