【问题标题】:test case teardown does not execute when using robot.api but does work when using pybot runner使用robot.api时不执行测试用例拆解,但在使用pybot runner时确实有效
【发布时间】:2016-11-01 19:53:23
【问题描述】:

背景

我正在试验 Robot Framework 的编程 API,以便从活动数据集动态创建测试用例。

问题总结

在将我的测试转换为使用robot.api 包中提供的实用程序库运行时,我遇到了一个问题,即测试用例的teardown 关键字似乎没有被执行。如果我在 .robot 文件中使用关键字,则此 teardown 似乎会执行。

我的问题是:为什么我的测试用例拆解在使用robot.api而不是pybot时无法执行?

示例

我创建了以下示例来演示我遇到的问题。要运行它,您需要安装 Python 机器人框架库 (pip install robotframework)。

在示例运行(如下)中,您将看到控制台日志行 Test Case Teardown Ran 出现在 pybot 结果中,但不在 robot.api 结果中。我提前为这些行的格式道歉 - 这是测试用例运行器输出它们的方式,我不想修改输出并可能删除过程中的一些线索。

resource.robot

** Keywords **
TestSuite Setup
    Log To Console  TestSuite Setup Ran 

TestSuite Teardown
    Log To Console  TestSuite Teardown Ran 

TestCase Setup
    Log To Console  TestCase Setup Ran 

TestCase Teardown
    Log To Console  TestCase Teardown Ran 

suite.robot

** Settings **
Resource             resource.robot

Suite Setup     TestSuite Setup
Suite Teardown  TestSuite Teardown

** Test Cases **
Example test case
    [setup]     TestCase Setup
    [teardown]  TestCase Teardown
    Log To Console  Test ran 

suite.py

import robot.api as robot_api

suite = robot_api.TestSuite('Programmatic test suite')
suite.resource.imports.resource('resource.robot')
suite.keywords.create('TestSuite Setup', type='setup')
suite.keywords.create('TestSuite Teardown', type='teardown')

test = suite.tests.create('Example test case')
test.keywords.create('TestCase Setup', type='setup')
test.keywords.create('TestCase Teardown', type='teardown')
test.keywords.create('Log To Console', args=['Test ran'])
result = suite.run(output='output.xml')
robot_api.ResultWriter(result).write_results(
    log='log.html',
    report='report.html'
)

结果(使用 Pybot)

$ pybot suite.robot
==============================================================================
Suite                                                                         
==============================================================================
TestSuite Setup Ran
Example test case                                                     TestCase Setup Ran
.Test ran
.TestCase Teardown Ran
Example test case                                                     | PASS |
------------------------------------------------------------------------------
TestSuite Teardown Ran
Suite                                                                 | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /private/tmp/robot_problem/output.xml
Log:     /private/tmp/robot_problem/log.html
Report:  /private/tmp/robot_problem/report.html

结果(使用 python-2.7)

$ python suite.py 
==============================================================================
Programmatic test suite                                                       
==============================================================================
TestSuite Setup Ran
Example test case                                                     TestCase Setup Ran
.Test ran
Example test case                                                     | PASS |
------------------------------------------------------------------------------
TestSuite Teardown Ran
Programmatic test suite                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /private/tmp/robot_problem/output.xml

【问题讨论】:

    标签: python-2.7 robotframework


    【解决方案1】:

    这对我来说听起来像是一个错误。或者也许是一个无证的怪癖。 setup 关键字必须添加 FIRST,而 teardown 必须添加 LAST

    test = suite.tests.create('Example test case')
    test.keywords.create('TestCase Setup', type='setup')
    test.keywords.create('Log To Console', args=['Test ran'])
    test.keywords.create('TestCase Teardown', type='teardown')
    result = suite.run(output='output.xml')
    

    我通过查看 robots/model/keyword.py 中的类关键字发现了这一点:

    @property
    def setup(self):
        return self[0] if (self and self[0].type == 'setup') else None
    
    @property
    def teardown(self):
        return self[-1] if (self and self[-1].type == 'teardown') else None
    

    注意 0 和 -1 的指示并结合类型检查。

    【讨论】:

    • 感谢您的快速回复和额外的解释!您是绝对正确的 - 将拆解调用更改为已定义关键字的末尾可以解决此问题。看来我还能留半头头发。 :)
    • 很高兴能提供帮助。仅供参考,在下一个版本中,您似乎可以通过上述属性设置设置和拆卸 - github.com/robotframework/robotframework/commit/…
    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2021-07-13
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多