使用robot.api时测试用例拆解不执行,但在使用pybot runner

时间:2016-06-29 22:11:53

标签: python-2.7 robotframework

背景

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

问题摘要

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

我的问题是:为什么我的测试用例拆解在使用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

1 个答案:

答案 0 :(得分:1)

这对我来说听起来像个错误。或者也许是一个无证的怪癖。必须添加 FIRST 设置关键字,并且必须添加拆解最后

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')

我通过查看机器人/ 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的指示以及类型检查。