liveBook · Manning

Tests.py

As explained in Django in Action

tests.py is the conventional Django application module containing tests for the application.

tests.py is the conventional Django application module for unit tests. Django creates it when you create an app, usually with an import for django.test.TestCase. section 2.3 section 8.2

A test class inherits from TestCase and defines methods whose names begin with test_. Django’s test client exercises the application without opening a real browser. section 8.2 section 8.2

Testing a view

In home/tests.py, TestHome can call self.client.get("/credits/") and check response.status_code with self.assertEqual(200, response.status_code). Running python manage.py test discovers and executes the test, reporting success or failure. section 8.2 section 8.2

Listing 8.1 A test case for the credits() view
# home/tests.py
from django.test import TestCase  #1

class TestHome(TestCase): #2
    def test_credits(self):  #3
        response = self.client.get("/credits/")  #4
        self.assertEqual(200, response.status_code)  #5

Shows the complete tests.py listing, including TestCase inheritance, client request, and status assertion. section 8.2

How Django in Action covers it

How deep it goes
Defines TestCase inheritance, test_ naming, client requests, assertions, and command-line execution, but not broader testing strategy.
Examples
Uses the home app’s credits view, a GET request to /credits/, status code 200, and python manage.py test.
What it assumes
Assumes a Django application and view, while teaching test-file structure and basic view testing from the framework’s generated skeleton.
What you can do after
Apply Django’s test client and assertions to verify that a view returns the expected HTTP status.
How it connects
Connects Django applications with views, URL routing, HTTP responses, TestCase, assertions, and the test client.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.