title: Is it possible to get the NUnit TestContext MethodName from within an IEnumerable method?
body: <p>I am using NUnits <code>IEnumerable</code> to try and return test data by method name in a Nunit <code>SetUpFixture</code> test base class (see bottom of class):</p>
  <pre><code>    [SetUpFixture]
      public class TestBase
      {
          private readonly BrowserFactory _factory = new BrowserFactory();
          protected IWebDriver Driver;
  
          private static ExtentReports _extent;
          private static string _projectPath;
          protected ExtentTest _test;
          private static TestContext testContext;
    
          [OneTimeSetUp]
          public void OneTimeSetUp()
          {
              InitializeReport();
              _test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
              Driver = _factory.Init(Config.Browser);
              Driver.Manage().Window.Maximize();
              Driver.Manage().Cookies.DeleteAllCookies();
          }
  
          [TearDown]
          public void TearDown()
          {
              var status = TestContext.CurrentContext.Result.Outcome.Status;
              string testClassName = TestContext.CurrentContext.Test.Name;
              string testMethodName = TestContext.CurrentContext.Test.MethodName;
              var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace) ? &quot;&quot; : string.Format(&quot;{0}&quot;, TestContext.CurrentContext.Result.StackTrace);
              Status logstatus;
  
              switch (status)
              {
                  case TestStatus.Failed:
                      logstatus = Status.Fail;
                      Console.WriteLine(&quot;Test status is Fail&quot;);
                      DateTime time = DateTime.Now;
                      String fileName = &quot;Screenshot_&quot; + time.ToString(&quot;h_mm_ss&quot;) + &quot;.png&quot;;
                      String screenShotPath = Capture(Driver, fileName);
                      _test.Log(Status.Fail, &quot;Fail&quot;);
                      _test.Log(Status.Fail, &quot;Snapshot below: &quot; + _test.AddScreenCaptureFromPath(&quot;Screenshots\\&quot; + fileName));
                      break;
  
                  case TestStatus.Inconclusive:
                      logstatus = Status.Warning;
                      break;
  
                  case TestStatus.Skipped:
                      logstatus = Status.Skip;
                      break;
  
                  default:
                      logstatus = Status.Pass;
                      Console.WriteLine(&quot;Test status is Pass&quot;);
                      break;
              }
  
              _test.Log(logstatus, &quot;Test &quot; + &quot; - &quot; + testMethodName + &quot; Ended with a status of &quot; + logstatus + stacktrace);
          }
  
          [OneTimeTearDown]
          public void OneTimeTearDown()
          {
              CloseBrowser();     
              _extent.Flush();
          }
  
          public void CloseBrowser()
          {
              Driver?.Close();
              Driver?.Quit();
              Driver?.Dispose();
          }
  
          public static IEnumerable&lt;JObject&gt; GetTestData()
          {
              string testMethodName = TestContext.CurrentContext.Test.MethodName;
  
              JObject[] testDataReturned = GetDataByMethod(testMethodName);
  
              for (int i = 0; i &lt; testDataReturned.Length; i++)
              {
                  yield return testDataReturned[i];
              }
          }
  
          public static JObject[] GetDataByMethod(string testMethodName)
          {
              return JsonUtils.FetchData(testMethodName);
          }
      }
  }
  </code></pre>
  <p>My issue is that when I run this code, the string <code>testMethodName</code> within the <code>IEnumerable</code> method <code>GetTestData()</code> has a value of <code>AdhocTestMethod</code>, instead of the actual test method string <code>LoginTest_E2E</code>.</p>
  <p>I am able to fetch TestContext data such as MethodName just fine within my <code>OneTimeSetUp()</code> and <code>TearDown()</code> (see above), but cannot get the method name returned within the <code>IEnumerable</code>.</p>
  <p>The test case I am using the <code>IEnumerable</code> as follows:</p>
  <pre><code>        [Test, TestCaseSource(&quot;GetTestData&quot;)]
          public void LoginTest_E2E(JObject testData)
          {  
              string loginId = testData[&quot;loginId&quot;].Value&lt;string&gt;();
              string password = testData[&quot;password&quot;].Value&lt;string&gt;();
              string useCase = testData[&quot;useCase&quot;].Value&lt;string&gt;();  
          }
  </code></pre>
  <p>For reference, the .json data file I am using in the test is:</p>
  <pre><code>{
    &quot;LoginTest_E2E&quot;: [
      {
        &quot;loginId&quot;: &quot;testuser1&quot;,
        &quot;password&quot;: &quot;password1&quot;,
        &quot;useCase&quot;: &quot;verify for user 1&quot;
      },
      {
        &quot;loginId&quot;: &quot;tesetuser2&quot;,
        &quot;password&quot;: &quot;password2&quot;,
        &quot;useCase&quot;: &quot;verify for user 2&quot;
      }
    ]
  </code></pre>
  <p>Does anyone know why I am unable to get the string value for <code>testMethodName</code> within the <code>IEnumerable</code> method?</p>
  <p>Here is the method in question again:</p>
  <pre><code>public static IEnumerable&lt;JObject&gt; GetTestData()
  {
      string testMethodName = TestContext.CurrentContext.Test.MethodName;
  
      JObject[] testDataReturned = GetDataByMethod(testMethodName);
  
      for (int i = 0; i &lt; testDataReturned.Length; i++)
      {
          yield return testDataReturned[i];
      }
  }
  </code></pre>