1

I'm currently using Microsoft.VisualStudio.TestTools.UnitTesting Attribute <TestCleanup()> in a VB.Net project. Because some of my tests can leave the world a bit messy (after a fail that is) I need to find out wether the test for which a particular cleanup is running failed or not. Also the actual name of the "current" test (the one I'm cleaning up for) would be helpfull.

Is there a way to access this information?

  • Not sure in VB. In C# there's the [CallerMemberName] arg which I add to a util function and return back. See if this is relevant maybe: http://stackoverflow.com/questions/9666562/how-to-get-the-unit-test-method-name-at-runtime-from-within-the-unit-test – user326608 Jul 06 '15 at 16:12

1 Answers1

1

If you don't already have one, then you need to add a TestContext property to your class. This will be populated by the test runner and then you can check it in your cleanup.

The bits you are interested in are TestContext.TestName for the name of the test currently being executed and TestContext.UnitTestOutcome for the success state of the test.

Something like this:

Public Property TestContext As TestContext

<TestCleanup> Public Sub Cleanup()
    If (TestContext.CurrentTestOutcome = UnitTestOutcome.Passed And
        TestContext.TestName = "TestMethod1") Then
        'Do something
    Else
        'Do something else
    End If


End Sub
forsvarir
  • 10,749
  • 6
  • 46
  • 77