0
0
PyTesttesting~20 mins

pytest-django for Django testing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
pytest-django Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'
ATest fails because assertion count is wrong
BTest fails because database access is not allowed
CTest raises AttributeError due to missing model
DTest passes successfully
Attempts:
2 left
💡 Hint
Remember to use @pytest.mark.django_db to allow database access in tests.
assertion
intermediate
1: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?
Aassert response.status_code == 200
Bassert response.code == 200
Cassert response.status == 200
Dassert response.status_code is '200'
Attempts:
2 left
💡 Hint
Check the attribute name for HTTP status code in Django test responses.
🔧 Debug
advanced
2: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
ATest fails because User model is not imported
BUser creation fails because password field is required and missing
CTest fails because @pytest.mark.django_db is missing
DUser creation fails because username is not unique
Attempts:
2 left
💡 Hint
Check required fields for Django's User model when creating users.
framework
advanced
1: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?
Adjango_client
Bdb_client
Cclient
Dtransactional_db_client
Attempts:
2 left
💡 Hint
pytest-django provides a built-in fixture for HTTP client with DB access.
🧠 Conceptual
expert
2: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?
ATest raises RuntimeError about database access not allowed
BTest runs successfully but database changes are not saved
CTest silently skips database queries
DTest raises django.core.exceptions.ImproperlyConfigured error
Attempts:
2 left
💡 Hint
pytest-django restricts database access unless explicitly allowed.