Robot Frameworkでテストケースを実行しているときに、「NameError:グローバル名 'x'が定義されていません」というエラーが表示されます。
以下は私のカスタムライブラリファイルです(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 [email protected]
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」という1つのキーワードがあります。Robotはアンダースコアを削除するため、Robotの観点から、このキーワードは「合格不合格基準」と呼ばれます。
テストケースでは、「合格不合格基準」というキーワードも作成しています。なぜそうしているのかは不明です。やめてください。そのキーワードを削除します。不要です。
あなたは、変数を使用しているx
中でpass_fail_criteria
、あなたはそれを定義していません。それはエラーがあなたに言っていることです。定義するか、渡す必要があります。渡すには、パラメーターにする必要があります。次に、そのパラメーターの値を指定する必要があります。これは、他のキーワードや他の関数と何ら変わりはありません。
ファイル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}
(2行目の終わりの引数に注意してください)
現在テストケースを構造化する方法では、あるテストケースで定義${x}
し、それを別のテストケースで使用しようとしています。これが意図的なものかどうかはわかりませんが、多くの人がこの悪いテストケースの設計を検討しています。テストケースは、可能な限り独立している必要があります。
これは(組み込みのキーワードSet Suite Variableを使用して)実行できますが、定義されている「Check Deep PacketInspectionStats」という名前のテストケースで呼び出すことをお勧めします。pass fail criteria
${x}
例えば:
Check Deep Packet Inspection Stats
...
${x} Read Until Regexp .*#
${status} pass fail criteria ${x}
Run keyword if "${status}" == "pass" ...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加