0
0
PyTesttesting~10 mins

pytest-django for Django testing - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the pytest module for Django testing.

PyTest
import [1]
Drag options to blanks, or click blank then click option'
Aunittest
Bpytest
Cdjango
Drequests
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'django' instead of 'pytest'.
Using 'unittest' which is a different testing framework.
2fill in blank
medium

Complete the code to mark a test function to use the Django database.

PyTest
@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'
Drag options to blanks, or click blank then click option'
Askip
Bslow
Cdjango_db
Dxfail
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@pytest.mark.skip' which skips the test.
Using '@pytest.mark.slow' which is unrelated.
3fill in blank
hard

Fix the error in the test function to correctly use the Django test client fixture.

PyTest
def test_homepage([1]):
    response = client.get('/')
    assert response.status_code == 200
Drag options to blanks, or click blank then click option'
Aclient
Bdb
Crequest
Dsettings
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 'client' as a parameter.
Using 'db' which is for database access, not client.
4fill in blank
hard

Fill both blanks to create a test that checks if a Django model instance is saved correctly.

PyTest
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
Drag options to blanks, or click blank then click option'
Adjango_db
Bclient
Cdb
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'client' instead of 'db' as a fixture.
Missing the django_db mark.
5fill in blank
hard

Fill all three blanks to write a test that uses the Django test client to check the homepage response and content.

PyTest
def test_homepage_content([1]):
    response = [2].get('/')
    assert response.status_code == 200
    assert '[3]' in response.content.decode()
Drag options to blanks, or click blank then click option'
Aclient
CWelcome
Ddb
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'db' instead of 'client' for the fixture.
Checking for wrong content string.