In MSTest, how to check if the last test passed (in TestCleanup)

I create web tests in Selenium using MSTest and want to take a screenshot every time the test fails, but I do not want to take it every time the test passes.

I would like to take a screenshot function inside the [TestCleanup] method and run it if the test failed, but the test failed. But how do I know if the last test passed?

I am currently doing bool = false on [TestInitialize] and bool = true if the test passes.

But I do not think this is a very good solution.

So basically, I'm looking for a way to determine if the last test is true / false when executing [TestCleanup] .

+10
selenium webdriver mstest
source share
2 answers

@MartinMussmann's answer is correct but incomplete. To access the "TestContext" object, you need to declare it as a property in your TestClass:

 [TestClass] public class BaseTest { public TestContext TestContext { get; set; } [TestCleanup] public void TestCleanup() { if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed) { // some code } } } 

This is also mentioned in the next post .

+6
source share

Decision

 if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed) { // some code } 
+11
source share

All Articles