您的位置:首页 > 编程语言 > Python开发

Python - 生成Robot Framework 类

2016-07-19 20:21 549 查看
Python - 生成Robot Framework 类
Max.Bai
2016-07-19

Python - 生成Robot Framework 类

Max.Bai

2016-07-19

最近在写一个web版的RIDE,用到了python执行robot脚本的问题,现做一个记录

直接创建TestSuit类

用python脚本生成Robot Framework 里面的TestSuite,并执行

from robot.api import TestSuite
ts = TestSuite('test')
ts.resource.imports.library('Selenium2Library')
ts.resource.imports.resource('d:/temp/flows.robot')
tc = ts.tests.create('case')
tc.keywords.create('Open Browser', args=[u'url=http://www.baidu.com', u'browser=gc'])
tc.keywords.create('Go to', args=[u'url=http://tieba.baidu.com'])
tc.keywords.create(u'\u5546\u54c1\u8be6\u60c5\u9875\u968f\u673a\u9009\u62e9\u5546\u54c1', args=[])
ts.run()

文件转TestSuite

也可以从robot 文件生产TestSuite

from robot.api import TestSuiteBuilder

suite = TestSuiteBuilder().build('path/to/activate_skynet.robot')

执行结果

run方法可以返回结果,也可以读取一些执行的用例,用例状态等结果

result = suite.run(critical='smoke', output='skynet.xml')

assert result.return_code == 0
assert result.suite.name == 'Activate Skynet'
test = result.suite.tests[0]
assert test.name == 'Should Activate Skynet'
assert test.passed and test.critical
stats = result.suite.statistics
assert stats.critical.total == 1 and stats.critical.failed == 0


当然这样结果里面的内容还是有限的,特别是没有关键字执行的message,那么就需要导入另外一个类ExecutionResult

from robot.result import ExecutionResult

result = ExecutionResult(outputxml)
for case in result.suite.tests:
for kw in case.keywords:
print kw.name, kw.messages, kw.passed, kw.starttime)


生成Html Report

rom robot.api import ResultWriter

# Report and xUnit files can be generated based on the result object.
ResultWriter(result).write_results(report='skynet.html', log=None)
# Generating log files requires processing the earlier generated output XML.
ResultWriter('skynet.xml').write_results()

参考:https://robot-framework.readthedocs.io/en/3.0/autodoc/robot.running.html#examples
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息