docs/articles/nunit/writing-tests/attributes/testfixturesource.md
TestFixtureSourceAttribute is used on a parameterized fixture to identify the source from which the required
constructor arguments will be provided. The data is kept separate from the fixture itself and may be used by multiple
fixtures. See Parameterized Tests for a general introduction to tests with arguments.
Consider a test fixture class taking two parameters in its constructor, a string and an int. We can specify the test and its data using one of the forms of TestFixtureSourceAttribute:
[!code-csharpBasicTestFixtureSource]
The single attribute argument in this form is a string representing the name of the source used to provide arguments for
constructing the TestFixture. It has the following characteristics:
IEnumerable or a type that implements IEnumerable. For fields an array is generally used. For
properties and methods, you may return an array or implement your own iterator.
IAsyncEnumerable or a type that implements IAsyncEnumerable. (NUnit 4+)Task<T>. (NUnit 3.14+)TestFixtureParameters class. Arguments must be consistent with the fixture constructor.[!code-csharpStaticPropertyInAnotherClass]
The first argument of the attribute in this form is a Type representing the class that will provide the test fixture data.
The second argument is a string representing the name of the source used to provide test fixtures. It has the following characteristics:
IEnumerable or a type that implements IEnumerable. For fields an array is generally used. For
properties and methods, you may return an array or implement your own iterator.
IAsyncEnumerable or a type that implements IAsyncEnumerable. (NUnit 4+)Task<T>. (NUnit 3.14+)TestFixtureParameters class. Arguments must be consistent with the fixture constructor.[!code-csharpSourceFromEnumerable]
The Type argument in this form represents the class that provides test fixtures. It must have a default constructor and
implement IEnumerable.
The individual items returned by the enumerator must either be object arrays or derive from the TestFixtureParameters
class. Arguments must be consistent with the fixture constructor.
TestFixtureSourceAttribute supports two named parameters:
Types to be used when constructing a generic test fixture. (NUnit 4.5+)In constructing fixtures, NUnit uses each item returned by the enumerator as follows:
TestFixtureParameters, its properties are used to provide the test fixture's data. NUnit
provides the TestFixtureData class for this purpose.object[], its members are used to provide the arguments for the method. This is the approach taken in
the examples above.IEnumerable and IEnumerator may be used but NUnit will actually deal with the underlying IEnumerator
in the current release.