[Django]-Django REST framework APIClient authenticating in shell, but not in my unit test

3πŸ‘

βœ…

If you are running the test cases, it will auto create the database to execute test. In shell you already have database and user is there, it’s authenticating. So you need to create the user here and authenticate. Follow this code:

class RiskViewSetTest(unittest.TestCase):

    def setUp(self):
        self.client = APIClient()
        User.objects.create_user(
            username='test@test.us', password='realpassword')
    def testClientView(self):
        self.client.login(
            username='test@test.us',password='realpassword')
        response = self.client.get('/api/v1/risks/', format='json')
        self.assertTrue(response.status_code, 200)
πŸ‘€Seenu S

Leave a comment