我有一个POCO课程:
public class SomeEntity {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我想使用SomeEntity类中的不同值来测试其他一些类。问题是我需要测试许多属性的不同组合。例如:
因此,在每个测试中,我都想像这样创建测试对象:
// test that someOtherObject always return the same result
// for testObject with ID = 1 and accepts any values from range of
// values for FirstName and LastName
var testObject = new SomeEntity {
Id = 1, // this must be always 1 for this test
FirstName = It.IsIn(someListOfPossibleValues), // each of this values must be accepted in test
LastName = It.IsIn(someListOfPossibleValues) // each of this values must be accepted in test
}
var result = someOtherObject.DoSomething(testObject);
Assert.AreEqual("some expectation", result);
我不想使用nunit TestCase,因为会有很多组合(巨大的矩阵)。
我尝试在调试中运行此测试,它仅使用列表中的第一个值进行一次DoSomething调用。
问题:如何组合所有可能的值?
您使用It.IsIn
不正确;仅用于匹配参数时,即Verify
用于检查值是否在一组可能值中的Setup
调用中,或用于控制Moq响应方式的调用中。它并非旨在用于以某种方式生成一组测试数据。这正是NUnit的的ValuesAttribute
和ValueSourceAttribute
是。
关于由于组合太多而反对使用NUnit的原因,是因为您不想编写所有组合还是因为不想进行太多测试?如果是前者,则使用NUnit的属性,CombinatorialAttribute
使NUnit完成创建所有可能测试的工作。像这样:
[Test]
[Combinatorial]
public void YourTest(
[Values(null, "", "John")] string firstName,
[Values(null, "", "Doe")] string lastName)
{
var testObject = new SomeEntity {
Id = 1, // this must be always 1 for this test
FirstName = firstName,
LastName = lastName
};
var result = someOtherObject.DoSomething(testObject);
Assert.AreEqual("some expectation", result);
}
该测试将运行9次(2个参数中的每个参数都有3个数据,所以3 * 3)。请注意,组合是默认设置。
但是,如果您仅对结果中的测试数量表示反对,那么请专注于您实际想要测试和编写这些测试的内容,而不是用所有可能的值来应对。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句