ValueSourceAttribute (NUnit 2.5)
ValueSourceAttribute is used on individual parameters of a test method to identify a named source for the argument values to be supplied. The attribute has two public constructors.
ValueSourceAttribute(Type sourceType, string sourceName); ValueSourceAttribute(string sourceName);
If sourceType is specified, it represents the class that provides the data. It must have a default constructor.
If sourceType is not specified, the class containing the test method is used. NUnit will construct it using either the default constructor or - if arguments are provided - the appropriate constructor for those arguments.
The sourceName, represents the name of the source that will provide the arguments. It should have the following characteristics:
- It may be a field, a non-indexed property or a method taking no arguments.
- It may be either an instance or a static member.
- It must return an IEnumerable or a type that implements IEnumerable.
- The individual items returned from the enumerator must be compatible with the type of the parameter on which the attribute appears.
Order of Execution
In NUnit 2.5, individual test cases are sorted alphabetically and executed in that order. With NUnit 2.5.1, the individual cases are not sorted, but are executed in the order in which NUnit discovers them. This order does not follow the lexical order of the attributes and will often vary between different compilers or different versions of the CLR.
As a result, when ValueSourceAttribute appears multiple times on a parameter or when other data-providing attributes are used in combination with ValueSourceAttribute, the order of the arguments is undefined.
However, when a single ValueSourceAttribute is used by itself, the order of the arguments follows exactly the order in which the data is returned from the source.
Note on Object Construction
NUnit locates the test cases at the time the tests are loaded, creates instances of each class with non-static sources and builds a list of tests to be executed. Each source object is only created once at this time and is destroyed after all tests are loaded.
If the data source is in the test fixture itself, the object is created using the appropriate constructor for the fixture parameters provided on the TestFixtureAttribute, or the default constructor if no parameters were specified. Since this object is destroyed before the tests are run, no communication is possible between these two phases - or between different runs - except through the parameters themselves.