1 of 8

Pytest Django

2 of 8

Django testing framework 

  • “Inherits” from unittest
  • Tests are organized in django.test.TestCase classes
  • Each method uses various assert*  methods to verify � expected outputs

3 of 8

4 of 8

  • Create test databases for our test run
  • For each test creates a transaction so that each test can be rolled back after the test case.
  • Set up utilities such as testing client

Under the hood:�django does some crazy stuff on top of the unittest.�so when we use TestCase class…

5 of 8

  • Nothing really, it gets the job done
  • A lot of boilerplate code (test classes, weird assert methods)
  • If you are running tests which don’t use DB you are wasting resources.
  • Composition over inheritance

What’s wrong with django

testing framework?

6 of 8

�Why use pytest-django instead of � Django's testing framework?

  • Nothing really, it gets the job done
  • A lot of boilerplate code (test classes, weird assert methods)
  • If you are running tests which don’t use DB you are wasting resources.
  • Composition over inheritance

7 of 8

@pytest.mark.django_db

  • Just like in django.test.TestCaseTo get access to the Django test database
  • Each test will run in its own transaction that will be rolled back at the end of the test.�

We use it for each request to our app�Just like in in django.test.client�pytest-django has a build-in fixture client

Client

8 of 8