我是靠谱客的博主 爱笑咖啡,最近开发中收集的这篇文章主要介绍python调用robotframework api_Robot框架API系列—在Python中重新运行失败,robotframeworkapi,python,实现,rerunfailed...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

前言

今天跟一素未谋面的网友讨论python调用robot framework api重新re-run-failure-test的问题, 很遗憾未能帮得上忙。

便抽空结合官方的例子写了个小demo, 下面一起来看看吧!

How to re-run failure tests of suite

Gather all failure tests

put those tests into new suite

run the suite

done

Simple demo

two test case of the suites

one pass, one fail

# example: https://robot-framework.readthedocs.io/en/latest/autodoc/robot.running.html#examples

from robot.api import TestSuite

from robot.api import ResultWriter

from robot.conf import RobotSettings

def create_suite(name=None):

return TestSuite(name)

def is_test_suite_obj(suite):

'''

check suite type

'''

if not suite or not isinstance(suite, TestSuite):

raise Exception("Only accepted TestSuite Object")

def import_libs_into_suite(suite, lib_name='OperatingSystem'):

# import library into suite

is_test_suite_obj(suite)

suite.resource.imports.library(lib_name)

def create_test(suite, name, tag='smoke'):

is_test_suite_obj(suite)

return suite.tests.create(name, tags=[tag])

def set_robot_options(name, path="allure-reports"):

options = {

"output": "{}-output.xml".format(name),

"log": "{}-log.html".format(name),

"report": "{}-reporter.html".format(name),

"outputdir": path,

}

return RobotSettings(options)

def run(suite, output_name):

is_test_suite_obj(suite)

settings = set_robot_options(output_name)

result = suite.run(settings, critical='smoke')

ResultWriter(settings.output if settings.log

else result).write_results(

report=settings.report, log=settings.log)

return result

test_suite = create_suite('set failure test')

import_libs_into_suite(test_suite)

test = create_test(test_suite, 'Set Environment Variable')

test.keywords.create('Set Environment Variable', args=[

'SKYNET', 'activated'], type='setup')

test.keywords.create('Environment Variable Should Be Set', args=['SKYNET'])

failed_test = create_test(test_suite, 'Set failure testcase')

failed_test.keywords.create('Should Be Equal', args=[

1, 2])

result = run(test_suite, 'all-results')

Gather all failure tests

Gather all failure tests from result

# Gather all tests which are not passed

# trigger: use dir(result), dir(result.suite)

tests = [item.longname for item in result.suite.tests if not item.passed]

Put all failure tests into new suite

# trigger: cannot append test of the result

# but can append test of the suite

rerun_failed_suite = create_suite('Rerun failure test')

import_libs_into_suite(rerun_failed_suite)

for index, item in enumerate(test_suite.tests):

if item.longname in tests:

rerun_failed_suite.tests.append(item)

The whole demo

from robot.api import TestSuite

from robot.api import ResultWriter

from robot.conf import RobotSettings

def create_suite(name=None):

return TestSuite(name)

def is_test_suite_obj(suite):

'''

check suite type

'''

if not suite or not isinstance(suite, TestSuite):

raise Exception("Only accepted TestSuite Object")

def import_libs_into_suite(suite, lib_name='OperatingSystem'):

# import library into suite

is_test_suite_obj(suite)

suite.resource.imports.library(lib_name)

def create_test(suite, name, tag='smoke'):

is_test_suite_obj(suite)

return suite.tests.create(name, tags=[tag])

def set_robot_options(name, path="allure-reports"):

options = {

"output": "{}-output.xml".format(name),

"log": "{}-log.html".format(name),

"report": "{}-reporter.html".format(name),

"outputdir": path,

}

return RobotSettings(options)

def run(suite, output_name):

is_test_suite_obj(suite)

settings = set_robot_options(output_name)

result = suite.run(settings, critical='smoke')

ResultWriter(settings.output if settings.log

else result).write_results(

report=settings.report, log=settings.log)

return result

test_suite = create_suite('set failure test')

import_libs_into_suite(test_suite)

test = create_test(test_suite, 'Set Environment Variable')

test.keywords.create('Set Environment Variable', args=[

'SKYNET', 'activated'], type='setup')

test.keywords.create('Environment Variable Should Be Set', args=['SKYNET'])

failed_test = create_test(test_suite, 'Set failure testcase')

failed_test.keywords.create('Should Be Equal', args=[

1, 2])

result = run(test_suite, 'all-results')

tests = [item.longname for item in result.suite.tests if not item.passed]

rerun_failed_suite = create_suite('Rerun failure test')

import_libs_into_suite(rerun_failed_suite)

for index, item in enumerate(test_suite.tests):

if item.longname in tests:

rerun_failed_suite.tests.append(item)

run(rerun_failed_suite, 'rerun-results')

最后

以上就是爱笑咖啡为你收集整理的python调用robotframework api_Robot框架API系列—在Python中重新运行失败,robotframeworkapi,python,实现,rerunfailed...的全部内容,希望文章能够帮你解决python调用robotframework api_Robot框架API系列—在Python中重新运行失败,robotframeworkapi,python,实现,rerunfailed...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部