0

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 ?

  • Why are you not sure what the parameters will be? You are the programmer, you define them in your test case. If you don't know them, what use is running code that uses them? – oerkelens Aug 29 '17 at 07:10
  • Parameters are dynamic. So. I should be able to execute them without knowing it. I will be just knowing the type of parameters which is string type in the above code. – Mahesh Bhagwat Aug 29 '17 at 07:15
  • I wonder if you have the same idea of the concept of a unit test that most people have. If your code should handle different types, set up test cases with different types. A unit test uses fixed values to see if your code gives you the expected result. Without determining the input beforehand, you cannot predict the output, and thus, you cannot test. (Yes, in some cases one might want to use random values, but not in general) – oerkelens Aug 29 '17 at 07:19
  • Maybe your question will be more clear if you add some more actual code. I doubt you want to unit test `Console.Writeline`. – oerkelens Aug 29 '17 at 07:20
  • That will be a big answer if I try to Answer that. But is it possible to call the parameterized testcase from Nunit console without specifying the probable parameters using the TestCaseAttribute ? – Mahesh Bhagwat Aug 29 '17 at 07:29
  • @MaheshBhagwat: Well where do you want the parameter values to come from? Typically they're either specified inline or via TestCaseSource. They don't just come out of thin air. Please clarify what you're trying to achieve. If you're asking whether you can specify the parameters *on the command line*, I don't know of any way to do that, but it would be a pretty odd use case IMO. – Jon Skeet Aug 29 '17 at 11:03

1 Answers1

1

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");
       }
    }
}
Steve
  • 367
  • 3
  • 11