Nissa Armelon
Nissa Armelon

Reputation: 111

How to have dynamic Nunit TestCaseSource in C#?

What I'd like to do is exactly the first example from this page but...

http://nunit.org/index.php?p=testCaseSource&r=2.5

with value that I can change

    static object[] DivideCases =
    {
        for (int i = 0; i < qtyCmd(); i++)
        {
            new object[] { getCmd[i] },
        }
    };

qtyCmd is just a static method which return a number getCmd read a line (index sent as parameter) in a text file

where the arrays command. I know about Data-Driven Unit Test, but I was asked to do not use it. To be more specific, I am asked to do so with [TestCase]

Upvotes: 2

Views: 3944

Answers (1)

Thiago S&#225;
Thiago S&#225;

Reputation: 852

You can turn DivideCases into a method:

private object[] DivideCases() {
    var amountOfSamples = qtyCmd();
    var result = new object[amountOfSamples];

    for (var i = 0; i < amountOfSamples; i++) {
        result[i] = new object[] {getCmd[i]};
    }

    return result;
}

And then use it with TestCaseSource:

[Test, TestCaseSource("DivideCases")]
public void TestMethod(object[] samples) {
    // Your test here.
}

Upvotes: 3

Related Questions