Legacy Documentation. View NUnit 3 Documentation

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.

NUnit locates value sources at the time the tests are loaded, creates instances of each class having non-static sources and builds a list of tests to be executed. Each instance is only created once at this time and is destroyed after all tests are loaded. In the case of sources located within the same class as the tests, a different instance is created each time the test is run, so no communication is possible using non-static members of the class.