C#, NUnit Assert in a Loop

后端 未结 3 1969
一个人的身影
一个人的身影 2021-02-13 09:45

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 \'Ass

3条回答
  •  生来不讨喜
    2021-02-13 10:11

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

    public IEnumerable 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);
            }
        }
    

提交回复
热议问题