Skip to content

Support programmatic modifications of test data and results as part of normal execution #1976

Closed

Description

@pekkaklarck

There are many command line options to alter the parsed test suite structure before executing it. You can, for example, use --name and --doc to set new name and documentation, respectively, or use --test, --suite, --include and --exclude to select which tests to execute. There are, however, sometimes needs for custom modifications.

For example, a team at Nokia recently wanted to way to run only every Xth test in one test run. This isn't supported using any command line options, but programmatically Robot's public APIs make the basic implementation pretty simple:

import sys
from robot.model import SuiteVisitor
from robot.running import TestSuiteBuilder

class RemoveEveryXthTest(SuiteVisitor):

    def __init__(self, x):
        self.x = int(x)

    def start_suite(self, suite):
        suite.tests = list(suite.tests)[::self.x]

if __name__ == '__main__':
    path, x = sys.argv[1:]
    remover = RemoveEveryXthTest(x)
    suite = TestSuiteBuilder().build(path)
    suite.visit(remover)
    suite.run()

A problem with the above solution is that it only runs tests and doesn't create logs and reports, but that would be pretty easy to implement too. A much bigger is that this script doesn't take same arguments as pybot does, and handling all same arguments would be a really big task.

A pretty simple but generic and powerful solution would be allowing registering visitors like RemoveEveryXthTest above to be used after parsing tests but before running them. This could work similarly as when registering listeners that get notifications during test execution:

pybot --visitor RemoveEveryXthTest:2 tests.robot

The :2 part of the the visitor name would specify arguments passed to the visitors __init__ same way listeners accept them.

Activity

  1. added this to the 2.9 milestone on Apr 23, 2015
  2. pekkaklarck commented on Apr 23, 2015

    @pekkaklarck
    MemberAuthor

    Things to consider:

  3. boakley commented on May 11, 2015

    @boakley

    Would this function run exactly once before the tests start running?

    Instead, what about making it possible to define a function to be called immediately before running each test? That function could return True or False, and if False, that test is skipped? That would let you do everything described in this feature, plus allow dynamic skipping of tests.

  4. pekkaklarck commented on May 11, 2015

    @pekkaklarck
    MemberAuthor

    Yes, this is executed once before execution. There are also other use cases than dropping certain tests.

    I think modifications during execution are better done using the listener API. It obviously needed to be enhanced somehow to make dropping tests possible.

  5. just-be-dev commented on May 24, 2015

    @just-be-dev
    Contributor

    So, using this interface could you programmatically build suites/tests? That would be extremely cool and something I've wanted to be able to do for a while.

    Also, skipping tests/changing the results of a test should definitely be doable from the listener interface.

  6. pekkaklarck commented on May 24, 2015

    @pekkaklarck
    MemberAuthor

    You can build tests already now, but then you need to also take care of running tests, creating logs, etc. This API is planned to allow pre-run modifications so that execution etc. would be like normally.

  7. just-be-dev commented on May 24, 2015

    @just-be-dev
    Contributor

    Right, that's more what I mean. I know I can build them, but it's very cumbersome to add them in to normal test execution.

  8. pekkaklarck commented on May 28, 2015

    @pekkaklarck
    MemberAuthor

    What should the command line option to enable this be called? --visitor I used in earlier examples is too generic. My best idea so far is --prerunvisitor or --previsitor.

  9. boakley commented on May 28, 2015

    @boakley

    It's going to be used to initialize the test run, so something along the
    lines of "--init" or "--initfunc" or "--initializer" makes sense to me.

    On Thu, May 28, 2015 at 4:15 PM, Pekka Klärck [email protected]
    wrote:

    What should the command line option to enable this be called? --visitor I
    used in earlier examples is too generic. My best idea so far is
    --prerunvisitor or --previsitor.

    —
    Reply to this email directly or view it on GitHub
    #1976 (comment)
    .

  10. pekkaklarck commented on May 29, 2015

    @pekkaklarck
    MemberAuthor

    I think --init is a bit too generic. The current implementation uses --prerunvisitor, but that visitor part doesn't really tell much. Perhaps something like --prerunmodifier would be better. It's pretty long, but options can be shortened (e.g. --prerun) when used as long as they are unique.

    I've also been thinking should we add similar --postrunmodifier to allow modifying results before logs and reports are created. It could also be used with Rebot, so perhaps it should be --prerebotmodifier in both sides.

  11. just-be-dev commented on May 29, 2015

    @just-be-dev
    Contributor

    Sounds good to me.

  12. 4 remaining items

  13. self-assigned this
    on May 31, 2015
  14. added a commit that references this issue on Jun 1, 2015
  15. changed the title [-]Support custom modifications of test data and results[/-] [+]Support programmatic modifications of test data and results as part of normal run[/+] on Jun 3, 2015
  16. pekkaklarck commented on Jun 3, 2015

    @pekkaklarck
    MemberAuthor

    Here's an example of a modifier that selects only every Xth test. It would work both with --prerunmodifier and --prerebotmodifier.

    from robot.model import SuiteVisitor
    
    
    class RunEveryXthTest(SuiteVisitor):
    
        def __init__(self, run_every_xth, start_from=0):
            self.run_every_xth = int(run_every_xth)
            self.start_from = int(start_from)
    
        def start_suite(self, suite):
            suite.tests = suite.tests[self.start_from::self.run_every_xth]
    
        def end_suite(self, suite):
            suite.suites = [child for child in suite.suites
                            if child.test_count > 0]
  17. changed the title [-]Support programmatic modifications of test data and results as part of normal run[/-] [+]Support programmatic modifications of test data and results as part of normal execution[/+] on Jun 3, 2015
  18. pekkaklarck commented on Jun 4, 2015

    @pekkaklarck
    MemberAuthor

    This issue is now ready for review. Especially new docs committed in ce4d5fa (visitor API docs), b26a664 (User Guide) and 54ad854 (--help) should be proof-read.

    Review by anyone is more than appreciated, but I leave it for @jussimalinen to close this issue when he's reviewed the changes.

  19. jussimalinen commented on Jun 5, 2015

    @jussimalinen
    Member

    Looks good.

  20. davekipfer commented on Jul 31, 2015

    @davekipfer

    Could you please clarify what this change currently supports? You say at one point that this occurs once before exceution, but then you mention the Listener interface needs to be enhanced. Does that mean that a keyword/test/suite listener can now make modifications on a listener event, or is that something tied to a different enhancement, or something not yet captured?

  21. pekkaklarck commented on Jul 31, 2015

    @pekkaklarck
    MemberAuthor

    This functionality is documented in the User Guide:
    http://robotframework.org/robotframework/2.9/RobotFrameworkUserGuide.html#programmatic-modification-of-test-data

    Could you @davekipfer take a look at it and see is it adequate? If it isn't, please submit a new issue and tell what you don't understand and we'll enhance docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions