C#、NUnit 在循环中断言

发布于 2024-10-17 12:00:59 字数 1107 浏览 0 评论 0原文

我有一项学校作业,需要创建数据驱动风格的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命比纸薄 2024-10-24 12:00:59

[TestCaseSource] 属性将允许您执行此操作。您可以创建一个返回测试用例的可枚举列表的函数

public IEnumerable<Database1DataSet.GetProductDetailsTestRow> GetTestCases()
{
    SqlDataAdapter da = new SqlDataAdapter("Select * From GetProductDetailsTest;", Connection);
    Database1DataSet.GetProductDetailsTestDataTable dt = new Database1DataSet.GetProductDetailsTestDataTable();
    da.Fill(dt);

    foreach (Database1DataSet.GetProductDetailsTestRow dr in dt.Rows)
    {
        yield return dr;
    }
}

,然后您可以在以下位置传递 TestCaseSource:

    [Test, TestCaseSource("GetTestCases")]
    public void GetProductDetailsTest(Database1DataSet.GetProductDetailsTestRow dr)
    {
        if (pm.GetProductById(dr.productId) == null)
            Assert.Fail("Id of test case: " + dr.id + ", Product id of failure: " + dr.productId);
        }
    }

The [TestCaseSource] attribute will allow you to do this. You can create a function that returns an enumerable list of test cases

public IEnumerable<Database1DataSet.GetProductDetailsTestRow> GetTestCases()
{
    SqlDataAdapter da = new SqlDataAdapter("Select * From GetProductDetailsTest;", Connection);
    Database1DataSet.GetProductDetailsTestDataTable dt = new Database1DataSet.GetProductDetailsTestDataTable();
    da.Fill(dt);

    foreach (Database1DataSet.GetProductDetailsTestRow dr in dt.Rows)
    {
        yield return dr;
    }
}

then you can pass a TestCaseSource in:

    [Test, TestCaseSource("GetTestCases")]
    public void GetProductDetailsTest(Database1DataSet.GetProductDetailsTestRow dr)
    {
        if (pm.GetProductById(dr.productId) == null)
            Assert.Fail("Id of test case: " + dr.id + ", Product id of failure: " + dr.productId);
        }
    }
揽月 2024-10-24 12:00:59

您可以使用 nunit 中的数据驱动测试来执行此操作,但我不确定您是否可以对来自数据库的数据执行此操作。大致如下:

[TestFixture]
public class RowTestSample
{
     [RowTest]
     [Row( 1)]
     [Row( 2)]
     [Row( 3)]
     [Row( 4)]
     [Row( 5)]
     [Row( 6)]
     public void GetProductById(int productId)
     {
          Assert.That(pm.GetProductById(productId),Is.Not.Null);
     }
}

其中 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:

[TestFixture]
public class RowTestSample
{
     [RowTest]
     [Row( 1)]
     [Row( 2)]
     [Row( 3)]
     [Row( 4)]
     [Row( 5)]
     [Row( 6)]
     public void GetProductById(int productId)
     {
          Assert.That(pm.GetProductById(productId),Is.Not.Null);
     }
}

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.

梦一生花开无言 2024-10-24 12:00:59

除了按照 Sam Holder 建议使用 RowTest 扩展之外,您还可以使用 TestCaseAttribute 来实现此目的:

[TestFixture]
public class TestCaseSample 
{      
    [TestCase(1)]
    [TestCase(2)]
    [TestCase(3)]
    [TestCase(4)]
    [TestCase(5)]
    [TestCase(6)]
    public void GetProductById(int productId)
    {           
        Assert.That(pm.GetProductById(productId),Is.Not.Null);
    } 
} 

Beside using the RowTest extension as suggested by Sam Holder you can also use the TestCaseAttribute for this:

[TestFixture]
public class TestCaseSample 
{      
    [TestCase(1)]
    [TestCase(2)]
    [TestCase(3)]
    [TestCase(4)]
    [TestCase(5)]
    [TestCase(6)]
    public void GetProductById(int productId)
    {           
        Assert.That(pm.GetProductById(productId),Is.Not.Null);
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文