Thursday, April 12, 2012

Microsoft Unit Testing Framework - Using Test Context to determine test result

When creating your test automation, and you need to run a procedure when the test did not pass. Use the TestContext.CurrentTestOutcome property. This property will determine the result of the test in the Microsoft Unit Testing Framework.

In this following example I wanted to capture a screen shot only if the result had failed.



[TestCleanup]
public void CleanUpTest()
{
if(TestContext.CurrentTestOutcome!=UnitTestOutcome.Passed)
{
Tool.GetScreenShot();
Tool.SaveScreenShot();

}
 
}


The TestContext.CurrentTestOutcome property will reference a UnitTestOutcome enumerator. In the above example, if the test hadn't passed, then on clean up I will take a screen shot, and then save all screen shots to a result file. .

No comments:

Post a Comment