I remember an article from Jeff Brown, the lead developer of Gallio/MbUnit, which talks about dynamic and static factories in MbUnit v3. There is a nice example which describes how to create static and dynamic test factories.
In the other hand, test data factories are easier to create, and provide an interesting alternative to the [Row]
-based data-driven tests, which only accept primitive values as input (a limitation of C# for the parameters passed to an attribute)
Here is an example for MbUnit v3. The data factory is here a property of the test fixture, but it can be a method or a field, which may be located in a nested type or in an external type. This is indeed a very flexible feature :)
[TestFixture]
public class MyTestFixture()
{
private IEnumerable<object[]> ProvideTestData
{
get
{
yield return new object[] { new Foo(123), "Hello", Color.Blue};
yield return new object[] { new Foo(456), "Big", Color.Red};
yield return new object[] { new Foo(789), "World", Color.Green};
}
}
[Test, Factory("ProvideTestData")]
public void MyTestMethod(Foo foo, string text, Color color)
{
// Test logic here...
}
}