Complete the code to import the pytest module for Django testing.
import [1]
We use import pytest to write tests with pytest and pytest-django.
Complete the code to mark a test function to use the Django database.
@pytest.mark.[1] def test_create_user(db): from django.contrib.auth.models import User user = User.objects.create(username='testuser') assert user.username == 'testuser'
The @pytest.mark.django_db decorator allows the test to access the Django database.
Fix the error in the test function to correctly use the Django test client fixture.
def test_homepage([1]): response = client.get('/') assert response.status_code == 200
The client fixture provides the Django test client for making requests in tests.
Fill both blanks to create a test that checks if a Django model instance is saved correctly.
from myapp.models import Item @pytest.mark.[1] def test_item_creation([2]): item = Item(name='Test Item') item.save() assert Item.objects.count() == 1
The test needs the django_db mark to access the database and the db fixture to use the database in the test function.
Fill all three blanks to write a test that uses the Django test client to check the homepage response and content.
def test_homepage_content([1]): response = [2].get('/') assert response.status_code == 200 assert '[3]' in response.content.decode()
The test function uses the 'client' fixture to make a GET request. It checks the status code and verifies that the word 'Welcome' is in the response content.