locked
Coded UI test exception handling RRS feed

  • Question

  • Hi,

    I am wrtiting an assert method and when this assertion fails I want to handle the exception in Try ctach block and also fail the assertion. So I am using Assert.fail method in the catch block. But I get an error message "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException was unhandled by user code"

    I need information on how to use assert.fail method in try catch block. I have seen posts where it has been suggested to use assert.fail method but nothing is mentioned on how to use it.

    Regards

    Rajib

     

    Monday, February 21, 2011 11:53 AM

Answers

  • Assert.Fail is always going to throw an exception.  If you really need to catch it for some reason, put the check in another try catch block (using Vishn's example)

     

    try

    {

     try
    
    {
    Assert.IsTrue(false , "Fail while asserting" );
    }
    catch (AssertFailedException)
    {
    Assert.Fail("Assert from Catch Block" );
    }

    }

    catch

    {

    //  Whatever you need to do on a failure

    }

     

    This will catch the Assert.Fail but your test will always pass.  I always wrapper my test steps in a try/catch block so I can gracefully exit on an unexpected failure.

    • Marked as answer by Rajib Barua Wednesday, June 15, 2011 10:58 AM
    Thursday, March 17, 2011 2:07 PM
  • It works. Thank you Sir.
    • Marked as answer by Rajib Barua Wednesday, June 15, 2011 10:59 AM
    Wednesday, June 15, 2011 10:58 AM

All replies

  • I think you are looking for something like below.

           try
          {
            Assert.IsTrue(false, "Fail while asserting");
          }
          catch (AssertFailedException)
          {
            Assert.Fail("Assert from Catch Block");
          }
    

     


    Please mark this post as answer if this answers your question
    Monday, February 21, 2011 12:54 PM
    Moderator
  • HI Vishnu,

    When I execute this code, i get an error as "AssertFailedException was unhandled by usercode". when the execution reaches the catch block. I wish I could attach a screenshot, but I have put the error I receive in the block below.

    Regards

    Rajib

     Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException was unhandled by user code
     Message=Assert.Fail failed. Assert from Catch Block
     Source=Microsoft.VisualStudio.QualityTools.UnitTestFramework
     StackTrace:
        at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.HandleFail(String assertionName, String message, Object[] parameters)
        at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail(String message)
        at N_EventProgramming.Events.EventAction(CommonVariables objCommVar) in C:\Rajib\Project Work\Asset\VSTS\Automation\Test Scripts\ExcelProgramming\Events.cs:line 88
        at N_DriverScripts.DriverScripts.ReadTestCase(CommonVariables objCommVar, CommonFunctions objCommFunc, String strWsheet) in C:\Rajib\Project Work\Asset\VSTS\Automation\Test Scripts\ExcelProgramming\Driver.cs:line 147
        at N_DriverScripts.DriverScripts.ReadSettings(CommonVariables objCommVar, CommonFunctions objCommFunc) in C:\Rajib\Project Work\Asset\VSTS\Automation\Test Scripts\ExcelProgramming\Driver.cs:line 61
        at KeywordDriven.CodedUITest1.CodedUITestMethod1() in C:\Rajib\Project Work\Asset\VSTS\Automation\Test Scripts\ExcelProgramming\CodedUITest1.cs:line 61
     InnerException: 
    
    

     

     

    Tuesday, February 22, 2011 6:09 AM
  • I am still waiting for a favourable answer.
    Wednesday, March 16, 2011 7:22 AM
  • Hi,

    Are you executing the test in Debug mode?

    If yes, once try the same in Run Mode.

    Regards,

    Pankaj

    Wednesday, March 16, 2011 11:54 AM
  • Assert.Fail is always going to throw an exception.  If you really need to catch it for some reason, put the check in another try catch block (using Vishn's example)

     

    try

    {

     try
    
    {
    Assert.IsTrue(false , "Fail while asserting" );
    }
    catch (AssertFailedException)
    {
    Assert.Fail("Assert from Catch Block" );
    }

    }

    catch

    {

    //  Whatever you need to do on a failure

    }

     

    This will catch the Assert.Fail but your test will always pass.  I always wrapper my test steps in a try/catch block so I can gracefully exit on an unexpected failure.

    • Marked as answer by Rajib Barua Wednesday, June 15, 2011 10:58 AM
    Thursday, March 17, 2011 2:07 PM
  • It works. Thank you Sir.
    • Marked as answer by Rajib Barua Wednesday, June 15, 2011 10:59 AM
    Wednesday, June 15, 2011 10:58 AM