A
A
Alexey Nazarenko2019-04-01 13:48:37
C++ / C#
Alexey Nazarenko, 2019-04-01 13:48:37

How to implement TestResult check in unit tests?

Good time to all readers!
My question is about test automation: how can I implement a TestResult check after the test method completes?
For example, there is a code:

[TestClass]
public class Class1
{
    [TestMethod]
    public void Method 1()
    {
        MethodA();
        MethodB();
        Method C();
    }

    [TestCleanup]
    public void AfterTest();
    {
        MethodA();
        MethodB();
    }
}

I need to analyze the result of the test method - passed or failed - and, depending on the result, call one or another cleanup test method.
Help, please, to understand.
Framework used: MSTest

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nazarenko, 2019-05-12
@Cunctator

Found a solution using the TestContext.CurrentTestOutcome property.

[TestClass]
public class UnitTest
{
    private TestContext TestContext { get; set; }

    [TestCleanup]
    public void TestCleanup()
    {
        if (TestContext.CurrentTestOutcome == UnitTestOutcome.Passed)
            //do something
    } 

    [TestMethod]
    public void TestMethod()
    {
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question