测试失败:匹配查询不存在

2024-03-28 16:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了一整天的时间试图找出这个错误可能来自哪里,但是没有成功。 我的测试功能如下:

def test_match_data_while_updating(self):
    # TST N.1 : Match is live
    # -------     
    match_updated_data1 = {
        'match': {
            'id': 1,
            'status': 'not_started',
        },
    }
    match1 = Match.objects.get(id=1)
    request = self.__class__.factory.put('', match_updated_data1, format='json')        
    add_authentication_to_request(request, is_staff=True)
    response = update_match_video(request)
    self.assertEqual(Match.objects.get(id=1).status,'live')

这是我正在测试的功能的相关部分:

^{pr2}$

当我使用命令时:

$ python server/manage.py test --settings=skill.settings_test api.views.tests.test_views.ViewsTestCase

要运行文件/views/tests/test_views包含的所有测试函数(此文件仅包含一个类,即ViewsTestCase和多个测试),所有测试都将成功,但当我使用以下方法运行位于文件夹/api/views中的所有测试时:

$ python server/manage.py test --settings=skill.settings_test api.views 

我得到以下错误:

ERROR: test_match_data_while_updating (api.views.tests.test_views.ViewsTestCase)

Traceback (most recent call last):

File "/home/yosra/Bureau/app/master/server/api/views/tests/test_views.py", line 226, in test_match_data_while_updating match1 = Match.objects.get(id=1)

File "/home/yosra/Bureau/app/master/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/home/yosra/Bureau/app/master/venv/lib/python3.6/site-packages/django/db/models/query.py", line 399, in get self.model._meta.object_name

api.models.match.Match.DoesNotExist: Match matching query does not exist.

我的测试数据库不是空的,它包含一个id=1的匹配实例。也没有一个测试函数可以从我的数据库中删除这个元素。有谁能告诉我这可能是从哪里来的吗?在

谢谢你的帮助


Tags: pytestselfapiiddatagetsettings