Skip to content

Commit 956978f

Browse files
authored
Add documentation for TestFixtureSource TypeArgs and move examples into snippets (#1121)
* Add documenation for testfituresource typeargs and move examples into snippets * Update docs/articles/nunit/writing-tests/attributes/testfixturesource.md
1 parent 138202f commit 956978f

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed

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

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,7 @@ its data using one of the forms of **TestFixtureSourceAttribute**:
1111

1212
### Form 1 - [TestFixtureSource(string sourceName)]
1313

14-
```csharp
15-
[TestFixtureSource(nameof(FixtureArgs))]
16-
public class MyTestClass
17-
{
18-
public MyTestClass(string word, int num) { ... }
19-
20-
/* ... */
21-
22-
static object [] FixtureArgs = {
23-
new object[] { "Question", 1 },
24-
new object[] { "Answer", 42 }
25-
};
26-
}
27-
```
14+
[!code-csharp[BasicTestFixtureSource](~/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs#StaticPropertyInOwnClass)]
2815

2916
The single attribute argument in this form is a string representing the name of the source used to provide arguments for
3017
constructing the `TestFixture`. It has the following characteristics:
@@ -40,23 +27,7 @@ constructing the `TestFixture`. It has the following characteristics:
4027

4128
### Form 2 - [TestFixtureSource(Type sourceType, string sourceName)]
4229

43-
```csharp
44-
[TestFixtureSource(typeof(AnotherClass), nameof(AnotherClass.FixtureArgs)]
45-
public class MyTestClass
46-
{
47-
public MyTestClass(string word, int num) { ... }
48-
49-
...
50-
}
51-
52-
class AnotherClass
53-
{
54-
static object [] FixtureArgs = {
55-
new object[] { "Question", 1 },
56-
new object[] { "Answer", 42 }
57-
};
58-
}
59-
```
30+
[!code-csharp[StaticPropertyInAnotherClass](~/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs#StaticPropertyInAnotherClass)]
6031

6132
The first argument of the attribute in this form is a Type representing the class that will provide the test fixture
6233
data.
@@ -75,42 +46,26 @@ characteristics:
7546

7647
### Form 3 - [TestFixtureSource(Type sourceType)]
7748

78-
```csharp
79-
[TestFixtureSource(typeof(FixtureArgs))]
80-
public class MyTestClass
81-
{
82-
public MyTestClass(string word, int num) { /* ... */ }
83-
84-
/* ... */
85-
}
86-
87-
class FixtureArgs: IEnumerable
88-
{
89-
public IEnumerator GetEnumerator()
90-
{
91-
yield return new object[] { "Question", 1 };
92-
yield return new object[] { "Answer", 42 };
93-
}
94-
}
95-
```
96-
97-
The Type argument in this form represents the class that provides test cases. It must have a default constructor and
49+
[!code-csharp[SourceFromEnumerable](~/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs#SourceFromEnumerable)]
50+
51+
The Type argument in this form represents the class that provides test fixtures. It must have a default constructor and
9852
implement `IEnumerable`.
9953

10054
The individual items returned by the enumerator must either be object arrays or derive from the `TestFixtureParameters`
10155
class. Arguments must be consistent with the fixture constructor.
10256

10357
## Named Parameters
10458

105-
`TestCaseSourceAttribute` supports one named parameter:
59+
`TestFixtureSourceAttribute` supports two named parameters:
10660

107-
* **Category** is used to assign one or more categories to every test case returned from this source.
61+
* **Category** is used to assign one or more categories to every test fixture returned from this source.
62+
* **TypeArgs** can be used to explicitly specify the `Type`s to be used when constructing a generic test fixture. (_NUnit 4.5+_)
10863

109-
## Test Case Construction
64+
## Test Fixture Construction
11065

111-
In constructing tests, NUnit uses each item returned by the enumerator as follows:
66+
In constructing fixtures, NUnit uses each item returned by the enumerator as follows:
11267

113-
* If it is an object deriving from `TestFixtureParameters`, its properties are used to provide the test case. NUnit
68+
* If it is an object deriving from `TestFixtureParameters`, its properties are used to provide the test fixture's data. NUnit
11469
provides the [TestFixtureData](xref:testfixturedata) class for this purpose.
11570
* If it is an `object[]`, its members are used to provide the arguments for the method. This is the approach taken in
11671
the examples above.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NUnit.Framework;
2+
using System.Collections;
3+
namespace Snippets.NUnit.Attributes
4+
{
5+
public class TestFixtureSourceAttributeExamples
6+
{
7+
internal static class StaticPropertyInOwnClass
8+
{
9+
#region StaticPropertyInOwnClass
10+
[TestFixtureSource(nameof(FixtureArgs))]
11+
public class MyTestClass
12+
{
13+
public MyTestClass(string word, int num) { /* ... */ }
14+
15+
/* Tests */
16+
17+
static object[] FixtureArgs = {
18+
new object[] { "Question", 1 },
19+
new object[] { "Answer", 42 }
20+
};
21+
}
22+
#endregion
23+
}
24+
25+
internal static class StaticPropertyInAnotherClass {
26+
#region StaticPropertyInAnotherClass
27+
[TestFixtureSource(typeof(AnotherClass), nameof(AnotherClass.FixtureArgs))]
28+
public class MyTestClass
29+
{
30+
public MyTestClass(string word, int num) { /* ... */ }
31+
32+
/* Tests */
33+
}
34+
35+
public class AnotherClass
36+
{
37+
public static object[] FixtureArgs = {
38+
new object[] { "Question", 1 },
39+
new object[] { "Answer", 42 }
40+
};
41+
}
42+
#endregion
43+
}
44+
45+
internal static class SourceFromEnumerable
46+
{
47+
#region SourceFromEnumerable
48+
[TestFixtureSource(typeof(FixtureArgs))]
49+
public class MyTestClass
50+
{
51+
public MyTestClass(string word, int num) { /* ... */ }
52+
53+
/* ... */
54+
}
55+
56+
class FixtureArgs : IEnumerable
57+
{
58+
public IEnumerator GetEnumerator()
59+
{
60+
yield return new object[] { "Question", 1 };
61+
yield return new object[] { "Answer", 42 };
62+
}
63+
}
64+
#endregion
65+
}
66+
}
67+
}

docs/snippets/Snippets.NUnit/Snippets.NUnit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0</TargetFrameworks>
4+
<TargetFrameworks>net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<NoWarn>NUnit2007;NUnit2009;CS7022</NoWarn>

docs/snippets/Snippets.NUnitLite/Snippets.NUnitLite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<RootNamespace>Snippets.NUnitLite</RootNamespace>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>

0 commit comments

Comments
 (0)