![](/img/trans.png)
[英]How can I resolve a "NameError: name 'null' is not defined" error while trying to import any module in Python 2.7
[英]How can I resolve “NameError: global name 'x' is not defined” error while running a Robot Framework testcase using a custom library?
在 Robot Framework 中運行測試用例時,我看到“NameError: global name 'x' is not defined”錯誤。
以下是我的自定義庫文件(根據 Bryan Oakley 的評論修改):
import re
def pass_fail_criteria():
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+", x)[0]):
return "pass"
else:
return "fail"
以下是“pass_fail.robot”文件內容:
*** Settings ***
Library Selenium2Library
Library SSHLibrary
Library regexp_def.py
Suite Setup Go to gmail page
Suite Teardown Close All Browsers
*** Variables ***
${HOMEPAGE} https://www.gmail.com/intl/en/mail/help/about.html
${BROWSER} firefox
${LOGINPAGE} https://www.gmail.com/intl/en/mail/help/about.html
${FINALURL} https://mail.google.com/mail/
${FINALURL1} https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/'
${HOST} 1.1.1.1
${USERNAME} test
${PASSWORD} test
*** Test Cases ***
Login into gmail
Go to gmail page
Login Page Should Be Open
Click Signin Button
Input Username test@gmail.com
Input Password test@123
Submit Credentials
Inbox page should open
Check Deep Packet Inspection Stats
Open Connection ${HOST}
enable ssh logging XYZ
Login ${USERNAME} ${PASSWORD}
Write enable
Write show dpi app stats gmail on AVC/ap7532-15E8CC
${x} Read Until Regexp .*#
Pass fail Criteria
${status} pass fail criteria
should be equal ${status} pass
${result} Pass fail criteria ${x}
*** Keywords ***
Go to gmail page
Open Browser ${HOMEPAGE} ${BROWSER}
Maximize Browser Window
Login Page Should Be Open
Location Should Be ${LOGINPAGE}
Click Signin Button
Click Element id=gmail-sign-in
Input Username
[Arguments] ${username}
Input Text id=Email ${username}
Input Password
[Arguments] ${password}
Input Text id=Passwd ${password}
Submit Credentials
Click Button id=signIn
Inbox page should open
Location Should Be ${FINALURL}
運行此文件時出現以下錯誤:
C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria>pybot pass_
fail.robot
==============================================================================
Pass Fail
==============================================================================
Login into gmail | PASS |
------------------------------------------------------------------------------
Check Deep Packet Inspection Stats | PASS |
------------------------------------------------------------------------------
Pass fail Criteria | FAIL |
NameError: global name 'x' is not defined
------------------------------------------------------------------------------
Pass Fail | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================
Output: C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\ou
tput.xml
Log: C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\lo
g.html
Report: C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\re
port.html
C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria>
以下代碼存在問題:
Pass fail Criteria
${status} pass fail criteria
should be equal ${status} pass
${result} Pass fail criteria ${x}
我該如何解決這個問題?
你有幾個問題對你不利。 您似乎對基於 Python 的關鍵字的工作方式有根本的誤解。
您正在定義和導入名為 regexp_def.py 的庫。 其中有一個關鍵字,“pass_fail_criteria”。 Robot 會去掉下划線,所以從Robot 的角度來看,這個關鍵字被命名為“通過失敗標准”。
在您的測試用例中,您還創建了一個名為“通過失敗標准”的關鍵字。 目前還不清楚你為什么這樣做。 不要那樣做。 刪除該關鍵字; 這是不必要的。
您在pass_fail_criteria
中使用了變量x
,但您尚未定義它。 這就是錯誤告訴你的。 你需要定義它,或者傳入它。要傳入它,你需要把它變成一個參數,然后你需要為那個參數提供一個值。 這與任何其他關鍵字或任何其他功能沒有什么不同。
在文件regexp_def.py 中:
import re
def pass_fail_criteria(x):
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
return "pass"
else:
return "fail"
(注意定義中添加的參數)
在您的測試用例中:
Pass fail Criteria
${status} pass fail criteria ${x}
(注意第二行末尾的參數)
按照您當前構建測試用例的方式,您是在一個測試用例中定義${x}
,然后嘗試在另一個測試用例中使用它。 我不知道這是不是故意的,但很多人認為這種測試用例設計很糟糕。 測試用例應該盡可能獨立。
雖然您可以這樣做(使用內置關鍵字Set Suite Variable ),但我建議在名為“Check Deep Packet Inspection Stats”的測試用例中調用pass fail criteria
,其中定義了${x}
。
例如:
Check Deep Packet Inspection Stats
...
${x} Read Until Regexp .*#
${status} pass fail criteria ${x}
Run keyword if "${status}" == "pass" ...
x
未定義,您在以下語句中使用x
。
if int(re.findall(r"NUM_FLOWS\\n-+[\\s\\S]*?(\\d+)\\s*-+",x)[0]):
將x
作為參數傳遞給函數pass_fail_criteria(x)
並使用try except
def pass_fail_criteria(x):
try:
a = int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0])
return "pass"
except:
return "fail"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.