开发者社区> 问答> 正文

从ROBOT框架中的python模块调用特定方法

有一个具有2个类的Python模块。每个类都有一组定义的函数或方法。我们如何从ROBOT框架的类中调用特定方法。我正在尝试下面的方法,但是它给出了以下错误。有人可以帮我解决这里的问题。Python模块和Robot文件在同一路径中。我尝试将库语句更改为CheckCode.employee WITH_NAME xyz。这没有帮助。谢谢。

ERRORS
==============

[ WARN ] Imported library '/homes/user/New/CheckCode.py' contains no keywords.
==============================================================================
CheckCode :: Checking small built in code                                     
==============================================================================
Verify we can call a particular class from a Python Module in Robot   | FAIL |
No keyword with name 'my_code.employee.staff info' found.
------------------------------------------------------------------------------
CheckCode :: Checking small built in code                             | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================


Python Module File output
******************************

import re
import collections
import math

class person():
    def __init__(self,first,last):
        self.firstname = first
        self.lastname = last

    def emp_name(self):
        return self.firstname + " " + self.lastname

class employee(person):
    def __init__(self,first,last,empId):
        person.__init__(self,first,last)
        self.staffId = empId

    def staff_info(self):
        return self.Name() + " " + self.staffId

ROBOT FILE 
******************************

*** Settings ***
Documentation    Checking small built in code
Library   BuiltIn
Library   Collections
Library   CheckCode.py     WITH NAME   my_code

*** Test Cases ***
Verify we can call a particular class from a Python Module in Robot
    Log     Hello World
    ${var} =    my_code.employee.staff info     Maggi       Nestle      20000


*** Keywords ***
Init
    Set Log Level    DEBUG

展开
收起
祖安文状元 2020-02-22 18:28:15 1031 0
1 条回答
写回答
取消 提交回答
  • Robot不会自动创建库文件中的类的实例,只有一个例外:如果名称与不带.py扩展名的文件名匹配,它将自动创建类的实例。例如,如果您的文件CheckCode.py定义了一个名为的类CheckCode,robot将自动创建一个实例,并使用该实例将每个方法公开为关键字。

    如果要在文件中创建某个类的实例,则必须创建一个执行该操作的关键字。例如:

    # CheckCode.py
    class person()
        ...
    ...
    def create_person(first, last):
        return person(first, last)
    
    

    然后可以像这样使用它:

    *** Settings ***
    Library    CheckCode.py
    
    
    *** Test Cases ***
    Example
        ${person}=    create person    Maggi    Nestle
        Should be equal as strings    ${person.emp_name()}    Maggi Nestle
    
    

    您还可以使用Call Method关键字在对象上调用方法:

    ${name}=    Call method    ${person}    emp_name
    
    2020-02-22 18:28:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载