3

i'm currently testing my django (2.1.0) application with some api-unit-tests. I used the django rest framework(3.9.0) to build a login. For this i use code like this:

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)

The tests are running when i use:

coverage run --source='.' manage.py test myapp

like you see:

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'...

But when i do coverage report i just get

myapp/auth.py 72 46 36%

Despite the fact my api test uses the code in my auth.py. My auth.py looks like this:


    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)

But coverage says

return JsonResponse({'error': 'Other Methods than GET not allowed'}, status=405) will never be used.

Do you have an idea?

Sammy J
  • 1,048
  • 10
  • 28
user50548
  • 81
  • 7

1 Answers1

2

I found an answer. I wrote wrong tests. Django provides his own test tools and you should use them! And dont write something with requests cause coverage will identify your code in for example auth.py as not used cause your tests are not with the django test tools

user50548
  • 81
  • 7