mercredi 1 juillet 2020

django test client CRUD API, unit testing

I want to write pure unit tests for my CRUD APP in django. Everyone use django tests or pytests, but I am really confused. Should I use test client to testing eg. POST method? Unit tests should be isolate and class should be test independently. We should use mocks DB etc.

So why this code from https://medium.com/@ksarthak4ever/test-driven-development-tdd-in-django-and-django-rest-framework-drf-a889a8068cb7 is unit tests?

class PrivateIngredientsApiTest(TestCase): #Test the private ingredients api
 
 def setUp(self):
  self.client = APIClient()
  self.user = get_user_model().objects.create_user(
   'ksarthak4ever@gmail',
   'hakunamatata'
  )
  self.client.force_authenticate(self.user)

 def test_retrieve_ingredients_list(self): #Test retrieving a list of ingredients
  Ingredient.objects.create(user=self.user, name='Potato')
  Ingredient.objects.create(user=self.user, name='Peas')
  res = self.client.get(INGREDIENTS_URL)ingredients = Ingredient.objects.all().order_by('-name')
  serializer = IngredientSerializer(ingredients, many=True)
  self.assertEqual(res.status_code, status.HTTP_200_OK)
  self.assertEqual(res.data, serializer.data)

For me this is functional test, not unit test. I search all over the internet how to write unit tests for django, but I didn't find anything.

Aucun commentaire:

Enregistrer un commentaire