Search code examples
c#unit-testingnunit

NUnit Test Per Database Row?


I don't know if this is possible, but I'll go ahead and explain what I'm trying to do.

I want to create a test fixture that runs a test with 5 different types of inputs that come from a database.

TestFixture
    Test using input1
    Test using input2
    Test using input3
    Test using input4
    Test using input5

This way, I can see from the NUnit GUI exactly which input is causing the failure, but I don't know how to do this. Currently, I have something set up like this:

[TestFixture]
public class Tester{
    [Test]
    public void RunTest(){
        var inputs = db.inputs.where(a=>a.id < 6).ToList();
        bool testSuccess=true;
        foreach(var input in inputs){
            bool success = RunTheTest(input);
            if(success==false){
                testSuccess=false;
            }
        }
        //Tell NUnit that the entire test failed because one input failed
    }
}

In this case, in NUnit, I see:

Tester
    RunTest

And even though RunTest tries 5 different inputs, I only know if there was one or more inputs that failed, but I have no idea of which input failed. Basically what I'm asking is if it's possible to dynamically create tests that show up in the NUnit GUI based on whatever I want to grab from the database.


Solution

  • Inside the foreach loop you can do
    Assert.True( success, string.Format("Input: {0}", input ));

    Alternately you can try the ValueSourceAttribute with a sourceType being a helper class that has a method that returns an IEnumerable named sourceName. The implementation of this method should fetch the input values from DB.