![](/img/trans.png)
[英]Importing test library to Robot Framework for user defined methods
[英]Robot Framework import library instance does not contain defined methods
我已经在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'.
我在WebServiceUtil
类的__init__
中放入的console
打印返回:
>> 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:
我尝试调用不带CALL METHOD
WebServiceUtil
方法,如@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}
正如我在问题编辑中提到的那样,该问题与被覆盖的__init__
方法有关,并以其他方式使用了我的类变量。 我不知道为什么,但是删除__init__
解决了问题。 方法仍然是类方法; 这意味着静态方法和类方法在这里都被视为相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.