C#, NUnit Assert in a Loop

后端 未结 3 1966
一个人的身影
一个人的身影 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:17

    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.

提交回复
热议问题