Bohdan
Bohdan

Reputation: 133

NUnit TestCaseSource how to set parameters from list

One of my common Class methods generates a list of parameters and I want to set each of them consistently as a parameter to my NUnit test.

I read some documentation about TestCaseSource attribute but can't figure out how to implement it.

For example my method return a list with

"1","2","3","4"

...inside how do I put each of them into my test. Thanks!

Here is my method that return a list:

        public static List<string> TestCombinationsProvider()
    {
        List<string> resultList = new List<string>();
        List<string> parametersList = new List<string>();

        foreach (var item in ReturnDynamicPararmetersEntityProperties())
        {
            parametersList.Add(item.Name);
        }

        for (int i = 0; i < parametersList.Count; i++)
        {
            for (int j = i + 1; j < parametersList.Count; j++)
            {
                resultList.Add(parametersList[i] + " AND " + parametersList[j]);
            }
        }

        return resultList;
    }

And empty Nunit test:

    [Test]
    [TestCaseSource("")]
    public void Test1()
    {

    }

I want to run my test with each row from my list

Upvotes: 1

Views: 3578

Answers (2)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37113

The docs are pretty clear. Instead of returning a List<string>, you need to return a list of testcases, e.g. by directly using IEnumerable<TestCaseData>:

public static IEnumerable<TestCaseData> TestCombinationsProvider()
{
    List<string> resultList = new List<string>();
    List<string> parametersList = new List<string>();

    foreach (var item in ReturnDynamicPararmetersEntityProperties())
    {
        parametersList.Add(item.Name);
    }

    for (int i = 0; i < parametersList.Count; i++)
    {
        for (int j = i + 1; j < parametersList.Count; j++)
        {
            yield return new TestCaseData(parametersList[i] + " AND " + parametersList[j]);
        }
    }
}

There are multiple different signatures for that method, e.g. also returning a collection of object[]. But I won´t recommend them, as you´re losing much of the things you can do with the TestCaseData, e.g. setting each tests Name or also its Description.

Then decorate your actuall test-method:

[Test]
[TestCaseSource(nameof(TestCombinationsProvider))]
public void Test1(string p)
{

}

EDIT: When your TestCombinationsProvider-method is in another class use this instead:

[TestCaseSource(typeof(TheClass), nameof(TheClass.TestCombinationsProvider))]

Upvotes: 0

Support Ukraine
Support Ukraine

Reputation: 1024

TestCaseSourceAttribute constructor has two overloads if you need to set data from another class you should use this constructor.

TestCaseSourceAttribute(Type sourceType, string sourceName);

And it looks like this:

public class TestData {

    public static IEnumerable<string> TestCombinationsProvider()
    {
        var parametersList = ReturnDynamicPararmetersEntityProperties().Select(x => x.Name).ToList();

        for (int i = 0; i < parametersList.Count; i++)
        {
            for (int j = i + 1; j < parametersList.Count; j++)
            {
                yield return  parametersList[i] + " AND " + parametersList[j];
            }
        }
    }
}

public class Tests
{
    [TestCaseSource(typeof(TestData), nameof(TestData.TestCombinationsProvider))]
    public void Test1(string test)
    {
        Assert.Pass();
    }
}

Upvotes: 2

Related Questions