Python 覆盖率显示Django测试的百分比太低
我目前正在用一些api单元测试测试我的django(2.1.0)应用程序。我使用django rest框架(3.9.0)构建登录。为此,我使用如下代码:Python 覆盖率显示Django测试的百分比太低,python,django,code-coverage,python-unittest,Python,Django,Code Coverage,Python Unittest,我目前正在用一些api单元测试测试我的django(2.1.0)应用程序。我使用django rest框架(3.9.0)构建登录。为此,我使用如下代码: def logout(request): if request.method == 'GET': request.user.auth_token.delete() return JsonResponse({'message':'You are sucessfully lo
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)
类LogoutTest(APITestCase):
def测试\登录\发布\未授权(自我):
response=requests.post('http://127.0.0.1:8000/myapp/user_info/')
self.assertEqual(response.status\u代码,401)
def测试\登录\放置\验证(自我):
令牌=auth()
有效负载={'Authorization':'Token'+Token}
response=请求。put('http://127.0.0.1:8000/myapp/user_info/,标头=有效负载)
self.assertEqual(response.status_代码,405)
def测试\登录\删除\身份验证(自我):
令牌=auth()
有效负载={'Authorization':'Token'+Token}
响应=请求。删除('http://127.0.0.1:8000/myapp/user_info/,标头=有效负载)
self.assertEqual(response.status_代码,405)
当我使用以下命令时,测试正在运行:
coverage run--source='.'manage.py test 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)
但是报道说
returnjsonresponse({'error':'Other Methods than GET not allowed',status=405)将永远不会被使用。
你有什么想法吗?我找到了答案。我写了错误的测试。Django提供了自己的测试工具,您应该使用它们!不要编写带有请求的内容,因为覆盖率会在例如auth.py中标识您的代码为未使用,因为您的测试没有使用django测试工具