dayzz
dayzz

Reputation: 13

How to stop execution test if 2 or more tests failed?

Hello i have so issue when i try to stop test if 2 or more test failed, I'm using listener lib for this, I'm checking status, and when status "FAIL" add to the counter, when counter == 2, tests must stop, but it doesn't.

from robot.libraries.BuiltIn import BuiltIn

class PythonListener(object):
    ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
    ROBOT_LISTENER_API_VERSION = 2

def __init__(self):
    self.ROBOT_LIBRARY_LISTENER = self

def end_test(self, name, attrs):
    global result
    global count
    count = 0
    result = None
    print 'Suite %s (%s) start %s.' % (name, attrs['longname'], attrs['status'])
    if attrs['status'] == "FAIL":
        count += 1
        if count >= 2:
            result = BuiltIn.fatal_error(self)
            return result

Upvotes: 1

Views: 523

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385880

There are several things wrong with your code. However, even when you fix the syntax issues this solution won't work. A listener can't cause a test to fail. A listener is purely an observer, it can't run keywords. You can call sys.exit() but that will immediately terminate the test without generating a report.

What you will need to do is implement a keyword that every test case calls, which can examine the count variable defined by the listener. You could then call this keyword in every test setup or teardown.

An example of implementing a keyword and a listener in the same module can be seen in this answer: https://stackoverflow.com/a/28508009/7432

As to the problems in your code:

First, the indentation of the methods __init__ and __end_test__ is incorrect, causing those functions to be global functions rather than methods of the class.

Second, your end_test method resets the count to zero every time it is called. You need to make count an object attribute.

Third, you are calling fatal_error incorrectly. Change this:

result = BuiltIn.fatal_error(self)

... to this (note the parenthesis on BuiltIn()):

result = BuiltIn().fatal_error("failed due to failure >= 2")

But like I said earlier, you can call this keyword within the listener but it won't affect your test run.

Upvotes: 1

Related Questions