【问题标题】:Simple test case in robotframework机器人框架中的简单测试用例
【发布时间】:2018-08-11 13:42:56
【问题描述】:

我开始学习如何使用机器人框架进行测试 我创建了以下要在本地 linux 主机上执行的测试用例。

    *** Settings ***
Library     Process
Suite Teardown  Terminate All Processes     kill=True

*** Test Cases ***
${result} =     Run Process ping 1.1.1.1 -c 1   shell=True  stdout=/home/user/stdout.txt
Log     all output: ${result.stdout}
Should Contain  ${result.stdout}    64 bytes from 1.1.1.1*

当我执行这个测试用例时,我会在终端中得到以下结果:

➜  ~ robot firstlinux.robot
==============================================================================
Firstlinux                                                                    
==============================================================================
${result} =                                                           | PASS |
------------------------------------------------------------------------------
Log                                                                   | FAIL |
No keyword with name 'all output: ${result.stdout}' found.
------------------------------------------------------------------------------
Should Contain                                                        | FAIL |
No keyword with name '64 bytes from 1.1.1.1*' found.
------------------------------------------------------------------------------
Firstlinux                                                            | FAIL |
3 critical tests, 1 passed, 2 failed
3 tests total, 1 passed, 2 failed
==============================================================================
Output:  /home/user/output.xml
Log:     /home/user/log.html
Report:  /home/user/report.html

我如何让这个测试通过,我正在检查标准输出并确保它有来自 ping 命令的字符串输出,但看起来机器人没有捡起这个。我看到创建了一个 stdout.txt 文件,该文件具有来自 stdtout 的结果:

➜  ~ ping 1.1.1.1 -c 1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=55 time=30.5 ms

--- 1.1.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 30.565/30.565/30.565/0.000 ms

如何在生成的报告中显示标准输出,目前我在报告中看不到输出。

谢谢

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    您的问题是您没有正确缩进测试用例中的关键字。 Robot 认为左边距的任何东西都是测试用例的名称。

    您发布了此代码:

    *** Test Cases ***
    ${result} =     Run Process  ping 1.1.1.1 -c 1   shell=True  stdout=/home/user/stdout.txt
    Log     all output: ${result.stdout}
    Should Contain  ${result.stdout}    64 bytes from 1.1.1.1*
    

    在上面,机器人认为第一个测试用例命名为${result}。它认为Log 是第二个测试用例的名称,Should Contain 是第三个测试用例的名称。

    紧跟在这些名称之后的词被假定为关键字。这就是它抱怨all output: ${result.stdout} 的原因,因为这是您命名为Log 的“测试用例”的第一个关键字。

    相反,您需要为您的测试用例命名,然后缩进所有这些语句。例如,如果您想将此测试称为“Ping 测试”,您可以这样做:

    *** Test Cases ***
    Ping test
        ${result} =     Run Process ping 1.1.1.1 -c 1   shell=True  stdout=/home/user/stdout.txt
        Log     all output: ${result.stdout}
        Should Contain  ${result.stdout}    64 bytes from 1.1.1.1*
    

    注意:Run Process 后面还需要两个或多个空格。

    【讨论】:

    • 感谢您的回答。这解决了我的问题。当我浏览文档时,我没有理解关于测试用例缩进的观点。
    猜你喜欢
    • 2016-03-01
    • 2018-10-25
    • 2019-10-25
    • 2020-07-12
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2018-12-25
    相关资源
    最近更新 更多