exception with context manager: raises marking test functions with attributes: markers monkeypatching/mocking modules and environments with fixture: monkeypatch many types of fixtures… Some cool features
def get_queryset(self): """ Return the last five published questions (not including those set to be published in the future). """ return Question.objects.filter( pub_date__lte=timezone.now()).order_by('-pub_date')[:5] views.py $ git checkout 44bd202
the given `question_text` and published the given number of `days` offset to now (negative for questions published in the past, positive for questions that have yet to be published). """ time = timezone.now() + datetime.timedelta(days=days) return Question.objects.create(question_text=question_text, pub_date=time) class QuestionMethodTests(TestCase): def test_was_published_recently_with_future_question(self): """ was_published_recently() should return False for questions whose pub_date is in the future. """ time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) self.assertEqual(future_question.was_published_recently(), False) class QuestionViewTests(TestCase): def test_index_view_with_a_past_question(self): """ Questions with a pub_date in the past should be displayed on the index page. """ create_question(question_text="Past question.", days=-30) response = self.client.get(reverse('polls:index')) self.assertQuerysetEqual( response.context['latest_question_list'], ['<Question: Past question.>'] ) tests.py $ git checkout 44bd202
manage.py migrate $ python manage.py test Creating test database for alias 'default'... ........ ---------------------------------------------------- Ran 8 tests in 0.069s OK Destroying test database for alias 'default'...
a specific directory $ py.test -k test_foo Run all the tests cases that are named *test_foo* $ py.test —-durations=4 Run tests and show 4 slowest setup/test durations (0 for all) $ py.test —-n=4 Run tests in four parallel processses (plugin pytest-xdist) $ py.test —-reuse-db Reuse database from last run $ py.test —-reuse-db —-create-db Reuse database schema if models if changed Run tests…
should return False for questions whose pub_date is in the future. """ time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) self.assertEqual(future_question.was_published_recently(), False) def test_was_published_recently_with_recent_question(): time = timezone.now() - datetime.timedelta(hours=1) recent_question = Question(pub_date=time) assert recent_question.was_published_recently() is True testing with assert
def test_index_view_with_no_questions(self): """ If no questions exist, an appropriate message should be displayed. """ response = self.client.get(reverse('polls:index')) self.assertEqual(response.status_code, 200) self.assertContains(response, "No polls are available.") @pytest.mark.django_db def test_index_view_with_no_questions(client): response = client.get(reverse('polls:index')) assert response.status_code == 200 assert "No polls are available." in str(response.content) pytest-django & database
= instance of django.test.Client admin_client = instance of django.test.Client that is logged in as an admin user admin_user = instance of a superuser, with username “admin” and password “password” django_user_model = user model used by Django django_username_field = field name used for the username on the user model db = ensure the Django database is set up transactional_db = database including transaction support live_server = runs a live Django server in a background thread settings = provide a handle on the django settings module