nose或pytest的插件,可以自动重新运行flaky测试。
flak的Python项目详细描述
关于
flaky是nose或pytest的插件,可以自动重新运行flaky测试。
理想情况下,测试可靠地通过或失败,但有时测试夹具必须依赖于不是100%的组件 可靠。使用flaky,而不是删除这些测试或将它们标记为@skip,它们可以自动 已重试。
有关flaky的更多信息,请参见this presentation。
标记测试片状
要将测试标记为flaky,只需导入flaky并用@flaky:
fromflakyimportflaky
@flakydeftest_something_that_usually_passes(self):value_to_double=21result=get_result_from_flaky_doubler(value_to_double)self.assertEqual(result,value_to_double*2,'Result doubled incorrectly.')
默认情况下,flaky将重试一次失败的测试,但可以通过将值传递给 片状装饰。它接受两个参数:max_runs和min_passes;flaky将运行最多max_runs次的测试,直到 它已经成功地超越了时间。一旦测试通过了min_passes times,就被认为是成功的;一旦 运行max_运行次而不通过min_passes次,这被视为失败。
@flaky(max_runs=3,min_passes=2)deftest_something_that_usually_passes(self):"""This test must pass twice, and it can be run up to three times."""value_to_double=21result=get_result_from_flaky_doubler(value_to_double)self.assertEqual(result,value_to_double*2,'Result doubled incorrectly.')
标记类薄片
除了标记单个测试碎片外,还可以标记整个测试用例碎片:
@flakyclassTestMultipliers(TestCase):deftest_flaky_doubler(self):value_to_double=21result=get_result_from_flaky_doubler(value_to_double)self.assertEqual(result,value_to_double*2,'Result doubled incorrectly.')@flaky(max_runs=3)deftest_flaky_tripler(self):value_to_triple=14result=get_result_from_flaky_tripler(value_to_triple)self.assertEqual(result,value_to_triple*3,'Result tripled incorrectly.')
@flaky class decorator会将test_flaky_doubler标记为flaky,但它不会覆盖3个max_运行 对于test_flaky_tripler(来自该测试方法上的decorator)。
Pytest标记
使用pytest时,可以使用@pytest.mark.flaky代替@flaky。
不要重新运行某些类型的故障
根据你的测试,有些失败显然不是由于片状的。而不是重新运行 在这些失败之后,您可以指定一个过滤器函数,它可以告诉flaky立即使测试失败。
defis_not_crash(err,*args):returnnotissubclass(err[0],ProductCrashedError)@flakydeftest_something():raiseProductCrashedError@flaky(rerun_filter=is_not_crash)deftest_something_else():raiseProductCrashedError
flaky将运行test_something两次,但只运行test_something_else一次。
它还可用于在两次测试重试之间引起延迟:
importtimedefdelay_rerun(*args):time.sleep(1)returnTrue@flaky(rerun_filter=delay_rerun)deftest_something_else():...
激活插件
像任何鼻子插件一样,flaky可以通过命令行激活:
nosetests --with-flaky
使用pytest,flaky将自动运行。但是,可以通过命令行禁用它:
pytest -p no:flaky
命令行参数
无片状报告
通过--no-flaky-report可在运行结束时取消显示详细显示片状测试结果的报告。
较短的片状报告
通过--no-success-flaky-report来抑制有关成功的片状测试的信息。
强制剥落
通过--force-flaky将所有测试视为片状。
传递--max-runs=MAX_RUNS和/或--min-passes=MIN_PASSES以控制flaky if ^{tt8}的行为$ 已指定。单个测试上的片状装饰器将覆盖这些默认值。
其他用法示例在代码中-请参见test/test_nose/test_nose_example.py和test/test_pytest/test_pytest_example.py
安装
要安装,只需:
pip install flaky
兼容性
Flaky使用以下测试运行程序和选项进行测试:
- 鼻子检查。博士不能被标记为片状的。
- py.测试。使用pytest-xdist,但不使用--boxed选项。博士不能被标记为片状的。
贡献
设置
创建虚拟环境并安装软件包-
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 http://www.apache.org/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.