【发布时间】:2015-02-22 15:08:28
【问题描述】:
我有一个 python 类:
from robot.api import logger
class TestClass(object):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
logger.info('initialized', also_console=True)
def print_arg1(self):
print self.arg1
def print_arg2(self):
print self.arg2
我写了一个名为“CommonKeywords.robot”的关键字文件:
*** Settings ***
Library ../Libs/TestClass.py arg1 arg2 WITH NAME class1
*** Keywords ***
print arg1 class1
class1.print_arg1
print arg2 class1
class1.print_arg2
而我的场景文件是“scenario.robot”:
*** Settings ***
Resource ../Keywords/CommonKeywords.robot
*** Test Cases ***
Test Prints
print arg1 class1
这是我的项目结构:
Test
---- Keywords
---- CommonKeywords.robot
---- Scenarios
---- scenario.robot
---- Libs
---- TestClass.py
我将目录更改为Test/Scenarios 并在命令行中输入pybot scenario.robot。该脚本打印了两个initialized,这意味着它已经对对象进行了两次初始化:
有什么问题??
我这样改变了我的班级:
from robot.api import logger
class TestClass(object):
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
logger.info('initialized', also_console=True)
def print_arg1(self):
print self.arg1
def print_arg2(self):
print self.arg2
这是我想要的,我在应用 Bryan 的回答后得到了:
【问题讨论】:
-
我不会重现您的问题。您在哪里打印“初始化”?我什至没有得到它一次。
-
@LaurentBristiel,我添加了一些细节。顺便说一句,布莱恩的回答很棒! :-)
标签: python import robotframework