Django Rest Framework test fails or passes, depending on number of functions in APITestCase class

8 views
Skip to first unread message

Conor

unread,
Oct 24, 2019, 10:28:49 AM10/24/19
to Django users
Hei all,

I have an issue with a test class (APITestCase) where a function can pass the test if it is the only function within the class, or fail, if it is one of two or more functions.

I have put this question on Stack Overflow where it is available with nicer formatting. 


If I run the following, the test will succeed.

class AudioTests(APITestCase):
    # setup a test user and a factory
    # factory = APIRequestFactory()
    test_user = None

    @classmethod
    def setUpTestData(cls):
        importer()  # imports data into the database
        cls.test_user = User(username='jim', password='monkey123', email='j...@jim.com')
        cls.test_user.save()


    def test_audio_retrieve(self):
        """
            Check that audio returns a 200 OK
        """
        factory = APIRequestFactory()
        view = AudioViewSet.as_view(actions={'get': 'retrieve'})

        # Make an authenticated request to the view...
        request = factory.get('/api/v1/audio/')

        # force refresh of user
        self.test_user.refresh_from_db()
        user = User.objects.get(username='jim')
        
        force_authenticate(request, user=user)
        response = view(request, pk="2")

        self.assertContains(response, 'audio')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

But if I add a second method as show in the code below, the newly added test_audio_list() will pass, but the previously passing test_audio_retrieve() will fail with a 404.

class AudioTests(APITestCase):
    # setup a test user and a factory
    # factory = APIRequestFactory()
    test_user = None

    @classmethod
    def setUpTestData(cls):
        importer()  # imports data into the database
        cls.test_user = User(username='jim', password='monkey123', email='j...@jim.com')
        cls.test_user.save()

    def test_audio_list(self):
        """
            Check that audo returns a 200 OK
        """
        factory = APIRequestFactory()
        view = AudioViewSet.as_view(actions={'get': 'list'})

        # Make an authenticated request to the view...
        request = factory.get('/api/v1/audio/')

        self.test_user.refresh_from_db()
        force_authenticate(request, user=self.test_user)
        response = view(request, pk="1")

        self.assertContains(response, 'audio/c1ha')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_audio_retrieve(self):
        """
            Check that audio returns a 200 OK
        """
        factory = APIRequestFactory()
        view = AudioViewSet.as_view(actions={'get': 'retrieve'})

        # Make an authenticated request to the view...
        request = factory.get('/api/v1/audio/')

        # force refresh of user
        self.test_user.refresh_from_db()
        user = User.objects.get(username='jim')
        
        force_authenticate(request, user=user)
        response = view(request, pk="2")

        self.assertContains(response, 'audio')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

I'm going slightly crackers trying to find out what's going run, but I am completely out of ideas. Any light anyone can shed on this is much appreciated.

Cheers,

Conor
Reply all
Reply to author
Forward
0 new messages