Skip to content

Keyword type passed to listeners is wrong then keyword is for loop #2227

Closed

Description

@adam-wypych

As we observed during RED debugger implementation everytime RF during test execution meets keyword, which are :FOR loop, the type attribute in start_keyword attributes dictionary/map is set to Test For and the same is done when loop keyword is end. For corresponding items inside loop the type is set to Test Foritem. We are strongly base on this contract for present user, which line is currently executed by RF. This works completly correct, when the loop is made from Library keywords or simple user keywords (here I mean without loop inside).
When we made test case like below (LoopInLoop.robot), where Nested Loop Keyword contains inside also internal :FOR loop we get wrong about type of Nested Loop Keyword in case of end_keyword function from listener API - the type is Keyword instead of Test Foritem. The same problem shows also for next loop item \ Log 'End for: ' ${x}, but for both start_keyword and end_keyword.

Listener output for LoopInLoop.robot execution (see lines: 16-18 and 32-34)

1 {start_keyword, name=BuiltIn.Log, type=Keyword}
2 {end_keyword, name=BuiltIn.Log, type=Keyword}
3 {start_keyword, name=${x} IN RANGE [ 0 | 2 ], type=Test For}
4 {start_keyword, name=${x} = 0, type=Test Foritem}
5 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
6 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
7 {start_keyword, name=Nested Loop Keyword, type=Test Foritem}
8 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
9 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
10 {start_keyword, name=${y} IN RANGE [ 0 | 1 ], type=Test For}
11 {start_keyword, name=${y} = 0, type=Test Foritem}
12 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
13 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
14 {end_keyword, name=${y} = 0, type=Test Foritem}
15 {end_keyword, name=${y} IN RANGE [ 0 | 1 ], type=Test For}
16 {end_keyword, name=Nested Loop Keyword, type=Keyword}
17 {start_keyword, name=BuiltIn.Log, type=Keyword}
18 {end_keyword, name=BuiltIn.Log, type=Keyword}
19 {end_keyword, name=${x} = 0, type=Test Foritem}
20 {start_keyword, name=${x} = 1, type=Test Foritem}
21 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
22 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
23 {start_keyword, name=Nested Loop Keyword, type=Test Foritem}
24 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
25 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
26 {start_keyword, name=${y} IN RANGE [ 0 | 1 ], type=Test For}
27 {start_keyword, name=${y} = 0, type=Test Foritem}
28 {start_keyword, name=BuiltIn.Log, type=Test Foritem}
29 {end_keyword, name=BuiltIn.Log, type=Test Foritem}
30 {end_keyword, name=${y} = 0, type=Test Foritem}
31 {end_keyword, name=${y} IN RANGE [ 0 | 1 ], type=Test For}
32 {end_keyword, name=Nested Loop Keyword, type=Keyword}
33 {start_keyword, name=BuiltIn.Log, type=Keyword}
34 {end_keyword, name=BuiltIn.Log, type=Keyword}
35 {end_keyword, name=${x} = 1, type=Test Foritem}
36 {end_keyword, name=${x} IN RANGE [ 0 | 2 ], type=Test For}
37 {start_keyword, name=BuiltIn.Log, type=Keyword}
38 {end_keyword, name=BuiltIn.Log, type=Keyword}

DATA TO REPRODUCE
LoopInLoop.robot file content:

*** Test Cases ***
ForTest
    Log     start
    :FOR    ${x}    IN RANGE    0   2
    \   Log     'Start for: ' ${x}
    \   Nested Loop Keyword     ${x}
    \   Log     'End for: ' ${x}
    Log     end

*** Keywords ***
Nested Loop Keyword
    [Arguments]     ${got}
    Log     ${got}
    :FOR    ${y}    IN RANGE    0   1
    \   Log     ${y}

MyListener.py file content used to collect data as RF execution listener via option --listener:

import os.path
import tempfile


class MyListener(object):
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, filename='listen.txt'):
        self.output_path = os.path.join(tempfile.gettempdir(), filename)
        print 'Communication stored in: ' + str(self.output_path)
        self.outfile = open(self.output_path, 'w')
        self.line_number = 0

    def start_keyword(self, name, attributes):
        self.line_number += 1
        self.outfile.write(str(self.line_number) + ' {start_keyword, name=' + str(name) + ', type=' + str(attributes['type']) + '}')
        self.outfile.write('\n')


    def end_keyword(self, name, attributes):
        self.line_number += 1
        self.outfile.write(str(self.line_number) + ' {end_keyword, name=' + str(name) + ', type=' + str(attributes['type']) + '}')
        self.outfile.write('\n')

    def close(self):
        self.outfile.close()

The problem also show up on the newest RF 3.0a1 version.

Activity

  1. pekkaklarck commented on Nov 23, 2015

    @pekkaklarck
    Member

    Interesting bug, thanks for the report. The problem actually is that the code setting keyword types in listeners doesn't take for loops into account at all. It tries to return Keyword or Suite/Test Setup/Teardown to make it possible to know is a normal keyword or setup/teardown executed. As you can see from the code, it totally ignores the possibility that type can also be for or foritem:
    https://github.com/robotframework/robotframework/blob/master/src/robot/output/listeners.py#L129

    This is easy to fix but we need to first decide how to fix it. Should we set type to For or For Loop when a for loop starts? Should we use the same when different iterations start or should we then use something like For Item or For Loop Iteration?

  2. changed the title [-]RF wrongly notify test execution listener in case :FOR loop inside other :FOR loop exists in test case[/-] [+]Keyword type passed to listeners is wrong then keyword is for loop[/+] on Nov 23, 2015
  3. added this to the 3.0 milestone on Nov 23, 2015
  4. adam-wypych commented on Nov 23, 2015

    @adam-wypych
    Author

    In my opinion only the first :FOR should get type for and anything inside this loop include other :FOR should get type as foritem. In our case and I think in most cases listener is not able to judgment what new iterations will brings and the fact that to get :FOR in :FOR we need to bring this nested loop to other keyword is enough to get where we are now.

  5. jussimalinen commented on Nov 23, 2015

    @jussimalinen
    Member

    Internally foritem is a bad name for one iteration of the loop. (You will see this in the log file).

    So considering this input:

    *** Test Cases ***
    Example
        :FOR   ${i}   IN   first   second
        \      Log    ${i}
        \      No Operation  

    We would suggest renaming the foritem to for loop and the listener would then get the following events:

    for
      for loop (first) 
        keyword (log)
        keyword (no operation)
      for loop (second)
        keyword (log)
        keyword (no operation)
    

    How does this seem to you?

  6. adam-wypych commented on Nov 23, 2015

    @adam-wypych
    Author

    Can you describe Your proposal (events, which will we get) base on below test case:

    *** Test Cases ***
    ForTest
        Log     start
        :FOR    ${x}    IN RANGE    0   2
        \   Log     'Start for: ' ${x}
        \   Nested Loop Keyword     ${x}
        \   Log     'End for: ' ${x}
        Log     end
    
    *** Keywords ***
    Nested Loop Keyword
        [Arguments]     ${got}
        Log     ${got}
        :FOR    ${y}    IN RANGE    0   1
        \   Log     ${y}

    Thanks in advance for your prompt responses.

  7. jussimalinen commented on Nov 23, 2015

    @jussimalinen
    Member

    We talked with @pekkaklarck that the per loop item could be called "for loop" and each iteration "for iteration".

    So like this:

    • kw log (start)
    • for loop ( in range 0, 2)
      • for iteration (0)
        • kw log
        • kw nested loop keyword
          • kw log
          • for loop ( in range 0, 1)
            • for iteration (0)
              • kw log
        • kw log (end for 0)
      • for iteration (1)
        • kw log
        • kw nested loop keyword
          • kw log
          • for loop (in range 0, 1)
            • for iteration (0)
              • kw log
        • kw log (end for 1)
    • kw log (end)
  8. adam-wypych commented on Nov 23, 2015

    @adam-wypych
    Author

    The most important for us is to get the same type attribute in the end_keyword as we will get in start_keyword listener API function - without this information we will be blind. The second information is to clear indicate that RF execute currently repeat/iterate action - here :for loop. For me seems that we can meet this goal with this new two types, but it will be nice to do not mix old Test foritem with new types for loop iteration proposed by You.

  9. pekkaklarck commented on Nov 23, 2015

    @pekkaklarck
    Member

    OK, will go with the design illustrated by @jussimalinen above. I guess type names For Loop and For Iteration are good enough. Old Test foritem etc. were just bugs, they won't appear as types once we get this fixed.

  10. adam-wypych commented on Nov 23, 2015

    @adam-wypych
    Author

    OK thanks for both of You.

  11. pekkaklarck commented on Nov 23, 2015

    @pekkaklarck
    Member

    Actually, would it be enough if the type would just be Keyword? After studying this a bit more (including issue #859 that introduced the type attribute), I noticed that type is Suite/Test Setup/Teardown not only with the top level keyword uses as setup/teardown has but also with all keywords it uses. It would be more consistent to use same type everywhere.

  12. adam-wypych commented on Nov 23, 2015

    @adam-wypych
    Author

    Yes type keyword is enough for keywords inside other keywords, if main keyword will give much information where is executed - means Suite/Test Setup/Teardown/Test or :FOR loop.

  13. 9 remaining items

  14. adam-wypych commented on Dec 1, 2015

    @adam-wypych
    Author

    Ok as I understand the implementation will have 3 types only setup\keyword\teardown if yes then is acceptable.

  15. pekkaklarck commented on Dec 1, 2015

    @pekkaklarck
    Member

    My plan was to have types keyword, setup, teardown, for loop and for iteration (exact spelling/casing not yet decided). I wouldn't mind using keyword also with for loops, but somehow using the actual type sounds better to me.

  16. adam-wypych commented on Dec 1, 2015

    @adam-wypych
    Author

    @pekkaklarck for me its totaly ok with those 3 types (keyword, setup, teardown) minimum and maximum 5

  17. pekkaklarck commented on Dec 6, 2015

    @pekkaklarck
    Member

    Submitted #2248 about changes that we are going to do keyword type reporting. Problems initially reported here will be fixed as part of it, so I'll close this issue as its duplicate.

  18. removed this from the 3.0 milestone on Dec 6, 2015
  19. pekkaklarck commented on Dec 11, 2015

    @pekkaklarck
    Member

    Have you @adam-wypych had a change to test alpha 2 and how types work there? We can still do modifications before the final version if needed.

  20. adam-wypych commented on Dec 11, 2015

    @adam-wypych
    Author

    @Mateosy could you comment since is your nice feature ;) ?

  21. Mateosy commented on Dec 30, 2015

    @Mateosy

    Hi, It works fine for me.

  22. sathishj-gh1 commented on Nov 5, 2018

    @sathishj-gh1

    I'm not sure did I understand the previous comment correctly. Keyword type is always set, the question what values the type should have. Currently the type is setup/teardown for all keywords that are executed as part of setup/teardown. You can see that from my earlier example.

    If we change the type to be always the actual keyword type (possibility 2 in the aforementioned comment), then the output of the example in the comment would be like this:

    Test Setup:       Keyword [setup]
    Keyword:          BuiltIn.No Operation []
    Keyword:          Keyword [body]
    Keyword:          BuiltIn.No Operation []
    Test Teardown:    Keyword [teardown]
    Keyword:          BuiltIn.No Operation []
    

    This approach would have the following benefits:

    • It would make it possible to report for loop types separately from keywords.
    • It would allow separating keyword teardowns from test/suite teardowns. I just tested that currently keyword teardowns are reported to wrong!
    • It would simplify implementation

    The fact that currently also keyword teardowns are reported wrong was the last straw, and I plan to change the reported keyword types to be whatever type the keyword actually has. Let me know ASAP if that would be totally unacceptable for you and I can still reconsider it.

    Hi Pekkaklarck,
    if only the top level keyword has the type of Setup/Teardown, how do i know when Setup/Teardown keywords finishes and the Test keyword begins in case of nested keywords in the setup/teardown?
    Thanks
    Sathish

  23. adam-wypych commented on Nov 9, 2018

    @adam-wypych
    Author

    @sathishj-gh1 you need to simple
    a) implement your stack for getting notify about keywords executed so far
    b) in listener API http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#id780 you have end_keyword message - then you need to check the type 'type: String Keyword for normal keywords, Setup or Teardown for the top level keyword used as setup/teardown, For for for loops, and For Item for individual for loop iterations. NOTE: Keyword type reporting was changed in RF 3.0. See issue #2248 for details.'

    The b option is so far the best one ;)

  24. added theissue type on Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions