Forum Discussion

MrDysprosium's avatar
MrDysprosium
Contributor
8 years ago

How can I cause a test to fail programatically?

In my test, I need to check to see if two values are the same.. simple enough in code, but how to I get TestComplete to fail via Python code?


eample:

if Var1 != Var2:    
    # Do something that cause the test to fail!



  • Define "fail".  Do you want it just to log an error and continue?  Then simply call Log.Error.  Do you want it to throw an exception to get trapped by a try/catch logic?  I'm not familiar with Python but, in JavaScript that would be a call to throw Error('blah').  

     

    Generally, define what you want to call a "failure" (and, perhaps, different "levels" of failure), and we should be able to find a way to get that to happen. :)

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Define "fail".  Do you want it just to log an error and continue?  Then simply call Log.Error.  Do you want it to throw an exception to get trapped by a try/catch logic?  I'm not familiar with Python but, in JavaScript that would be a call to throw Error('blah').  

     

    Generally, define what you want to call a "failure" (and, perhaps, different "levels" of failure), and we should be able to find a way to get that to happen. :)

    • MrDysprosium's avatar
      MrDysprosium
      Contributor

      Good stuff, as always Tristaan.

      In Python it's:


      try:
        //do work
      
      except:
        //handle exception
      
      finally:
        //do different work

      But can I cause TestComplete test to actually *stop* when I log an error? That's a project setting... isn't it?

       

      **Edit** it is. Thank you!

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        It is a setting on the project level.  But, if you're using Test Items (View -> Organize Tests) you can even tell it to be a bit more judicious in that, when there's an error, you can tell it to stop the specific item and not the entire project.

         

        Glad I could help!