C#、NUnit 在循环中断言
我有一项学校作业,需要创建数据驱动风格的 NUnit 测试。使用下面的代码,我可以从数据库获取数据,但是每次“Assert”调用失败时,测试就会停止。
有什么方法可以实际将循环结果显示为六个不同的测试(考虑到我的数据库中有六行)?
namespace TestClasses
{
[TestFixture]
public class TestingClass : ConnectionClass
{
private ProductManagement pm;
[TestFixtureSetUp]
public void CreateTestClass()
{
pm = new ProductManagement();
}
[TestCase]
public void GetProductDetailsTest()
{
SqlDataAdapter da = new SqlDataAdapter("Select * From GetProductDetailsTest;", Connection);
Database1DataSet.GetProductDetailsTestDataTable dt = new Database1DataSet.GetProductDetailsTestDataTable();
da.Fill(dt);
foreach (Database1DataSet.GetProductDetailsTestRow dr in dt.Rows)
{
if (pm.GetProductById(dr.productId) == null)
Assert.Fail("Id of test case: " + dr.id + ", Product id of failure: " + dr.productId);
}
}
}
}
基本上我正在寻找的是,如果可能的话,让 NUnit 显示 3 个通过的测试和 3 个失败的测试!任何帮助将不胜感激,谢谢! :)
I have a school assignment where I need to create a data-driven style of NUnit testing. Using the below code, I am able to get the data from the database, but everytime an 'Assert' call fails, the test is stopped.
Is there any way in which I can actually show the results of the loop as six different tests (considering I have six rows in my database)?
namespace TestClasses
{
[TestFixture]
public class TestingClass : ConnectionClass
{
private ProductManagement pm;
[TestFixtureSetUp]
public void CreateTestClass()
{
pm = new ProductManagement();
}
[TestCase]
public void GetProductDetailsTest()
{
SqlDataAdapter da = new SqlDataAdapter("Select * From GetProductDetailsTest;", Connection);
Database1DataSet.GetProductDetailsTestDataTable dt = new Database1DataSet.GetProductDetailsTestDataTable();
da.Fill(dt);
foreach (Database1DataSet.GetProductDetailsTestRow dr in dt.Rows)
{
if (pm.GetProductById(dr.productId) == null)
Assert.Fail("Id of test case: " + dr.id + ", Product id of failure: " + dr.productId);
}
}
}
}
Basically what I am looking for is, for NUnit to display 3 passed tests and 3 failed tests, if possible! Any help would be greatly appreciated, thanks! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[TestCaseSource]
属性将允许您执行此操作。您可以创建一个返回测试用例的可枚举列表的函数,然后您可以在以下位置传递 TestCaseSource:
The
[TestCaseSource]
attribute will allow you to do this. You can create a function that returns an enumerable list of test casesthen you can pass a TestCaseSource in:
您可以使用 nunit 中的数据驱动测试来执行此操作,但我不确定您是否可以对来自数据库的数据执行此操作。大致如下:
其中
Row(n)
中的值是您要测试的产品 ID。这将显示为 6 个测试,每个测试都有不同的值。我不确定这些是否可以来自数据库,但无论如何,这可能不是在测试中做的一件好事。
我也不确定这些测试的价值,我想这取决于 ProductManager 正在做什么。
you can do this using the data driven tests in nunit, but i'm not sure you can do it for data that comes from the database. something along the lines of:
where the values in
Row(n)
are the product ids you want to test. This will show as 6 tests, each one with a different value.I'm not sure if these can come from the DB, but probably this is not a good thing to be doing in the test anyway.
I'm not sure of the value in these tests either, I suppose it depends on what ProductManager is doing.
除了按照 Sam Holder 建议使用 RowTest 扩展之外,您还可以使用
TestCaseAttribute
来实现此目的:Beside using the RowTest extension as suggested by Sam Holder you can also use the
TestCaseAttribute
for this: