2017-04-16 937 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