简体   繁体   English

C#,NUnit在循环中断言

[英]C#, NUnit Assert in a Loop

I have a school assignment where I need to create a data-driven style of NUnit testing. 我有一个学校作业,我需要创建一个数据驱动的NUnit测试样式。 Using the below code, I am able to get the data from the database, but everytime an 'Assert' call fails, the test is stopped. 使用下面的代码,我可以从数据库中获取数据,但每次'Assert'调用失败时,测试都会停止。

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! 基本上我正在寻找的是,如果可能的话,NUnit将显示3个通过的测试和3个失败的测试! Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢! :) :)

The [TestCaseSource] attribute will allow you to do this. [TestCaseSource]属性允许您执行此操作。 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: 然后你可以传递一个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);
        }
    }

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. 您可以使用nunit中的数据驱动测试来完成此操作,但我不确定您是否可以对来自数据库的数据执行此操作。 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. 其中Row(n)中的值是您要测试的产品ID。 This will show as 6 tests, each one with a different value. 这将显示为6个测试,每个测试具有不同的值。

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. 我也不确定这些测试的价值,我想这取决于ProductManager在做什么。

Beside using the RowTest extension as suggested by Sam Holder you can also use the TestCaseAttribute for this: 除了使用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);
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM