对 AssertionError 重复测试 [英] Repeat a test upon an AssertionError

查看:54
本文介绍了对 AssertionError 重复测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个模块 pytest-repeat 可用于重复执行pytest N 次(整个测试套件).

There is the module pytest-repeat which can be used to repeat the execution of pytest N times (the whole test suite).

但是,我想在出现 AssertionError 时再次重新运行该特定测试,而不是运行整个套装.那么,如何捕获 AssertionError 并以编程方式调用相同的测试函数?

However, I want to, upon an AssertionError, re-run that specific test again, instead of running the whole suit. So, how can I capture the AssertionError and programmatically call the same test function?

推荐答案

flaky package 提供了一个 @flaky 装饰器.来自文档:

The flaky package provides a @flaky decorator. From the documentation:

@flaky(max_runs=3, min_passes=2)
def test_something_that_usually_passes(self):
    """This test must pass twice, and it can be run up to three times."""
    value_to_double = 21
    result = get_result_from_flaky_doubler(value_to_double)
    self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')

这篇关于对 AssertionError 重复测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆