Assert and Continue

6 posts, 1 answers
  1. Naveen
    Naveen avatar
    4 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2011 Link to this post

    If i have a scenario to test whether 2 elements on the page are visible

    Assert.IsTrue(Element1.IsVisible());
    Assert.IsTrue(Element2.IsVisible());


    In my test method if first Assert fails, i do not want to stop the execution, is there a mechanism where in i can continue the execution of second assert statement??

  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 19 Oct 2011 Link to this post

    Hello Naveen,
       you can simply put the assert statement inside of try catch block and then paste the exception message into the log like so:
    try {
    Assert.IsTrue(Element1.IsVisible());
    } catch (Exception e)    {
    Log.WriteLine(e.Message);
    }

    Assert.IsTrue(Element2.IsVisible());

     
    Greetings,
    Stoich
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Answer
    Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 19 Oct 2011 Link to this post

    Hello Naveen,
         one our engineers pointed something out. The approach in my previous post has the drawback that test will appear to have passed even if the assert does fail. If this is a problem for you and you want the test to fail (but not stop) if one or both Asserts fail, you can try a different approach:
    if (!Element1.IsVisible())
               {
                   Log.WriteLine("Error: Element 1 is not visible");
               }
               if (!Element2.IsVisible())
               {
                   Log.WriteLine("Error: Element 2 is not visible");
               }
     
               Assert.IsTrue(Element1.IsVisible() && Element2.IsVisible());


    Greetings,
    Stoich
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Gaurav
    Gaurav avatar
    3 posts
    Member since:
    Aug 2013

    Posted 19 Mar 2014 Link to this post

    Hi Stoich,

    In your above example, if I use logical statement then tell me is there any need to write the Assert method?. Because we have already verify the element in logical statement.
    if (!Element2.IsVisible())           
    {               
    Log.WriteLine("Error: Element 2 is not visible");          
    }
    here you have already checked, if element is not visible then write the following line on code.
    Then what is the need to write Assert method here.




  5. Matt
    Matt avatar
    19 posts
    Member since:
    Dec 2013

    Posted 21 Mar 2014 in reply to Naveen Link to this post

    Naveen said:If i have a scenario to test whether 2 elements on the page are visible

    Assert.IsTrue(Element1.IsVisible());
    Assert.IsTrue(Element2.IsVisible());


    In my test method if first Assert fails, i do not want to stop the execution, is there a mechanism where in i can continue the execution of second assert statement??


    Since an Assertion is just a boolean check you can do the following:

    bool Element1IsVisible = Element1.IsVisible();
    bool Element2IsVisible = Element2.IsVisible();
    Assert.IsTrue(Element1IsVisible && Element2IsVisible, String.Format("At least one element was not visible. Element1: {0} Element2: {1}", Element1IsVisible, Element2IsVisible));

    What this does is have you perform the boolean checks before hand, and make sure both are visible during the assertion. The String.Format is used to provide the Assertion Error that you expect, and will tell you if both are False or if only one is False. That way you don't have to do a try catch or check the log file for a "passed" unit test.


  6. Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 24 Mar 2014 Link to this post

    @ Gaurav - You can use Assert statement if you want to fail the test, if the condition inside the Assert statement is not met. If you don't want to fail the test, you can use code that is similar to the code sample that you've showed us. 

    @Matt - Thank you for your answer and for helping others!

    Regards,
    Velin Koychev
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top