C# NUnit TestCaseSource Passing Parameter

前端 未结 3 1661
予麋鹿
予麋鹿 2021-02-04 06:58

I have the following method which generates a set of test cases!

public IEnumerable PrepareTestCases(param1)
{
    foreach (string e         


        
3条回答
  •  再見小時候
    2021-02-04 07:27

    If you look at TestCaseSourceAttribute doc you will see there is no any way to pass the parameter to the method which returns test cases.

    The method, which generates test cases, should be parameterless.

    So, assuming you are going to avoid code duplication and you need to reuse the same method to generate a few test case lists, I'd recommend you to do the following:

    1. Write parameterized method which actually generates test cases sets:
      (PrepareTestCases() already does that)

      public IEnumerable PrepareTestCases(string param)
      {
          foreach (string entry in entries)
          {
              yield return CallMyMethod(param);
          }
      }
      
    2. Write parameterless wrappers which call test cases generator and pass desired parameter there:

      public IEnumerable PrepareTestCases_Param1()
      {
          return PrepareTestCases("param1");
      }
      
      public IEnumerable PrepareTestCases_Param2()
      {
          return PrepareTestCases("param2");
      }
      
    3. Write test methods and pass paremeterless wrappers there as test case sources:

      [TestCaseSource("PrepareTestCases_Param1")]
      public void TestRun1(ResultsOfCallMyMethod data)
      {
      }
      
      [TestCaseSource("PrepareTestCases_Param2")]
      public void TestRun2(ResultsOfCallMyMethod data)
      {
      }
      

提交回复
热议问题