Chapter 6: Django Add Test View

Django Add Test View: Many beginners skip testing because “it works on my machine”… until it doesn’t, or until you hand the project to someone else, or until you deploy and everything breaks in production.

Today we’ll do it slowly, practically, and with zero theory overload — like I’m sitting next to you creating the first test together.

Goal for Today

We will:

  1. Create one very simple test view (just returns “Test page works!”)
  2. Write two basic tests for it using Django’s built-in test client
  3. Run the tests and see green output
  4. Understand folder structure & best naming habits
  5. See how to test a real view (your polls index) later

Step 1: Where Do Tests Live?

Django expects tests in files that:

  • Are inside your app
  • Start with test_ (e.g. tests.py, test_views.py, test_models.py)

Default file created by startapp:

text

Better practice (2026 standard) — split tests by topic:

text

Let’s do that.

Bash

Delete or leave empty the old tests.py — we won’t use it.

Step 2: Create a Tiny Test View (Just for Learning)

Open polls/views.py and add this at the bottom (or in a new file if you prefer):

Python

Step 3: Add URL for the Test View

In polls/urls.py:

Python

Visit http://127.0.0.1:8000/polls/test-view/ → should see green message.

Step 4: Write Your First Django Test

Open polls/tests/test_views.py

Python

Step 5: Run the Tests!

Two ways:

Way A – Run only polls app tests

Bash

Way B – Run everything (recommended when you have many apps)

Bash

You should see something beautiful like:

text

Green = success 🎉

Step 6: Bonus – Test Your Real Polls Index View

Now let’s test something useful.

Add this to polls/tests/test_views.py:

Python

Run again → python manage.py test polls

Best Practices & Quick Reference Table (2026 Style)

Thing to test Method / Assertion Example line
Status code assertEqual(response.status_code, 200) Check page loads
Contains text assertContains(response, “Hello”) Check important phrase exists
Template used assertTemplateUsed(response, ‘polls/index.html’) Verify correct template
Redirect assertRedirects(response, ‘/other-url/’) After POST or login
No content self.assertEqual(response.content, b”) Empty response
Context variable self.assertEqual(response.context[‘key’], value) Check data passed to template

Common Beginner Pain Points & Fixes

  • ImportError: No module named ‘polls.tests’ → Forgot __init__.py in tests/ folder
  • Reverse for ‘test-page’ not found → Forgot app_name = ‘polls’ in polls/urls.py
  • Database is not empty → Tests run in temporary test database — real db is untouched
  • Tests fail randomly → You modified real database in shell → always use TestCase (resets db)

Your Mini Homework Right Now

  1. Create polls/tests/test_views.py with the three simple tests above
  2. Run python manage.py test polls
  3. See green output
  4. Add one more test: check that /polls/test-view/ contains the emoji “✅”
  5. (Optional) Break the view on purpose (change status to 404) → see test become red → fix it

Tell me what feels next:

  • “Green! Now show me how to test models (create, save, methods)”
  • “How to test POST requests (voting form)”
  • “I want to use pytest instead of unittest – example”
  • “Got red test – here is the error message”
  • Or finally ready for: “Let’s implement the voting view + form + atomic vote increment”

You just wrote your first real Django tests — this habit will make you 10× better developer in the long run. Super proud of you — let’s keep the momentum! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *