嘲笑时间。睡眠会导致测试失败

发布于 2025-02-07 06:53:41 字数 1061 浏览 1 评论 0原文

我写了这项测试,但是为了不延迟测试,我嘲笑时间。睡觉和测试将遇到失败。

from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase

class CommandsTest(TestCase):
    @patch('time.sleep', return_value=None)
    def test_wait_for_db(self):
        """Test waiting for db"""
        with patch('django.utils.connection.BaseConnectionHandler.__getitem__') as gi:
            gi.side_effect = [OperationalError] * 5 + [True]
            call_command('wait_for_db')
            self.assertEqual(gi.call_count, 6)

通过对第二行(@Patch)评论,该程序将正确运行。 这是错误:

ERROR: test_wait_for_db (core.tests.test_commands.CommandsTest)
Test waiting for db
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/unittest/mock.py", line 1369, in patched
    return func(*newargs, **newkeywargs)
TypeError: CommandsTest.test_wait_for_db() takes 1 positional argument but 2 were given

I wrote this test, but in order not to delay the test, I mock the time.sleep and the test will encounter an fail.

from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase

class CommandsTest(TestCase):
    @patch('time.sleep', return_value=None)
    def test_wait_for_db(self):
        """Test waiting for db"""
        with patch('django.utils.connection.BaseConnectionHandler.__getitem__') as gi:
            gi.side_effect = [OperationalError] * 5 + [True]
            call_command('wait_for_db')
            self.assertEqual(gi.call_count, 6)

By commenting on this second line(@patch), the program will run properly.
here is the error:

ERROR: test_wait_for_db (core.tests.test_commands.CommandsTest)
Test waiting for db
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/unittest/mock.py", line 1369, in patched
    return func(*newargs, **newkeywargs)
TypeError: CommandsTest.test_wait_for_db() takes 1 positional argument but 2 were given

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

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

发布评论

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

评论(1

终陌 2025-02-14 06:53:41

您需要将参数添加到test_wait_for_db中。
模拟函数作为参数传递给该功能,

class CommandsTest(TestCase):
    @patch('time.sleep', return_value=None)
    def test_wait_for_db(self, mocked_sleep):

由于您使用装饰器,因此在测试中将 然后可以测试确定是否确实被调用。更多信息。

You'll need to add an argument to your test_wait_for_db.
Since you use a decorator, the mocked function is passed as argument to that function

class CommandsTest(TestCase):
    @patch('time.sleep', return_value=None)
    def test_wait_for_db(self, mocked_sleep):

In your test you can then test assert if indeed was called. More information here.

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