Skip to content

AttributeError: 'keywords' attribute is read-only and deprecated since Robot Framework 4.0. Use 'body', 'setup' or 'teardown' instead. #3868

Closed

Description

@sridharvpmca

I have been dynamically creating the test cases using the API. Below are the details for your reference.
During robot framework upgrade testing I am getting the following error.

AttributeError: 'keywords' attribute is read-only and deprecated since Robot Framework 4.0. Use 'body', 'setup' or 'teardown' instead.

Please help out to understand better on the changes in 4.0

Method Used:

class DynamicTestCases(object):
ROBOT_LISTENER_API_VERSION = 3
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

def __init__(self):
    self.ROBOT_LIBRARY_LISTENER = self

def _start_suite(self, suite, result):
    # save current suite so that we can modify it later
    self.current_suite = suite

def add_test_case(self, name, doc, tags, kwname, *args):
    """Adds a test case to the current suite

    'name' is the test case name
    'kwname' is the keyword to call
    '*args' are the arguments to pass to the keyword

    Example:
        add_test_case  Example Test Case
        ...  log hello, world  WARN
    """
    tc = self.current_suite.tests.create(name=name, doc=doc, tags=tags)
    tc.keywords.create(name=kwname, args=args)

TC:
Create and execute test cases dynamically using test data sheet
[Arguments] ${test_data_full_path}
Set Log Level DEBUG
Log ${test_data_full_path}
Log Get the list of test cases to be executed
${test_cases_list}= API Test Cases List ${test_data_full_path}
${no_of_test_cases}= Get Length ${test_cases_list}
Log Iterate through each test case and execute the test case
FOR ${i} IN RANGE ${no_of_test_cases}
${test_name}= Get From Dictionary ${test_cases_list}[${i}] test_name
${tags}= Get From Dictionary ${test_cases_list}[${i}] test_tags
@{test_tags}= Split String ${tags} ,
${doc}= Get From Dictionary ${test_cases_list}[${i}] test_documentation
Add Test Case ${test_name} ${doc} ${test_tags} Generate and compare expected/actual results ${test_name}
END

Activity

  1. sridharvpmca commented on Feb 27, 2021

    @sridharvpmca
    Author

    Based on further research in case of creating the keywords in 4.0 I will have to use the below code.. Please review and confirm
    tc = self.current_suite.tests.create(name=name, doc=doc, tags=tags)
    tc.body.create_keyword(name=kwname, args=args)

  2. pekkaklarck commented on Mar 2, 2021

    @pekkaklarck
    Member

    You are right, keywords has been renamed to body as part of #3749. It's possible to still access keywords, but modifying it isn't supported at all. Because body can contain other objects than keywords, you also need to use body.create_keyword instead of body.create. setup and teardown are also nowadays directly in the suite/test/keyword object when you earlier needed to use obj.keywords.setup and obj.keywords.teardown, but this doesn't seem to affect the code above.

    As you commented, it ought to be enough to simply change tc.keywords.create to tc.body.create_keyword in this case. Did that work? If you want to support also older RF versions, you can use, for example,

    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)
  3. sridharvpmca commented on Mar 3, 2021

    @sridharvpmca
    Author

    Thank you so much and after changing to tc.body.create_keyword .. everything works fine. The code which you share for backward compatibility is very useful, I have in corporate them in the script.

  4. Test-Hassan commented on Sep 7, 2021

    @Test-Hassan

    Can I get the list of Keywords which are deprecated in Robot Framework V 4.0 please?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions