Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

nunit

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 the test I want to get access to this information. How would I get access to it from the [TearDown] attributed method?

like image 575
Paul Mendoza Avatar asked Feb 01 '26 17:02

Paul Mendoza


1 Answers

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.

like image 177
Karel Frajták Avatar answered Feb 04 '26 05:02

Karel Frajták



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!