Skip to content

Refactor execution and result side model objects #3749

Closed

Description

@pekkaklarck

The TestSuite structures we use on the execution and result sides (robot.running.model and robot.result.model, respectively, with a common base classes in robot.model) have certain things that need to be changed:

  • Remove keywords and messages from execution side Keyword objects. These keywords never actually have child keywords or messages, they just represent the keyword call with the keyword name and arguments. Basically this means moving the related code from the common base class to the result side model objects.

  • TestSuite objects should not have keywords attribute but instead should have setup and teardown directly. Suites having keywords is odd in general and being able to use e.g. suite.setup instead of suite.keywords.setup is convenient.

  • keywords attribute used by TestCase, For, If (new in RF 4.0) and UserKeyword objects should be renamed to body. The body can contain FOR loops and soon also IF/ELSE in addition to keywords so body is a much better name than keywords and that's also the term parsing side model uses. The forthcoming IfElse objects should naturally have body, not keywords, as well.

  • Add setup and teadown directly to TestCase and UserKeyword objects. Accessing test.setup is more convenient than the current test.keywords.setup and test.body.setup would be just wrong.

  • Make setup and teardown objects, not None, also when they are not defined. This makes it easier for external code like pre-run modifiers to modify them. Their initial truth value should be false to make it possible to use code like if test.setup:.

This kind of changes are naturally backwards incompatible but in my opinion better model is worth that. We should, however, preserve keywords as a deprecated read-only property that is generated based on body and setup/teardown when accessed.


[Update on 2021-01-18] Everything discussed above has now been done, but doing these changes has uncovered other highly related problems in the model that also need to be changed:

  • On the execution side make For and If independent classes instead of extending Keyword. They don't need keyword related attributes like name or args, but instead they should have context specific attributes like For.flavor and If.condition. Earlier those context specific attributes were implemented as properties but they should be normal attributes. Old keyword specific attributes should probably be preserved as deprecated properties for backwards compatibility.

  • Add separate visit/start/end_for and visit/start/end_if methods to the visitor interface. Earlier For and If were visited using visit/start/end_keyword but that doesn't work anymore when they don't extend Keyword.

  • Add separate For and If classes on result side. Currently FOR and IF construct are reported as keywords with type set accordingly. This makes inspecting FOR/IF hard because their context specific information is not directly available and need to be parsed from keyword attributes. For example, all FOR information is in keyword name in format like ${x} IN [ a | b | c ]. This change requires some changes to output.xml as well and I added a separate comment about that.

  • On the result side move keywords' messages to body to preserve their relative order with keywords and FOR/IF constructs. This makes it easier to preserve their order also in log (Relative order of messages and keywords is not preserved in log #2086). The old messages attribute should be preserved as a property to make accessing only messages easy.

Activity

  1. added this to the v4.0 milestone on Oct 30, 2020
  2. pekkaklarck commented on Oct 30, 2020

    @pekkaklarck
    MemberAuthor
  3. pekkaklarck commented on Nov 26, 2020

    @pekkaklarck
    MemberAuthor

    Important things to decide:

    • Should visitor and listener APIs be changed? They both currently have start/end_keyword method which are called also with FOR and IF constructs. Should we add separate start/end_for and start/end_if methods? If we do, they could call start/end_keyword by default to avoid backwards incompatibly problems.

    • Should we change <keywords> to <body> also in output.xml? It would make this change even more backwards incompatible, but output.xml being inconsistent with internal model would also be a smallish problem. If we would decide to change output.xml format, we need to submit a separate issue about it and enhance output.xml also otherwise. For example, attributes currently using values yes or no should be changed to use true or false (i.e. xsd:boolean type).

  4. prescod commented on Dec 12, 2020

    @prescod

    Relevant to your refactor:

    I'm trying to measure and log the execution time of tests with Setup and Teardown subtracted. I'm having a tough time using the current Listener V3 Results object to do so.

    Given this test:

    Test Perf Measure
        [Teardown]      Sleep   0.5
        [Setup]         Sleep   0.5
        [Tags]  perf
        Log       Noop
    

    And this code:

        def end_test(self, data, result):
            print(type(result), len(result.keywords))
            print(type(data), len(data.keywords))
    

    I get:

    <class 'robot.result.model.TestCase'> 0
    <class 'robot.running.model.TestCase'> 3
    

    So the result model thinks I have 0 keywords and the running model thinks I have 3.

    But the running model has no start-time or end-time. So I have not figured out how to get a hold of the elapsed time for the Setup and Teardown in the end_test listener hook.

  5. 20 remaining items

  6. boakley commented on Feb 2, 2021

    @boakley

    Having an <if> inside an <if>, and having <if> both represent an "if" and "else" seems very odd. Why not a single tag such as <block> or <compound> which could be used for all statements that start a block of statements? For example, <block type='for'>, <block type='if'>, <block type='else'>, etc? Then, if robot ever gets a "with" or "while" or "try" statement, we don't have to invent more tags.

  7. pekkaklarck commented on Feb 3, 2021

    @pekkaklarck
    MemberAuthor

    A problem with a generic element like <block> is that IF and FOR need to store different information. This means that depending on the usage <block> would have different attributes and/or child elements. This makes, for example, creating a schema harder.

  8. pekkaklarck commented on Feb 11, 2021

    @pekkaklarck
    MemberAuthor

    Some more work to be done still:

    • Change IF/ELSE structure to use If object with all its branches in body and not the next ELSE IF or ELSE in orelse. Also change output.xml accordingly. This was discussed in the comment above.
    • Better type constant names and values.
    • Consistent naming of internal classes, visitor methods, etc.
    • Store FOR loop iteration information in output.xml so that it can be fully recovered.
  9. pekkaklarck commented on Feb 11, 2021

    @pekkaklarck
    MemberAuthor

    IF/ELSE model model and type constants were enhanced by the comments linked above. Class, method and attribute names were enhanced in 00c282f

  10. pekkaklarck commented on Feb 12, 2021

    @pekkaklarck
    MemberAuthor

    Everything listed above now done. Closing this again.

  11. pekkaklarck commented on Mar 3, 2021

    @pekkaklarck
    MemberAuthor

    As #3868 demonstrates, external tools using test.keywords.create() need to be updated because keywords is nowadays only a read-only property. The new way to write that is test.body.create_keyword(). This code is compatible both with the old and new model:

    if hasattr(tc, 'body'):    # RF >= 4.0
        create_keyword = tc.body.create_keyword
    else:    # RF < 4.0
        create_keyword = tc.keywords.create
    create_keyword(name=kwname, args=args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions