我在 Django Rest Framework 中有一个测试类,其中包含一个函数,如果它是类中唯一的函数,则该函数通过测试;如果它是两个或多个函数之一,则该函数失败。
如果我按原样运行以下代码,即 def test_audio_list(self)
函数保持注释掉,则测试将根据两个断言语句通过。
如果我取消注释 def test_audio_list(self)
函数并运行测试,则 def test_audio_retrieve(self)
函数将失败并返回 404,而 def test_audio_list( self)
将会通过。
这是我的完整测试用例(已注释掉 def test_audio_list(self):
)。
class AudioTests(APITestCase):
# setup a test user and a factory
# factory = APIRequestFactory()
test_user = None
def setUp(self):
importer()
self.test_user = User(username='jim', password='monkey123', email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b3b0b499b3b0b4f7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>')
self.test_user.save()
# def test_audio_list(self):
# """
# Check that audo returns a 200 OK
# """
# factory = APIRequestFactory()
# view = AudioViewSet.as_view(actions={'get': 'list'})
#
# # Make an authenticated request to the view...
# request = factory.get('/api/v1/audio/')
# force_authenticate(request, user=self.test_user)
# response = view(request, pk="1")
#
# self.assertContains(response, 'audio/c1ha')
# self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_audio_retrieve(self):
"""
Check that audo returns a 200 OK
"""
factory = APIRequestFactory()
view = AudioViewSet.as_view(actions={'get': 'retrieve'})
# Make an authenticated request to the view...
request = factory.get('/api/v1/audio/')
force_authenticate(request, user=self.test_user)
response = view(request, pk="1")
self.assertContains(response, 'audio/c1ha')
self.assertEqual(response.status_code, status.HTTP_200_OK)
在上面运行python manage.py test
不会产生错误。但是,如果我取消注释 def test_audio_list(self)
函数,def test_audio_retrieve(self)
将失败,并显示以下内容:
======================================================================
FAIL: test_audio_retrieve (api.tests.AudioTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/code/api/tests.py", line 96, in test_audio_retrieve
self.assertContains(response, 'audio/c1ha')
File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 446, in assertContains
response, text, status_code, msg_prefix, html)
File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 418, in _assert_contains
" (expected %d)" % (response.status_code, status_code)
AssertionError: 404 != 200 : Couldn't retrieve content: Response code was 404 (expected 200)
注意。如果我更改类中函数的顺序,即将 def test_audio_retrieve(self)
放在 def test_audio_list(self)
之前,结果仍然是相同的。
我假设我一定在这里做错了什么,但我就是无法指出它。
非常感谢任何帮助。
干杯,
C
最佳答案
您多次使用“force_authenticate”,也许您可以在setUp中验证用户身份或使用refresh_from_db
请检查:注意:force_authenticate 直接将 request.user 设置为内存中的用户实例。如果您在更新已保存用户状态的多个测试中重复使用同一用户实例,则可能需要在测试之间调用refresh_from_db()。
https://www.django-rest-framework.org/api-guide/testing/
...
你能尝试一下合并函数吗,比如这样,
def test_audio(self):
"""
Check that audio returns a 200 OK
"""
factory = APIRequestFactory()
view_retrieve = AudioViewSet.as_view(actions={'get': 'retrieve'})
view_list = AudioViewSet.as_view(actions={'get': ‘list’})
# Make an authenticated request to the view...
request = factory.get('/api/v1/audio/')
force_authenticate(request, user=self.test_user)
# Retrieve
response = view_retrieve(request, pk="1")
self.assertContains(response, 'audio/c1ha')
self.assertEqual(response.status_code, status.HTTP_200_OK)
# List
response = view_list(request, pk="1")
self.assertContains(response, 'audio/c1ha')
self.assertEqual(response.status_code, status.HTTP_200_OK)
关于Django Rest Framework 测试失败或通过,具体取决于函数数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530256/