pytest插件,可自动重跑不稳定的测试。
项目描述
关于
Flaky是一个pytest插件,可自动重跑不稳定的测试。
理想情况下,测试可以可靠地通过或失败,但有时测试用例必须依赖那些并非100%可靠的组件。使用flaky(不稳定),而不是删除这些测试或标记为@skip,它们可以自动重试。
有关flaky的更多信息,请参阅此演示文稿。
标记测试为不稳定
要将测试标记为不稳定,只需导入flaky并使用@flaky装饰器修饰测试。
from flaky import flaky
@flaky
def test_something_that_usually_passes(self):
value_to_double = 21
result = get_result_from_flaky_doubler(value_to_double)
self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')
默认情况下,flaky将重试失败的测试一次,但可以通过传递值给flaky装饰器来覆盖此行为。它接受两个参数:max_runs(最大运行次数)和min_passes(最小通过次数);flaky将运行测试,直到成功min_passes次。一旦测试成功min_passes次,它就被视为成功;一旦运行max_runs次而没有成功min_passes次,它就被视为失败。
@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.')
标记类为不稳定
除了标记单个测试为不稳定外,整个测试用例也可以标记为不稳定
@flaky
class TestMultipliers(TestCase):
def test_flaky_doubler(self):
value_to_double = 21
result = get_result_from_flaky_doubler(value_to_double)
self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')
@flaky(max_runs=3)
def test_flaky_tripler(self):
value_to_triple = 14
result = get_result_from_flaky_tripler(value_to_triple)
self.assertEqual(result, value_to_triple * 3, 'Result tripled incorrectly.')
@flaky类装饰器将test_flaky_doubler标记为不稳定,但它不会覆盖test_flaky_tripler(在该测试方法上的装饰器)的3 max_runs。
Pytest标记
当使用pytest时,可以使用@pytest.mark.flaky代替@flaky。
不重新运行某些类型的失败
根据您的测试,一些失败显然并非由于不稳定。在这些失败之后,您可以为flaky指定一个过滤器函数,使其立即失败测试。
def is_not_crash(err, *args):
return not issubclass(err[0], ProductCrashedError)
@flaky
def test_something():
raise ProductCrashedError
@flaky(rerun_filter=is_not_crash)
def test_something_else():
raise ProductCrashedError
flaky将test_something运行两次,但只运行test_something_else一次。
它还可以用于在测试重试之间引入延迟
import time
def delay_rerun(*args):
time.sleep(1)
return True
@flaky(rerun_filter=delay_rerun)
def test_something_else():
...
激活插件
使用pytest时,flaky将自动运行。但是,可以通过命令行禁用它
pytest -p no:flaky
命令行参数
无flaky报告
传递--no-flaky-report以在运行结束时抑制详细flaky测试结果的报告。
简短的flaky报告
传递--no-success-flaky-report以抑制关于成功flaky测试的信息。
强制flaky
传递--force-flaky将所有测试视为不稳定。
传递--max-runs=MAX_RUNS和/或--min-passes=MIN_PASSES以控制当指定--force-flaky时的flaky行为。单个测试上的flaky装饰器将覆盖这些默认值。
其他使用示例在代码中 - 请参阅test/test_pytest/test_pytest_example.py
安装
安装,只需
pip install flaky
兼容性
Flaky与以下测试运行器和选项进行了测试
Py.test。与pytest-xdist兼容,但不能与--boxed选项一起使用。无法标记doctests为不稳定。
贡献
请参阅CONTRIBUTING.rst。
设置
创建虚拟环境和安装包 -
mkvirtualenv flaky
pip install -r requirements-dev.txt
测试
使用-运行所有测试
tox
tox测试包括通过pycodestyle和pylint进行的代码风格检查。
版权和许可证
Copyright 2015 Box, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://apache.ac.cn/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
项目详细信息
下载文件
下载适用于您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。