简体   繁体   English

Robot Framework导入库实例不包含已定义的方法

[英]Robot Framework import library instance does not contain defined methods

I have written a test case in Robot Framework which creates an instance of a class in the middle of Test Suite using Builtin.Import_Library keyword, then calls its methods using Builtin.Call_Method : 我已经在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}

File src/Interface/Utility/WebServiceUtil.py contains: 文件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!'

I get this error in test execution: 我在测试执行中收到此错误:

Object 'webserviceutil' does not have method 'get_sponsor_report'.

The console prints which I have put in the __init__ of class WebServiceUtil returns: 我在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']

As you can see, class methods are listed in the output of dir() , but not shown in the output of self.__dict__ . 如您所见,类方法列在dir()的输出中,但未显示在self.__dict__的输出中。

I also tried changing ROBOT_LIBRARY_SCOPE to GLOBAL , but it didn't change anything. 我还尝试将ROBOT_LIBRARY_SCOPE更改为GLOBAL ,但没有做任何更改。

Any idea what is the cause? 知道是什么原因吗?

EDIT 1: 编辑1:

I also tried calling __init__ method of super class in the beginning of method __init__ of class WebServiceUtil : 我也打过电话__init__的方法super类方法的开始__init__类的WebServiceUtil

super(WebServiceUtil, self).__init__()

Same results. 结果相同。

EDIT 2: 编辑2:

I tried calling WebServiceUtil methods without CALL METHOD as @Bryan said with two approaches: 我尝试调用不带CALL METHOD WebServiceUtil方法,如@Bryan所说的两种方法:

  1. ${result} = webserviceutil get_report_a
  2. ${result} = get_report_a

The first one returned No keyword with name 'webserviceutil.get_report_a' found. 第一个返回No keyword with name 'webserviceutil.get_report_a' found. and the second returned No keyword with name 'get_report_a' found. 第二个No keyword with name 'get_report_a' found. .

EDIT 3: 编辑3:

There are two things that seems to be creating the issue in my mind: 在我看来,有两件事正在造成这个问题:

  1. I have overwritten __init__ method. 我已经覆盖了__init__方法。
  2. Methods aren't static methods. 方法不是静态方法。

I have used classes in Robot Framework before and none of them had above specs; 我以前在Robot Framework使用过类,但没有一个具有以上规范。 so, I guess maybe these are making the issue here. 因此,我想也许是这些在这里造成了问题。

If you are importing it, the methods become keywords. 如果要导入,则方法将成为关键字。 You don't need to use call method . 您不需要使用call method In your example, when you import WebServiceUtil , you have access to keywords named get report A , get report B , and get report C . 在您的示例中,当您导入WebServiceUtil ,可以访问名为get report Aget report Bget report C关键字。

*** Test Cases ***
Test Report A
    ${result} =     get report A
    LOG    Result: ${result}        console=${TRUE}

As I mentioned in the question edits, the issue was related to the overwritten __init__ method and used my class variables in other ways. 正如我在问题编辑中提到的那样,该问题与被覆盖的__init__方法有关,并以其他方式使用了我的类变量。 I don't know why, but removing __init__ solved the problem. 我不知道为什么,但是删除__init__解决了问题。 Methods are still class methods; 方法仍然是类方法; which means both static and class methods are treated the same here. 这意味着静态方法和类方法在这里都被视为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM