Python 单元测试:Nose 失败时重试?

发布于 2024-12-01 17:40:58 字数 527 浏览 4 评论 0原文

我有一个随机失败的测试,我想让它在发送错误消息之前重试多次。

我将 python 与 Nose 一起使用。

我写了以下内容,但不幸的是,即使使用 try/ except 处理,当第一次尝试测试失败时,Nose 也会返回错误。

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

    attempt = 1
    while True:
        if attempt == maxAttempts:
            yield func
            break

        else:
            try:
                yield func
                break
            except:
                attempt += 1

def run_something(self):
    #Do stuff

谢谢

I have a test which randomly fails and I want to let it retry a number of times before sending an error message.

I'm using python with Nose.

I wrote the following, but unfortunately, even with the try/except handling, Nose returns an error when the test fails on the first try.

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

    attempt = 1
    while True:
        if attempt == maxAttempts:
            yield func
            break

        else:
            try:
                yield func
                break
            except:
                attempt += 1

def run_something(self):
    #Do stuff

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2024-12-08 17:40:58

您可以通过 flaky noose 插件 在函数上使用属性,该插件会自动重新运行测试并让您使用高级参数(比如如果三分之二的测试通过,那么就通过了)

GitHub flaky 项目

如何安装 Flaky Python 插件:

pip install flaky

鼻子测试运行器配置示例:

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

带有 Flaky 属性标记的函数的示例 Python 代码:

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.')

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.')
毁我热情 2024-12-08 17:40:58

通过使用生成器,您可以运行鼻子 maxAttempts 测试。如果其中任何一个失败,则套件就会失败。 try/catch 并不特别适用于您生成的测试,因为它是运行它们的鼻子。像这样重写你的测试:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文