Locked How to make the test clean up method runs only when the test failed?

  • Tuesday, November 01, 2011 5:02 PM
     
     

    Hi,

     

    I have a test cleanup method for every coded ui test which  is running after every test run. How to make the test cleanup runs only when the test failed. Appreciate your help..

    Thanks,

    Suneetha

     

     

All Replies

  • Tuesday, November 01, 2011 9:06 PM
     
     Answered Has Code

    You can put this in your cleanup method:

     

    if (TestContext.CurrentTestOutcome.ToString() == "Failed")
    {
             //Do Something
    }
    


     

  • Wednesday, November 02, 2011 4:45 PM
     
     

    Thank you.

    -Suneetha.

  • Thursday, November 03, 2011 9:40 PM
     
     Answered Has Code

    Here is the TestCleanup method i used for this

            [TestCleanup()]
            public void MyTestCleanup()
            {
                // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
                // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
     
                switch (this.TestContext.CurrentTestOutcome)
                {
                    case  UnitTestOutcome.Failed:
                        {
                            this.TestContext.WriteLine("Browser was closed by the MyTestCleanup() method due to a failure on the test!");
                            this.UIMap.Action_CloseIE9();
                            break;                    
                        }
                }
            }

     

    NOTE UnitTestOutCome object has multiple values to check, hence i have it in a c# swtich statement so that i can edit it with ease in the future