Skip to content

Commit

Permalink
Update TestFixtureAttributeExamples.cs To give an example of using pa…
Browse files Browse the repository at this point in the history
…rams in a constructor (#807)

* Update TestFixtureAttributeExamples.cs To give an example of using params in a constructor

* Update TestFixtureAttributeExamples.cs

Fix issues with example code

* Update testfixture.md

* Fix: trailing markdown space

* Fix code snippet

* use nullable string for nullable field

---------

Co-authored-by: Sean Killeen <[email protected]>
  • Loading branch information
Shiney and SeanKilleen authored Dec 4, 2023
1 parent afd19b0 commit b010bb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 5 additions & 3 deletions docs/articles/nunit/writing-tests/attributes/testfixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ the name of the category or to a comma-separated list of categories.

### Example

The following test fixture would be instantiated by NUnit three times, passing in each set of arguments to the
appropriate constructor. Note that there are three different constructors, matching the data types provided as
arguments.
The following test fixture would be instantiated by NUnit five times,
passing in each set of arguments to the appropriate constructor. Note
that there are four different constructors, matching the data types
provided as arguments, and the params keyword can be used to allow
passing different numbers of arguments.

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using NUnit.Framework;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
#pragma warning disable NUnit2045
Expand All @@ -14,11 +14,13 @@ public class TestFixtureAttributeExamples
[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip")]
[TestFixture(42, 42, 99)]
[TestFixture('a', 'a', 'b')]
[TestFixture('A', 'A')]
public class ParameterizedTestFixture
{
private readonly string _eq1;
private readonly string _eq2;
private readonly string _neq;
private readonly string? _neq;

public ParameterizedTestFixture(string eq1, string eq2, string neq)
{
Expand All @@ -37,6 +39,17 @@ public ParameterizedTestFixture(int eq1, int eq2, int neq)
_neq = neq.ToString();
}

// Can use params arguments (but not yet optional arguments)
public ParameterizedTestFixture(params char[] eqArguments)
{
_eq1 = eqArguments[0].ToString();
_eq2 = eqArguments[1].ToString();
if (eqArguments.Length > 2)
_neq = eqArguments[2].ToString();
else
_neq = null;
}

[Test]
public void TestEquality()
{
Expand Down Expand Up @@ -162,4 +175,4 @@ public void TestMyArgTypes(T1 t1, T2 t2)
}
}
#endregion
}
}

0 comments on commit b010bb3

Please sign in to comment.