In the teardown event during an NUnit test how can I get to the attribute applied to the method that was just tested?

前端 未结 4 377
心在旅途
心在旅途 2021-01-20 06:18

I have a test method that is run. When the method generates an exception I want to know what the name of the test was and the exception content.

In the teardown for

4条回答
  •  野性不改
    2021-01-20 07:07

    You can access text context objects in test tear down method

    [TearDown]
    public void TestTearDown()
    {
      // inc. class name
      var fullNameOfTheMethod = NUnit.Framework.TestContext.CurrentContext.Test.FullName; 
      // method name only
      var methodName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
      // the state of the test execution
      var state = NUnit.Framework.TestContext.CurrentContext.Result.State; // TestState enum
    }
    

    I don't know which version was first to support it, but mine is 24.

提交回复
热议问题