I'm writing test cases for my Django-based rest APIs. The problem is in the method of testcase class. I want to use test_admin_login method in the setuptestdata method. And I'm unable to do that. I'm getting error that I mentioned in the title.
class MyTests(TestCase):
allow_database_queries: True
client = APIClient()
@classmethod
def setUpTestData(cls):
# client = APIClient()
# AUTHENTICATION
# response = cls.client.post(reverse('auth'),data={"username":"admin","password":"ppoopp00"},format='json')
value = cls.test_admin_login()
print(value)
cls.auth_token = cls.test_admin_login(cls)
# CREATE PROJECT
response = cls.client.post(
reverse('projects:project_create'),
data={"platform_subscriber_entity":"NucoinTest"},
format='json',
HTTP_AUTHORIZATION="JWT " cls.auth_token
)
cls.project_key = response.data["platform_subscriber_id"]
def test_admin_login(self):
"""
Ensure we can create a new account object.
"""
url = reverse('auth')
response = self.client.post(url,data={"username":"admin","password":"testpass"},format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 2)
return response.data["access"]
CodePudding user response:
if any function needs the self
this means you need to create an instance of it first to use it and you use cls
not an instance so all you need is to create an instance
cls().test_admin_login(cls)