我在Robot Framework
中编写了一个测试案例,该案例使用Test Suite
关键字在Builtin.Import_Library
中间创建一个类的实例,然后使用Builtin.Call_Method
调用其方法:
*** Settings ***
Resource MyKeywords.robot
Test Suite Initiate My Test
*** Keywords ***
Initiate My Test
${ip} = SET VARIABLE localhost
${port} = SET VARIABLE 2020
IMPORT LIBRARY src/Interface/Utility/WebServiceUtil.py
... ws_ip=${ip} ws_port=${port} WITH NAME webserviceutil
*** Test Cases ***
Test Report A
${result} = CALL METHOD webserviceutil get_report_a
LOG Result: ${result} console=${TRUE}
文件
src/Interface/Utility/WebServiceUtil.py
包含:# -*- encoding: utf-8 -*-
import requests
import json
from robot.api import logger
class WebServiceUtil(object):
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
def __init__(self, ws_ip, ws_port):
self.reporter_a = ReportA(ip=ws_ip, port=ws_port)
self.reporter_b = ReportB(ip=ws_ip, port=ws_port)
self.reporter_c = ReportC(ip=ws_ip, port=ws_port)
logger.console('>> ZiZi >> webserviceutil has been initialized successfully!')
logger.console('>> ZiZi >> self.__dict__: ' + str(self.__dict__))
logger.console('>> ZiZi >> dir(self): ' + str(dir(self)))
def get_report_a(self):
return self.reporter_a.get_report()
def get_report_b(self):
return self.reporter_b.get_report()
def get_report_c(self):
return self.reporter_c.get_report()
class Report(object):
def get_report():
return 'This is abstract class!'
class ReportA(Report):
def get_report():
return 'This is class A!'
class ReportB(Report):
def get_report():
return 'This is class B!'
class ReportC(Report):
def get_report():
return 'This is class C!'
我在测试执行中收到此错误:
Object 'webserviceutil' does not have method 'get_sponsor_report'.
我放在
console
类的__init__
中的WebServiceUtil
打印件返回:>> ZiZi >> webserviceutil has been initialized successfully!
>> ZiZi >> self.__dict__: {'reporter_a': <WebServiceUtil.ReportA object at 0x7fc18d96a8d0>, 'reporter_b': <WebServiceUtil.ReportB object at 0x7fc18d96abd0>, 'reporter_c': <WebServiceUtil.ReportC object at 0x7fc18d96a910>}
>> ZiZi >> dir(self): ['ROBOT_LIBRARY_SCOPE', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'get_report_a', 'get_report_b', 'get_report_c', 'reporter_a', 'reporter_b', 'reporter_c']
如您所见,类方法列在
dir()
的输出中,但未显示在self.__dict__
的输出中。我还尝试将
ROBOT_LIBRARY_SCOPE
更改为GLOBAL
,但没有任何改变。知道是什么原因吗?
编辑1:
我还尝试在类
__init__
的方法super
的开头调用__init__
类的WebServiceUtil
方法:super(WebServiceUtil, self).__init__()
结果相同。
编辑2:
我尝试调用不带
WebServiceUtil
的CALL METHOD
方法,如@Bryan所说的两种方法:${result} = webserviceutil get_report_a
${result} = get_report_a
第一个返回
No keyword with name 'webserviceutil.get_report_a' found.
,第二个返回No keyword with name 'get_report_a' found.
。编辑3:
在我看来,似乎有两件事正在造成这个问题:
我已经覆盖了
__init__
方法。方法不是静态方法。
我以前在
Robot Framework
中使用过类,但没有一个符合以上规范。因此,我想也许是这些在这里造成了问题。 最佳答案
如果要导入,则方法将成为关键字。您不需要使用call method
。在您的示例中,导入WebServiceUtil
时,您可以访问名为get report A
,get report B
和get report C
的关键字。
*** Test Cases ***
Test Report A
${result} = get report A
LOG Result: ${result} console=${TRUE}