2015-02-24 445 views
0

我是Robot framework自動化測試的新手。我爲使用Python的簡單Quiz應用程序編寫腳本和庫,並遵循關鍵字驅動的方法來測試用例。 我的腳本是:在Robot Framework中使用關鍵字驅動方法執行測試用例時出現錯誤

class Quiz(object): 
OPTIONS = 'ab' 
count = 0 

def __init__(self): 
    self._score = 0 
    Quiz.count+=1 
def score(self, str1): 
    if str1 not in self.OPTIONS: 
     raise QuizError("Invalid button '%s'." % str1) 
    if str1 == 'a' and Quiz.count == 1: 
     self._score +=1 
    elif str1 == 'a' and Quiz.count == 2: 
     self._score +=1 
    elif str1 == 'a' and Quiz.count == 3: 
     self._score +=1 
    return self._score 


class QuizError(Exception): 
pass 

庫文件:

from quiz import Quiz, QuizError 
class QuizLibrary(object): 


def __init__(self): 
    self._calc = Quiz() 
    self._result = 0 

def option(self, answer):   
    self._result = self._calc.score(answer) 


def result(self, expected): 

    if self._result != expected: 
     raise AssertionError('%s != %s' % (self._result, expected)) 

keyword_driven.txt:

*** Settings *** 

Library quizlibrary.py 

*** Test Cases *** 

Quiz Answer 

    option a 

    option b 

    option a 

    result 2 

它顯示了以下錯誤:

Quiz Answer                FAIL | 
No keyword with name 'option' found. 
---------------------------------------------------------------------------------------- 
Testcase :: Example test cases using the keyword-driven testing approach.  | FAIL | 
1 critical test, 0 passed, 1 failed 
1 test total, 0 passed, 1 failed 

任何人都可以幫助我至 解決這個?

請讓我知道如何在關鍵字驅動的方法中創建用戶關鍵字。

回答

1

用於關鍵​​字庫的Python模塊的名稱必須存在問題。

  • 如果你的模塊的名稱是QuizLibrary.py,然後更改的情況下,在您的測試庫導入:

Library QuizLibrary.py

  • 如果你的模塊的名稱是quizlibrary.py ,然後用這種對齊類的名稱:

class quizlibrary(object):

而且也不要忘記在「圖書館」和圖書館名稱之間至少有2個空格。

+0

模塊的名稱沒有問題,但它始終顯示「關鍵字未找到」。您能告訴我爲什麼會顯示此錯誤嗎? – Nithya 2015-02-25 06:29:12

+0

我在本地複製了您的代碼(使用我建議的修復方法),並且可以使其正常工作...因此,如果您仍然有「未找到關鍵字」,這意味着庫文件沒有正確加載。在日誌中,你有沒有關於lib的加載的錯誤信息? – 2015-02-25 09:12:51

+0

你..問題與您的建議解決。感謝您的答覆 – Nithya 2015-02-26 12:09:17

相關問題