在使用自定义库运行Robot Framework测试用例时,如何解决“NameError:全局名称“x”未定义”错误?

2024-04-19 15:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在Robot框架中运行测试用例时,我看到了“NameError:global name'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\u 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}

如何解决此问题


Tags: httpscominputstatspageloginmailpass
2条回答

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"

你有几个问题对你不利。似乎您对基于Python的关键字的工作方式有一个基本的误解

同名的两个关键字

您正在定义和导入名为regexp_def.py的库。其中有一个关键词,“通过\不通过\标准”。Robot将删除下划线,因此从Robot的角度来看,该关键字被命名为“通过-失败标准”

在您的测试用例中,您还创建了一个名为“Pass-fail-criteria”的关键字。不清楚你为什么这么做。不要那样做。删除该关键字;这是不必要的

变量“x”和“${x}”

您正在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"   ...

相关问题 更多 >