2017-04-16 936 views
2

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

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 

回答

4

机器人不会自动创建的是在一个库文件中的类的实例,但有一个例外:如果名文件名匹配,而不.py扩展它会自动创建一个类的实例。例如,如果您的文件CheckCode.py定义了一个名为CheckCode的类,机器人将自动创建一个实例,并使用该实例将每个方法公开为关键字。

如果你想在一个文件中创建一个类的实例,你将不得不创建一个关键字来做到这一点。例如:

# 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 
1

这听起来像你可能使用物理路径导入库。

*** Settings *** 
Library CheckCode.person firstname lastname 
Library CheckCode.employee firstname lastname someid 

或动态:为了从同一模块中导入两个库,必须通过名称,如导入

Import Library CheckCode.person firstname lastname 
Import Library CheckCode.employee firstname lastname someid 

为了导入这样,你需要得到你的模块Python路径。请参阅this section寻求帮助。

从用户指南中的Using physical path to library

这种方法的一个限制是,作为Python类实施库必须与相同名称的类模块中。

+0

感谢您的回答。让我继续研究PYTHONPATH方法,并在有问题时再回来。 – user2905950

+0

尽管我试图回答这个问题,但我建议你看看Bryan的答案。我同意他对代码结构的评估。 – ombre42