提问者:小点点

脚本根据python的逻辑引发异常,但测试用例仍然在机器人框架中传递


我是机器人的新人,我的脚本执行部分有问题。

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

任何意见和建议都非常欢迎

如果我尝试运行关键工作,但我不知道如何使用它。请在下面查看我的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()

预期结果:

>

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

    机器人应该显示失败

    实际输出:

    >

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

    显示PASS的机器人脚本


  • 共2个答案

    匿名用户

    不要捕获Python代码中的异常,让机器人执行失败。您可以在测试拆卸部分进行清理。

    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
    

    匿名用户

    (第一次猜测,对机器人框架一无所知,但正要深入研究):您的机器人脚本正在$输出中寻找FAIL。您的输出包含字符串未找到清理正在进行中(可能具有不同的格式,因为您已经打印了两次而没有指定不打印换行符)。您的输出中没有FAIL。也许你是故意的

    Run Keyword if 'string not found' in $output
    

    在您的机器人脚本中,或

    print("FAIL: string not found")
    

    在您的验证代码中?

    更新:现在我已经花了一些时间在留档上(它们不容易学会做一些简单的事情),我可以说Bence Kaulics的答案基本上是正确的,这可能取决于你运行的版本。对我来说,他的回答中显示的测试失败了,因为try没有,除了。以下是我的工作原理(如,主要由于预期的原因失败):

    . bot文件:

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

    exception.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更好的方法。然而,如果您真正需要做的是检查stdout的值,您将需要更像这样的东西:

    . bot文件:

    *** 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
    ==============================================================================