[Django]-How to properly test Django API ListView with PyTest?

1👍

Okay, apparently I’m an idiot and problem was totally different. My URL didn’t work because of / in the end. Changed it to:

response = client.get(path='/api/events',
                          format='json')

and it works just fine. No idea why I didn’t catch that before, I was running through the whole app with Pycharm debugger and the view was executed.

To be honest after reading the docs I’m quite surprised my fixture works. According to the docs I should add db fixture to my fixture, but it works just fine without it.

Thanks for help and sorry for wasting your time.

👤MKaras

1👍

This is a very simple example. But maybe you can make it work like this:

from rest_framework.test import APITransactionTestCase
from rest_framework.test import status

class TestThis(APITransactionTestCase):
    def test_this(self):
        data = {"key": "value"}
        response = self.client.post("/api/resource", data=data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

I hope it helps.

👤luistm

Leave a comment