Marko Pareigis
Marko Pareigis

Reputation: 43

Get status and name of unit test inside of TestCleanup

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?

Upvotes: 1

Views: 248

Answers (1)

forsvarir
forsvarir

Reputation: 10859

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

Upvotes: 1

Related Questions