docs/articles/nunit/writing-tests/attributes/testfixture.md
This is the attribute that marks a class that contains tests and, optionally, setup or teardown methods.
Most restrictions on a class that is used as a test fixture have now been eliminated. A test fixture class:
If any of these restrictions are violated, the class is not runnable as a test and will display as an error.
It is advisable that the constructor not have any side effects, since NUnit may construct the object multiple times in the course of a session.
Beginning with NUnit 2.5, the TestFixture attribute is optional for non-parameterized, non-generic fixtures. So long as the class contains at least one method marked with the Test, TestCase or TestCaseSource attribute, it will be treated as a test fixture.
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
// ...
}
}
The TestFixtureAttribute may be applied to a base class and is inherited by any derived classes. This includes any abstract base class, so the well-known Abstract Fixture pattern may be implemented if desired.
In order to facilitate use of generic and/or parameterized classes, where the derived class may require a different number of arguments (or type arguments) from the base class, superfluous TestFixture attributes are ignored, using the following rules:
This permits code like the following, which would cause an error if the attribute on the base class were not ignored.
[TestFixture]
public class AbstractFixtureBase
{
/* ... */
}
[TestFixture(typeof(string))]
public class DerivedFixture<T> : AbstractFixtureBase
{
/* ... */
}
Test fixtures may take constructor arguments. Argument values are specified as arguments to the TestFixture attribute. NUnit will construct a separate instance of the fixture for each set of arguments.
Individual fixture instances in a set of parameterized fixtures may be ignored. Set the Ignore named parameter of the reason for ignoring the instance.
Individual fixture instances may be given categories as well. Set the Category named parameter of the attribute to the name of the category or to a comma-separated list of categories.
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-csharpMultipleParameterizedTestFixtures]
You may also use a generic class as a test fixture. In order for NUnit to instantiate the fixture, you must either specify the types to be used as arguments to TestFixtureAttribute or use the named parameter TypeArgs= to specify them. NUnit will construct a separate instance of the fixture for each TestFixtureAttribute you provide.
The following test fixture would be instantiated by NUnit twice, once using an ArrayList and once using a List<int>.
[!code-csharpGenericTestFixtures]
If a Generic fixture, uses constructor arguments, there are three approaches to telling NUnit which arguments are type parameters and which are normal constructor parameters.
[!code-csharpGenericTestFixtureWithParameters]
[!code-csharpSpecifyTypeArgsSeparately]
[!code-csharpDeducedTypesFromArguments]