Search code examples
c#nunit

Is it possible to parameterize an NUnit test?


I would like to write a callable function that accepts two objects, and compares 30+ properties of those objects with asserts. The issue is this needs to be done for about 20 existing unit tests and most future tests, and writing out the 30+ asserts each time is both time and space consuming.

I currently have a non unit test function that compares the objects, and returns a string with "pass" or a "failure" message, and use an assert to validate that in each unit test. However, its quite messy and I feel like I'm going against proper unit testing methods.

Is there a way to make a function that is callable from inside unit tests that uses asserts to check conditions?


Solution

  • To answer the final part, you can of course have Asserts inside another function. Asserts work by raising exceptions which the test runner catches, and interprets as a failure, so have a Test like so will work fine:

    public void CheckAsserts(string value)
    {
        Assert.IsNotNull(value);
    }
    
    [TestCase("yes!")]
    public void MyTest(string value)
    {
        CheckAsserts(value);
    }