Python unittest: Retry on failure with Nose?

岁酱吖の 提交于 2019-12-06 10:47:59

By using a generator, you're giving nose maxAttempts tests to run. if any of them fail, the suite fails. The try/catch doesn't particularly apply to the tests your yielding, since its nose that runs them. Rewrite your test like so:

def test_something(self):
    maxAttempts = 3
    func = self.run_something

    attempt = 1
    while True:
        if attempt == maxAttempts:
            func() # <<<--------
            break

        else:
            try:
                func() # <<<--------
                break
            except:
                attempt += 1

def run_something(self):
    #Do stuff

You can use attributes on your functions with the flaky nose plugin that will automatically re-run tests and let you use advanced parameters (like if 2 in 3 test pass, then it's a pass)

GitHub flaky project

How to install Flaky plugin for Python:

pip install flaky

Example nose test runner configuration:

nosetests.exe your_python_tests.py --with-flaky --force-flaky --max-runs=3

Example Python code with function marked with Flaky attribute:

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.')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!