每个数据库行的 NUnit 测试?
我不知道这是否可能,但我会继续解释我想要做什么。
我想创建一个测试装置,使用来自数据库的 5 种不同类型的输入来运行测试。
TestFixture
Test using input1
Test using input2
Test using input3
Test using input4
Test using input5
这样,我可以从 NUnit GUI 中准确地看到哪个输入导致了失败,但我不知道该怎么做。目前,我的设置如下:
[TestFixture]
public class Tester{
[Test]
public void RunTest(){
var inputs = db.inputs.where(a=>a.id < 6).ToList();
bool testSuccess=true;
foreach(var input in inputs){
bool success = RunTheTest(input);
if(success==false){
testSuccess=false;
}
}
//Tell NUnit that the entire test failed because one input failed
}
}
在这种情况下,在 NUnit 中,我看到:
Tester
RunTest
尽管 RunTest 尝试了 5 个不同的输入,但我只知道是否有一个或多个输入失败,但我不知道是哪个输入失败的。基本上我要问的是是否可以根据我想从数据库中获取的内容动态创建显示在 NUnit GUI 中的测试。
I don't know if this is possible, but I'll go ahead and explain what I'm trying to do.
I want to create a test fixture that runs a test with 5 different types of inputs that come from a database.
TestFixture
Test using input1
Test using input2
Test using input3
Test using input4
Test using input5
This way, I can see from the NUnit GUI exactly which input is causing the failure, but I don't know how to do this. Currently, I have something set up like this:
[TestFixture]
public class Tester{
[Test]
public void RunTest(){
var inputs = db.inputs.where(a=>a.id < 6).ToList();
bool testSuccess=true;
foreach(var input in inputs){
bool success = RunTheTest(input);
if(success==false){
testSuccess=false;
}
}
//Tell NUnit that the entire test failed because one input failed
}
}
In this case, in NUnit, I see:
Tester
RunTest
And even though RunTest tries 5 different inputs, I only know if there was one or more inputs that failed, but I have no idea of which input failed. Basically what I'm asking is if it's possible to dynamically create tests that show up in the NUnit GUI based on whatever I want to grab from the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 TestCaseSource 属性。这将允许您定义另一个方法,该方法在运行时将为从源方法返回的每个项目创建测试用例。
Look at the TestCaseSource attribute. This will let you define another method that, at runtime, will create test cases for each item returned from the source method.
在 foreach 循环中你可以做
Assert.True( success, string.Format("输入:{0}", input ))
;或者,您可以尝试使用 ValueSourceAttribute ,其中 sourceType 为帮助程序类具有返回名为 sourceName 的 IEnumerable 的方法。该方法的实现应该从数据库中获取输入值。
Inside the foreach loop you can do
Assert.True( success, string.Format("Input: {0}", input ))
;Alternately you can try the ValueSourceAttribute with a sourceType being a helper class that has a method that returns an IEnumerable named sourceName. The implementation of this method should fetch the input values from DB.