Tom Smykowski
Tom Smykowski

Reputation: 26129

How to generate tests based on data in nunit framework using C#

So i have this code:

[TestFixture]
[Category("MyTestSet")]
public class MyTests
{
    [Test]
    public void TestCase12()
    {
        ExecuteTestCase(12);
    }

    [Test]
    public void TestCase13()
    {
        ExecuteTestCase(13);
    }

    [Test]
    public void TestCase14()
    {
        ExecuteTestCase(14);
    }
}

The ExecuteTestCase gets test parameters from my web server and executes the test case with these settings.

Each time i add a new test case parameters on my web server i need to add a new test in my C# code and pass the ID of test case parameters i have in my web server database and compile my code.

Is there any way to do it automatically? Like say, C# gets from my server ID's of all test case parameters and creates tests for them on the fly?

What is important, test cases change frequently. I was thinking about running all test cases in one test case on a loop, but than i'd be unable to run my test cases separately for example in Nunit IDE.

So my question is: how to run multiple test cases depending on data i receive on run time.

Upvotes: 1

Views: 959

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 38046

You can use TestCaseSourceattribute in order to get parameters from web service and have your test cases auto generated

[TestFixture]
[Category("MyTestSet")]
public class MyTests
{
    [Test, TestCaseSource(nameof(GetTestParameters))]
    public void TestCase(int parameter)
    {
        ExecuteTestCase(parameter);
    }

    static int[] GetTestParameters()
    {
        //call web service and get parameters
        return new[] { 1, 2, 3 };
    }
}

documentation

Upvotes: 1

Related Questions