测试更多输入
我正在使用 MSTest 进行测试,当我想应用更多输入时,测试看起来像这样:
[TestMethod]
public void SumTest()
{
// data to test
var items = new [] {
new { First = 1, Second = 1, Expected = 2 },
new { First = -1, Second = 1, Expected = 0 },
new { First = 1, Second = 2, Expected = 3 },
new { First = 1, Second = -1, Expected = 0 },
};
ICalculator target = GetSum(); // can be in the loop body
foreach(var item in items)
{
var actual = target.Sum(item.First, item.Second);
Assert.AreEqual(item.Expected, actual);
}
}
我觉得这种测试不是正确的方法。即我想将测试数据生成和测试本身分开。
我知道,MSTest 中有“数据驱动测试”支持,但这对我来说还不够:
- 无法使用某些算法生成
items
集合。 - 我不能使用非原始类型。
那么您对此类测试有何建议?
我想要这样的东西,但我不确定这是否是正确的方法以及某些测试框架是否支持这种情况。
[TestData]
public IEnumerable<object> SumTestData()
{
yield return new { First = 1, Second = 1, Expected = 2 };
yield return new { First = -1, Second = 1, Expected = 0 };
yield return new { First = 1, Second = 2, Expected = 3 };
yield return new { First = 1, Second = -1, Expected = 0 };
}
[TestMethod(DataSource="method:SumTestData")]
public void SumTest(int first, int second, int expected)
{
// this test is runned for each item that is got from SumTestData method
// (property -> parameter mapping is no problem)
ICalculator target = GetSum();
var actual = target.Sum(first, second);
Assert.AreEqual(expected, actual);
}
I'm using MSTest for testing and when I want to apply more inputs then the test looks like this:
[TestMethod]
public void SumTest()
{
// data to test
var items = new [] {
new { First = 1, Second = 1, Expected = 2 },
new { First = -1, Second = 1, Expected = 0 },
new { First = 1, Second = 2, Expected = 3 },
new { First = 1, Second = -1, Expected = 0 },
};
ICalculator target = GetSum(); // can be in the loop body
foreach(var item in items)
{
var actual = target.Sum(item.First, item.Second);
Assert.AreEqual(item.Expected, actual);
}
}
I feel that this kind of testing is not the right way. I.e. I would like to separate testing data generation and testing itself.
I know, there is "data driven test" support in MSTest but it isn't sufficient for me:
- The
items
collection cannot be generated using some algorithm. - I cannot use non-primitive types.
So what is your suggestion for this kind of tests?
I would like to have something like this but I'm not sure if this is the right way and if some testing framework supports this scenario.
[TestData]
public IEnumerable<object> SumTestData()
{
yield return new { First = 1, Second = 1, Expected = 2 };
yield return new { First = -1, Second = 1, Expected = 0 };
yield return new { First = 1, Second = 2, Expected = 3 };
yield return new { First = 1, Second = -1, Expected = 0 };
}
[TestMethod(DataSource="method:SumTestData")]
public void SumTest(int first, int second, int expected)
{
// this test is runned for each item that is got from SumTestData method
// (property -> parameter mapping is no problem)
ICalculator target = GetSum();
var actual = target.Sum(first, second);
Assert.AreEqual(expected, actual);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NUnit 支持该场景:
参数可以是任何类型。
TestCaseData
构造函数采用对象的 param 数组,因此您只需确保测试值可转换为实际的测试方法参数类型。非空参数化测试方法(作者:@RobertKoritnik)
通过使用非空测试方法可以进一步增强上层代码,并提供测试数据的结果。我还提供了创建测试数据方法的替代方法。
NUnit supports that scenario:
The parameters can be any type. The
TestCaseData
constructor takes a param array of objects, so you just have to make sure that your test values are castable to the actual test method parameter types.Non-void parametrised test method (by @RobertKoritnik)
Upper code can be even further enhanced by having a non void test method and provide results along test data. I also provided an alternative of creating test data method.
http://xunit.codeplex.com/ 可以做这样的事情:参见[Theory]属性详细信息http://xunit.codeplex.com/wikipage?title=Comparisons
看起来像这样:
http://xunit.codeplex.com/ can do such stuff: see [Theory] attribute details http://xunit.codeplex.com/wikipage?title=Comparisons
looks something like this:
MSTest(和 NUnit)允许您识别在每次测试之前或实例化测试类时运行的方法。
因此,您可以提取一个方法来设置测试数据并在运行测试之前运行它。
在 MSTest 中,您可以使用 TestInitializeAttribute 来标识在每个测试之前运行的方法,并且可以使用 ClassInitializeAttribute 来标识在创建测试类时运行一次的方法。
MSTest (and NUnit) allows you to identify methods that are run either before each test or when the test class is instantiated.
So you can extract a method that sets up your test data and have it run before your tests are run.
In MSTest you can use the TestInitializeAttribute to identify a method that is run before each test and you can use the ClassInitializeAttribute to identify a method that is run once when the test class is created.