Getting error when running automated for login apiview with DRF

Okay, how should I fix this then? Should I remove some import statements?

Add the appropriate import statements to the file missing them.

Okay I will do that. Thanks

I thought I added all the right import statements. I tried following the DRF testing docs

This is closest import statement for User I could find from django.contrib.auth.models import User

If you are looking for additional assistance, you need to always post the complete code that you want confirmed. We cannot diagnose descriptions of what you have done.

I’m sorry, here it is:

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from django.contrib.auth.models import User
from rest_framework.test import APIClient


client = APIClient()
class UserLoginTests(APITestCase):

    def create_user(self, username, email=None, password=None, **extra_fields):
        user = User.objects.create_user('username', 'email', 'Password!87')
        self.assertTrue(self.client.login(username='username', password='Password!87'))
        response = self.client.get(reverse('login'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

For clarification this is the complete login test in test_login.py

Are you using the standard Django User model in your project, or a custom model? (Notice what your imports are in your other test file.)

Ahh. I think I see what you mean. I’m using Custom User. In the test_register.py I’m using the following import from ..models import User.

So change this maybe? They should both have to same imports depending on what type of User model I’m using

That relative reference is correct if both test cases are in the same directory. But if this file is in a different directory, then the reference would be to the app in which the model resides.

Okay I’ll change that and try again. Hopefully it works this time

Okay so I changed the import statement, here is the error I get

  show_sunset_warning()
System check identified no issues (0 silenced).
E.
======================================================================
ERROR: arborfindr.test.test_login (unittest.loader._FailedTest.arborfindr.test.test_login)
----------------------------------------------------------------------
ImportError: Failed to import test module: arborfindr.test.test_login
Traceback (most recent call last):
  File "/usr/lib64/python3.13/unittest/loader.py", line 396, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib64/python3.13/unittest/loader.py", line 339, in _get_module_from_name
    __import__(name)
    ~~~~~~~~~~^^^^^^
  File "/home/coreyj/Documents/ArborHub/MyProject/arborfindr/test/test_login.py", line 5, in <module>
    from rest_framework.test import APIClientfrom
ImportError: cannot import name 'APIClientfrom' from 'rest_framework.test' (/home/coreyj/Documents/ArborHub/MyProject/.venv/lib64/python3.13/site-packages/rest_framework/test.py)


----------------------------------------------------------------------
Ran 2 tests in 0.491s

FAILED (errors=1)
Destroying test database for alias 'default'...

For reference, here is my test_login.py

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from ..models import User
from rest_framework.test import APIClientfrom


client = APIClient()
class UserLoginTests(APITestCase):

    def create_user(self, username, email=None, password=None, **extra_fields):
        user = User.objects.create_user('username', 'email', 'Password!87')
        self.assertTrue(self.client.login(username='username', password='Password!87'))
        response = self.client.get(reverse('login'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

Here is my test_register.py

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from ..models import User


class UserRegisterTests(APITestCase):
    def test_register_user(self):
        url = reverse('arborfindr:register')
        data = {'email': 'myemail@mail.com', 'username': 'jonedoe12', 'password': 'mypassword123'}
        response = self.client.post(url, data, format='json')
        self.assertTrue(User.objects.filter(username='jonedoe12').exists())
        user = User.objects.get(username='jonedoe12')
        self.assertEqual(user.email, 'myemail@mail.com')
        self.assertTrue(user.check_password('mypassword123'))

Also I get a warning, stating that APIClient isn’t defined in test_login.py, this happened only after I changed the import statement.

So what is that error message telling you?

How does that relate to the other code in that file?

What do you think you might need to change?

Well I only got this warning after I changed the import statement in test_login.py, it’s related to Pylance.You’re right though, this has nothing to do with the other traceback I was getting.

But when I was using from django.contrib.auth.models import User I wasn’t getting this APIClient issue. So I’m not really sure

Yeah I don’t know what to change here other than the changes I’ve already made. I’ll keep trying to figure this out though.

Again, what is the specific error message you’re trying to resolve?

What information is that error message telling you?

This issue has nothing at all to do with any previous issue you’ve fixed before. Focus on the most recent message only.

So it was my mistake, I did correct the minor error for the APIClient import. However, to reiterate, I’m still seeing this output when I run python manage.py

  show_sunset_warning()
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.570s

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

So while this isn’t technically an error, it only shows that 1 test was ran and I’m currently in the test_login.py file. Since I have two different APITestCases in two different files, I just assumed that the output would show that 2 tests were running instead of 1.

I hope this clears things up for you.

So what is the current contents of that file that had the error? (A test won’t run if the code can’t be loaded and run.)

It was this content in test_login.py

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from ..models import User


class UserRegisterTests(APITestCase):
    def test_register_user(self):
        url = reverse('arborfindr:register')
        data = {'email': 'myemail@mail.com', 'username': 'jonedoe12', 'password': 'mypassword123'}
        response = self.client.post(url, data, format='json')
        self.assertTrue(User.objects.filter(username='jonedoe12').exists())
        user = User.objects.get(username='jonedoe12')
        self.assertEqual(user.email, 'myemail@mail.com')
        self.assertTrue(user.check_password('mypassword123'))
        ```
But let me run test_register.py and see what happens

So I ran the test on test_register.py and got exactly the same output as the test executed on test_login.py, at the end console, it read ‘Ok’.

Basically I want to make sure that I set up and both the login/register api tests properly. I’m sorry, I’m just new to testing API’s with automated methods