Specifies a method that is used to generate tests statically at test exploration time.

Namespace:  MbUnit.Framework
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class StaticTestFactoryAttribute : ContributionMethodPatternAttribute
Visual Basic (Declaration)
Public Class StaticTestFactoryAttribute _
	Inherits ContributionMethodPatternAttribute

Remarks

The tests created by the static test factory are considered to be children of the fixture that contains the factory method that generated them. Because the tests are created statically, they will appear in the test runner and they can be filtered as usual. However, this means that the tests will be generated only when the fixture is being explored rather than each time the fixture is executed. It also means that the factory method must be static and cannot be parameterized.

Contrast with DynamicTestFactoryAttribute.

The method to which this attribute is applied must be declared by the fixture class. It must be static and must not have any parameters. It must return an enumeration of values of type Test.

Tests built by a StaticTestFactoryAttribute will not be visible in ReSharper and other test runners that rely only on source code and metadata exclusively. This is because the test factory must be executed in order to populate the list of tests but the code might not be available in a compiled form during test exploration. The tests should still run as part of the containing fixture although the results may only be visible in the full test report.

Examples

A simple static test factory that reads some data from a file and generates a number of static tests. The file is read at test exploration time, so if the file changes, the test package must be reloaded to obtain the new contents.

CopyC#
[StaticTestFactory]
public static IEnumerable<Test> CreateStaticTests()
{
    foreach (string searchTerm in File.ReadAllLines("SearchTerms.txt"))
    {
        var copySearchTerm = searchTerm; // Local copy for the closure.
        yield return new TestCase("Search Term: " + copySearchTerm, () => {
            var searchEngine = new SearchEngine();
            Assert.IsNotEmpty(searchEngine.GetSearchResults(copySearchTerm));
        });
    }
}

Inheritance Hierarchy

See Also