gpt4 book ai didi

python - 在 TestCase 中全部运行时,Django 中的模拟测试不起作用,但一个接一个运行良好

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:05 24 4
gpt4 key购买 nike

在 Django 中测试 View 时,我使用 python mock 来修补一些函数和类。

如果我独立运行每个测试,所有测试都有效。但是当我运行 TestCase 时,一些测试不工作(补丁没有生效)。

class ViewsTest(TestCase):
@mock.patch('extras.utils.get_user_category')
def test_select_test(self, mock_method):
mock_method.return_value = Category(id=1, name="Foo")

response = self.client.post(reverse('select_test', args=['Foo']))

self.assertEqual(200, self.client.post(reverse('select')).status_code)

@mock.patch('user_profile.models.Profile.categories')
def test_category_view(self, mock_related):
mock_related.all.return_value = []

self.assertEqual(200, self.client.post(reverse('category')).status_code)

我有一个 print int View 来查看每个模拟方法,当它工作时它会打印:

MagicMock name='get_user_category' id='162815756'

什么时候不起作用,我看到:

function get_user_category at 0x8e0fb8c

我尝试了修补程序 start() 和 stop() 但我仍然有问题。

¿问题是什么?

最佳答案

我遇到了同样的问题。您可能指向错误的方向。

检查这个link .它帮助了我。

关于python - 在 TestCase 中全部运行时,Django 中的模拟测试不起作用,但一个接一个运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20271383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com