Specifies a method that is used to generate tests dynamically at runtime.

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

Syntax

C#
public class DynamicTestFactoryAttribute : TestMethodPatternAttribute
Visual Basic (Declaration)
Public Class DynamicTestFactoryAttribute _
	Inherits TestMethodPatternAttribute

Remarks

The tests created by the dynamic test factory are considered to be children of the factory method that generated them. Because the tests are created dynamically, they will not appear in the test tree (so most test runners will not find them) and they are not directly affected by test filters. However, the dynamic test factory is represented as a test so it will be visible in the test tree.

Contrast with StaticTestFactoryAttribute.

The method to which this attribute is applied must be declared by the fixture class. It may have parameters and be data-driven just like an ordinary test method! The method may be static. It must return an enumeration of values of type Test.

Examples

A simple dynamic test factory that reads some data from a file and generates a number of dynamic tests.

CopyC#
[DynamicTestFactory]
public IEnumerable<Test> CreateDynamicTests()
{
    foreach (string searchTerm in File.ReadAllLines("SearchTerms.txt"))
    {
        yield return new TestCase("Search Term: " + searchTerm, () => {
            var searchEngine = new SearchEngine();
            Assert.IsNotEmpty(searchEngine.GetSearchResults(searchTerm));
        });
    }
}

A data-driven test factory that reads some data from a file and generates a number of dynamic tests repeatedly in multiple configurations.

CopyC#
[DynamicTestFactory]
[Row("Google")]
[Row("Yahoo")]
public IEnumerable<Test> CreateDynamicTests(string searchProvider)
{
    foreach (string searchTerm in File.ReadAllLines("SearchTerms.txt"))
    {
        yield return new TestCase("Search Term: " + searchTerm, () => {
            var searchEngine = new SearchEngine(searchProvider);
            Assert.IsNotEmpty(searchEngine.GetSearchResults(searchTerm));
        });
    }
}

Inheritance Hierarchy

See Also