【问题标题】:Script raising Exception as per python's logic but Test Case still passed in robot framework根据python的逻辑引发异常的脚本,但测试用例仍然在机器人框架中通过
【发布时间】:2019-07-09 21:22:15
【问题描述】:

我是 Robot 的新手,我的脚本执行部分有问题。

根据 python 的逻辑,我的案例正确失败。然而机器人仍然通过它。我很确定我错过了机器人脚本结尾的一些东西,比如一些关键字。

欢迎任何意见和建议

我尝试了 Run keywork if 但我不知道如何使用它。请在下面查看我的 python 和机器人代码

robot script:

*** Settings ***
Documentation       TC2
Library             exception
#Library           common_functions

*** Keywords ***

Run Keyword if 'FAIL' in $output

*** Test Cases ***
TEST CASE TWO
    validation

python 代码:

import os
import re


def validation():
    try:
        if 'loopback is up, line protocol is up' in open('1.txt').read():
            print("string found")

        else:
            print ("string not found")
            raise Exception("string not found")

    except:
        cleanup()

def cleanup():
    print("cleanup is in progress")


validation()

预期结果:

  1. Python 脚本显示: 未找到字符串 清理正在进行中

  2. 机器人应该显示失败

实际输出:

  1. Python 脚本显示: 未找到字符串 清理正在进行中

  2. 显示 PASS 的机器人脚本

【问题讨论】:

    标签: python robotframework


    【解决方案1】:

    不要在 Python 代码中捕获异常,否则会导致机器人执行失败。您可以在test teardown section 中进行清理。

    import os
    import re
    
    
    def validation():
        if 'loopback is up, line protocol is up' in open('1.txt').read():
            print("string found")
    
        else:
            print ("string not found")
            raise RuntimeError("string not found")
    
    
    def cleanup():
        print("cleanup is in progress")
    

    *** Settings ***
    Documentation       TC2
    Library             exception
    
    *** Test Cases ***
    TEST CASE TWO
        validation
        [Teardown]    cleanup
    

    【讨论】:

    • 伙计们三思而后行,我们可以通过 run 关键字 if 来做到这一点
    • @Diwakar 如果您的 Python 脚本会返回一个布尔值或字符串,那么您肯定可以将该值放入 Run Keyword If 并以这种方式执行清理。
    【解决方案2】:

    (首先猜测,对机器人框架一无所知,但即将深入研究):您的机器人脚本正在$output 中寻找FAIL。您的输出包含 string not found cleanup is in progress (可能具有不同的格式,因为您已打印两次但未指定不打印换行符)。您的输出中没有 FAIL。也许你打算拥有

    Run Keyword if 'string not found' in $output
    

    在您的机器人脚本中,或

    print("FAIL: string not found")
    

    在您的验证码中?

    更新:现在我已经花了一些时间在文档上(它们不会让学习做一些简单的事情变得容易),我可以说 Bence Kaulics 的答案大部分是正确的,可能取决于你的版本跑步。对我来说,由于try 没有except,他的答案中显示的测试以IndentationError 失败。这就是我所拥有的工作(例如,由于预期的原因而失败,主要是):

    .机器人文件:

    *** Settings ***
    Documentation       TC2
    Library             exception.py
    
    *** Test Cases ***
    TEST CASE TWO
        validation
    

    异常.py:

    def validation():
        try:
            if 'loopback is up, line protocol is up' in open('1.txt').read():
                result = 'string found'
            else:
                result = 'string not found'
                raise RuntimeError(result)
        except Exception as e:
            result = 'string not found'
            raise RuntimeError(result)
        finally:
            print(result)
            cleanup()
    
    
    def cleanup():
        print("cleanup is in progress")
    

    通过以上,我得到了输出:

    (robottestframework) ~/s/l/robottestframework> robot -L TRACE keyword_driven.robot  
    ==============================================================================
    Keyword Driven :: TC2                                                         
    ==============================================================================
    TEST CASE TWO                                                         | FAIL |
    string not found
    ------------------------------------------------------------------------------
    Keyword Driven :: TC2                                                 | FAIL |
    1 critical test, 0 passed, 1 failed
    1 test total, 0 passed, 1 failed
    ==============================================================================
    

    请注意,这会改变您正在测试的语义:您正在运行从库导入的关键字,而不是检查 stdout 的值,如果引发异常则失败。 IMO,这是比检查标准输出更好的方法。但是,如果您真正需要做的是检查 stdout 的值,您将需要更多类似的东西:

    .机器人文件:

    *** Settings ***
    Documentation       TC2
    Library             Process
    Suite Teardown      Terminate All Processes     kill=True
    
    *** Test Cases ***
    TEST CASE TWO
        ${result} =     Run Process     python      exception.py
        should not contain      ${result.stdout}        string not found
    

    exception.py 文件:

    def validation():
        result = ''
        try:
            if 'loopback is up, line protocol is up' in open('1.txt').read():
                result = 'string found'
            else:
                result = 'string not found'
                raise RuntimeError(result)
        except Exception as e:
            result = 'string not found'
        finally:
            print(result)
            cleanup()
        return result
    
    
    def cleanup():
        print("cleanup is in progress")
    
    
    validation()
    

    通过这些更改,我得到:

    (robottestframework) ~/s/l/robottestframework> robot -L TRACE keyword_driven.robot  
    ==============================================================================
    Keyword Driven :: TC2                                                         
    ==============================================================================
    TEST CASE TWO                                                         | FAIL |
    'string not found
    cleanup is in progress' contains 'string not found'
    ------------------------------------------------------------------------------
    Keyword Driven :: TC2                                                 | FAIL |
    1 critical test, 0 passed, 1 failed
    1 test total, 0 passed, 1 failed
    ==============================================================================
    

    【讨论】:

    • 我是否还需要在 $output 中指定一些内容,实际上我的输出文件位于 /home/output.xml 位置
    • 我是否还需要在 $output 中指定一些内容,实际上我的输出文件位于 /home/output.xml 位置 - 欢迎任何输入我现在正在使用以下内容
    • 如果 ${output} 中出现'FAIL:string not found',则运行关键字,我也在验证码中添加了这个
    • 全面披露:我也是机器人框架的新手,但我认为尝试回答您的问题会给我一个很好的介绍。我现在有一个加载了您的示例代码的项目。我会让你知道我的发现。
    • 很棒的答案
    猜你喜欢
    • 2019-10-25
    • 2018-09-04
    • 2022-12-28
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2018-12-25
    相关资源
    最近更新 更多