我想用相同的测试类测试一个接口的所有实现。我已经知道TestCaseSourceAttribute,所以我将它设置为从静态testObjects数组加载对象实例。此代码按我的意愿工作:
[TestFixture]
public class MySerializerTests
{
// IStreamSerializers loaded by the TestCaseSource attribute.
static object[] testObjects = new object[]
{
new BinarySerializer(),
new XmlSerializer(),
new JsonSerializer()
};
[Test, TestCaseSource("testObjects")]
public void Serialize_NullStreamArgument_ThrowsArgumentException(IStreamSerializer serializer)
{
Map map = new Map();
Assert.Throws<ArgumentNullException>(() => serializer.Serialize(null, map));
}
}
但是,我必须[TestCaseSource("testObjects")]
对每种方法都使用,这使得我拥有的方法数量相当繁琐。有没有办法用适用于整个测试类的属性替换TextCaseSource属性?也许类似于参数化FestFixture的工作方式?
我想要类似的东西,但是我可以通过测试类的构造函数传递我测试类的实例:
[TestFixture(0)]
[TestFixture(1)]
[TestFixture(2)]
public class MySerializerTests
{
// IStreamSerializers loaded by the TestCaseSource attribute.
static object[] testObjects = new object[]
{
new BinarySerializer(),
new XmlSerializer(),
new JsonSerializer()
};
int currentIndex;
public MySerializerTests(int index)
{
currentIndex = 0;
}
[Test]
public void Serialize_NullStreamArgument_ThrowsArgumentException()
{
Map map = new Map();
Assert.Throws<ArgumentNullException>(() => testObjects[currentIndex].Serialize(null, map));
}
}