13

I have a large number of test cases, in which several test cases are interdependent. Is it possible that while a later test case is getting executed you can find out the status of a previously executed test case? In my case, the 99th test case depends on the status of some prior test cases and thus, if either the 24th or the 38th fails I would like the 99th test case NOT to get executed at all and thus save me a lot of time. Kindly, explain with some example if possible. Thanks in advance!

2 Answers 2

27

Robot is very extensible, and a feature that was introduced in version 2.8.5 makes it easy to write a keyword that will fail if another test has failed. This feature is the ability for a library to act as a listener. With this, a library can keep track of the pass/fail status of each test. With that knowledge, you can create a keyword that fails immediately if some other test fails.

The basic idea is, cache the pass/fail status as each test finishes (via the special _end_test method). Then, use this value to determine whether to fail immediately or not.

Here's an example of how to use such a keyword:

*** Settings ***
Library   /path/to/DependencyLibrary.py

*** Test Cases ***
Example of a failing test
    fail  this test has failed

Example of a dependent test
    [Setup] | Require test case | Example of a failing test
    log | hello, world

Here is the library definition:

from robot.libraries.BuiltIn import BuiltIn

class DependencyLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.test_status = {}

    def require_test_case(self, name):
        key = name.lower()
        if (key not in self.test_status):
            BuiltIn().fail("required test case can't be found: '%s'" % name)
            
        if (self.test_status[key] != "PASS"):
            BuiltIn().fail("required test case failed: '%s'" % name)

        return True

    def _end_test(self, name, attrs):
        self.test_status[name.lower()] = attrs["status"]
Sign up to request clarification or add additional context in comments.

10 Comments

The above has to be marked answered. This is really helpful. @Bryan: I have a question on this. Instead of FAIL, I want to mark it as Not Executed. Is that possible?
@Vimal: no, it is not possible.
@Vimal, you can only have PASS or FAIL status, but you can turn the test non-critical by using --critical or --noncritical command line options and changing tags accordingly when failing the tests. BuiltIn.fail used above allows setting tags, but you can also use set_tags or remove_tags methods.
@BryanOakley - Awesome solution, I use it in my test's suites.
@MarkHu: you must be doing something wrong, because attrs should not be a TestCase object. Maybe you're using Listener API version 3. This example is clearly using version 2.
|
8

To solve this problem I'm using something like this:

Run Keyword if  '${PREV TEST STATUS}'=='PASSED'  myKeyword

so maybe this will be usable also for you.

1 Comment

I am using the same as a Test Setup in my suite. I call a keyword in my test setup which checks if the previous test failed. If it has, it will skip all cases until it reaches a case that has tag marked as independent. I check for this tag in my Test Setup keyword and execute the independent test case. Works brilliantly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.