How to execute parameterized testcase if I dont want to specify the parameters using TestCase Attribute?

61 views Asked by At

How to execute the below code from the Nunit Console if I doesnt know the Parameters which will be passed during execution.

    [TestCase]
    public void ExecuteString(string someValue)
    {
        Console.WriteLine(someValue);
    }

I know that we should pass the parameters in this format [TestCase("Values")]. But If I'm not sure about what the parameters will be ?

1

There are 1 answers

0
Steve On

You can not use the TestCase attribute this way, but there is the TestCaseSource attribute that can work with variables and runtime values.

It works as shown below. Might this be what you are looking for?

[Test, TestCaseSource(typeof(string), nameof(SomeClass.someCases))]
public void Test(string someValue)
{
   Console.WriteLine(someValue);
}

private class someClass
{
   public static IEnumerable someCases
   {
       get
       {
          yield return
             new TestCaseData("valuefornow");
          yield return
             new TestCaseData("valueforlater");
       }
    }
}