【发布时间】: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
【问题讨论】: