Search code examples
c#.netunit-testingvisual-studio-test-runnertest-class

Conditional behavior in TestCleanUp if test had failed


I have a TestClass. In my TestCleanUp function, I want to do conditional actions depending on wether the test had failed.

I could of course wrap each test with try-catch and add a flag which I can later query, but I was wondering if there is something built-in to the testing framework.


Solution

  • The TestContext.CurrentTestOutcome property contains this information. You can gain access to the current TestContext instance by adding a TestContext property to your test class. e.g.:

    [TestClass]
    public class YourTestClass
    {
        public TestContext TestContext { get; set; }
    
        //...
    }