robot framework: test case cannot load keyword in resource file that import in the test suite's parent test suite folder

做~自己de王妃 提交于 2019-12-13 03:52:19

问题


Hi, I am using robot framework to automate testing of a website, and the image above is the structure of test in the RIDE:

  • Test: a test suite folder, and I import resource file here, which is in the "init.robot" under the folder
  • Sub1: a sub test suite, import nothing
  • test: a test case

My problem is: in the test case "test", robot cannot recognize the keyword that imported in the "Test" test suite folder, because there will be more sub test suites, like sub2, sub3, how can I import resource in one place? I don't want to import the resource file in every test suite, is there a way to do that?


回答1:


You can chain an imports. Below is an example of such a chain and reuse. In this example we have a single resources.robot that imports all of the different sub*.robot files. This is the only file that imports these.

Then there are two testcases*.robot files that proceed to import resources.robot and are able to access the content of the sub*.robot keywords.

resources.robot

*** Settings ***
Resource    ../resources/sub1.robot
Resource    ../resources/sub2.robot
Resource    ../resources/sub1.robot

testcases1.robot

*** Settings ***
Resource    ../resources/resources.robot

*** Test Cases ***
TC
   No Operation

testcases2.robot

*** Settings ***
Resource    ../resources/resources.robot

*** Test Cases ***
TC
   No Operation

As discussed in the comments, that any keyword imported in the __init__.robot file is not available beyond that file. This is clearly described in the Robot Framework User Guide section on Initialization files.

That said, if the effort of including the master resource file in each suite file undesirable, then an alternative approach is to load the resource file using a listener at the start of each Suite. The documentation on Listeners can be found here: Docs

A new example:

AddResourceListener.py

from robot.libraries.BuiltIn import BuiltIn

class AddResourceListener(object):
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self):
        pass

    def start_suite(self, name, attributes):
        BuiltIn().import_resource('${EXECDIR}/resource.robot')

resource.robot

*** Keywords ***
Resource Keyword
    Log    "Keyword Executed from Resource File"

TestCase.robot

*** Test Cases ***
TC
    Resource Keyword

Then run your regular robot command with the additional argument --listener AddResourceListener.py and you'll be able to use the keyword regardless if it's imported or not.



来源:https://stackoverflow.com/questions/50790809/robot-framework-test-case-cannot-load-keyword-in-resource-file-that-import-in-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!