accessing static class variables with robotframework?

二次信任 提交于 2019-12-23 00:51:22

问题


Is it possible to access static class variables from a library loaded into robot framework?

For example, say I include the following python library in my .robot file:

foo = 'value'

class MyClass(self):
    bar = 'value'

Is there a way in the .robot file that included it to refer to foo or bar?


回答1:


You can get at these by getting a reference to the raw python module with Get Library Instance, and then use extended variable syntax to get the values.

For example, consider a library named MyLibrary.py:

# MyLibrary.py
foo = "this is foo"

class MyClass(object):
    bar = "this is bar"

You can access foo and bar by using Get Library Instance to get a handle to the library:

*** Settings ***
| Library | MyLibrary.py

*** Test Cases ***
| Example of accessing variables in a library
| | ${lib}= | Get Library Instance | MyLibrary
| | 
| | Should be equal as strings | ${lib.foo} | this is foo
| | Should be equal as strings | ${lib.MyClass.bar} | this is bar



回答2:


You should add ROBOT_LIBRARY_SCOPE = 'GLOBAL' to MyClass. If not Get Library Instance will create second instance of the MyClass.



来源:https://stackoverflow.com/questions/25859499/accessing-static-class-variables-with-robotframework

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