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. 

https://stackoverflow.com/questions/58530256/django-rest-framework-tests-fail-and-pass-depending-on-number-of-functions

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='[email protected]')
        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='[email protected]')
        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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e3a91932-11a2-482d-a7ca-8bbaf460bb79%40googlegroups.com.

Reply via email to