![](/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.