gpt4 book ai didi

python - 覆盖率显示 Django 测试的百分比太低

转载 作者:行者123 更新时间:2023-11-28 16:59:44 25 4
gpt4 key购买 nike

我目前正在使用一些 api-unit-tests 测试我的 django (2.1.0) 应用程序。我使用 django rest framework(3.9.0) 构建了一个登录。为此,我使用这样的代码:

class LogoutTest(APITestCase):
def test_login_post_unauth(self):
response = requests.post('http://127.0.0.1:8000/myapp/user_info/')
self.assertEqual(response.status_code, 401)

def test_login_put_auth(self):
token = auth()
payload = {'Authorization': 'Token '+token}
response = requests.put('http://127.0.0.1:8000/myapp/user_info/', headers=payload)
self.assertEqual(response.status_code, 405)

def test_login_delete_auth(self):
token = auth()
payload = {'Authorization': 'Token '+token}
response = requests.delete('http://127.0.0.1:8000/myapp/user_info/', headers=payload)
self.assertEqual(response.status_code, 405)

当我使用时,测试正在运行:

覆盖运行 --source='.' manage.py 测试 myapp

如你所见:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....................................
----------------------------------------------------------------------
Ran 36 tests in 5.082s

OK
Destroying test database for alias 'default'...

但是当我做报道报告的时候我就得到了

myapp/auth.py 72 46 36%

尽管我的 api 测试使用了我的 auth.py 中的代码。我的 auth.py 看起来像这样:


def logout(request):
if request.method == 'GET':
request.user.auth_token.delete()
return JsonResponse({'message':'You are sucessfully logged out'}, status=200)
return JsonResponse({'error': 'Other Methods than GET not allowed'}, status=405)

但是报道说

return JsonResponse({'error': 'Other Methods than GET not allowed'}, status=405) 永远不会被使用。

你有什么想法吗?

最佳答案

我找到了答案。我写了错误的测试。 Django 提供了自己的测试工具,你应该使用它们!并且不要写一些带有请求的东西,因为覆盖率将在例如 auth.py 中识别您的代码未被使用,因为您的测试不是使用 django 测试工具

关于python - 覆盖率显示 Django 测试的百分比太低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55375488/

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