Challenge - 5 Problems
pytest-django Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pytest-django test?
Consider this Django model and pytest-django test. What will be the test result?
PyTest
from django.db import models class Book(models.Model): title = models.CharField(max_length=100) import pytest @pytest.mark.django_db def test_book_creation(): book = Book.objects.create(title='Test Driven Development') assert Book.objects.count() == 1 assert book.title == 'Test Driven Development'
Attempts:
2 left
💡 Hint
Remember to use @pytest.mark.django_db to allow database access in tests.
✗ Incorrect
The test uses @pytest.mark.django_db which enables database access. The Book is created and counted correctly, so assertions pass.
❓ assertion
intermediate1:30remaining
Which assertion correctly checks the response status code in pytest-django?
You have a Django view that returns HTTP 200. Which assertion correctly verifies this in a pytest-django test?
PyTest
import pytest from django.urls import reverse @pytest.mark.django_db def test_homepage(client): url = reverse('home') response = client.get(url) # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Check the attribute name for HTTP status code in Django test responses.
✗ Incorrect
The correct attribute is response.status_code which holds an integer HTTP status code. Using '==' compares values correctly.
🔧 Debug
advanced2:30remaining
Why does this pytest-django test raise an error?
This test tries to create a Django user but raises an error. Identify the cause.
PyTest
import pytest from django.contrib.auth.models import User @pytest.mark.django_db def test_create_user(): user = User.objects.create(username='tester') assert User.objects.count() == 1 assert user.username == 'tester' # Error: django.db.utils.IntegrityError: NOT NULL constraint failed: auth_user.password
Attempts:
2 left
💡 Hint
Check required fields for Django's User model when creating users.
✗ Incorrect
Django's User model requires a password field. Creating a user without password causes IntegrityError due to NOT NULL constraint.
❓ framework
advanced1:30remaining
Which pytest-django fixture provides a test client with database access?
You want to test Django views with database access in pytest. Which fixture should you use?
Attempts:
2 left
💡 Hint
pytest-django provides a built-in fixture for HTTP client with DB access.
✗ Incorrect
The 'client' fixture in pytest-django provides a Django test client with database access enabled when @pytest.mark.django_db is used.
🧠 Conceptual
expert2:00remaining
What happens if you omit @pytest.mark.django_db in a test that accesses the database?
In pytest-django, what is the result of running a test that queries the database but does NOT have @pytest.mark.django_db?
Attempts:
2 left
💡 Hint
pytest-django restricts database access unless explicitly allowed.
✗ Incorrect
pytest-django raises a RuntimeError if a test accesses the database without @pytest.mark.django_db to prevent accidental DB usage.