Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 54

The Power of Automated Testing

Recommended resources

Hello! Welcome to the next stage of our journey into professional web development.

In our last lesson, we manually verified that our API endpoints work correctly using the Browsable API and Postman. While this is a crucial skill, you might have noticed it's a repetitive process. Imagine having to manually test every single feature of your Task Manager application every time you make a small change. It would quickly become impractical and error-prone.

This lesson addresses that exact problem. Today, we shift from manual verification to a core practice of modern software engineering: automated testing. Our goal is to understand why writing tests is not just a "nice-to-have," but an essential discipline for building reliable, maintainable applications and for being an effective developer.

1. The Core Problem: Why Manual Testing Isn't Enough

As your application grows, its complexity increases exponentially. A change in one part of the code can unexpectedly break something seemingly unrelated in another part. This is called a regression.

Manually re-testing every feature to catch regressions is not a scalable strategy. It's slow, tedious, and humans are bound to miss things.

Let's start by reading a short introduction that frames this problem perfectly.

Django Tutorial Part 10: Testing a Django web application

The introduction of the Mozilla Developer Network (MDN) guide on Django testing explains why manual testing becomes unsustainable as a project grows.

Please read the 'Overview' section. Focus on the arguments about growing complexity and the benefits of automated tests like speed and reliability.

As the article highlights, automated tests solve this by providing a fast, reliable, and repeatable way to ensure your application continues to work as intended every time you make a change.


2. The Solution: The Power of Automated Testing

Automated testing is the practice of writing code to test your application code. You define what the correct behavior is, and the tests automatically check if the application still produces that correct behavior.

Watch this very short clip that succinctly states the purpose of automated testing.

35 - Automated Test Basics - Python & Django 3.2 Tutorial Series

This clip from the 'CodingEntrepreneurs' channel offers a concise definition of the goal of automated testing.

Watch the first 19 seconds of the video.

So, why is this so important? Automated tests provide several key benefits that are crucial for any serious project, including the Task Manager you're building for your portfolio.

Reasons to Test Django Applications
This infographic summarizes the key motivations for testing: finding and fixing bugs, verifying functionality, and preventing regressions.

Let's dive a bit deeper into these advantages. The following article gives a great breakdown of why testing is so valuable.

Why Is Unit Testing important in Python Django Apps?

The article 'Why Is Unit Testing important in Python Django Apps?' from Quintagroup outlines the key benefits of incorporating tests into your development workflow.

Please read the sections titled 'Why Should You Test Your Django Apps?' and 'The Advantages of Unit Testing in Django'. Pay attention to points about early bug detection, cost reduction, and how testing supports agile development.

To summarize the key advantages:

  • Confidence: You can refactor old code or add new features with confidence, knowing that if you break something, your test suite will immediately alert you.
  • Early Bug Detection: Tests find bugs early in the development cycle, when they are cheapest and easiest to fix. A bug found by a unit test takes minutes to fix; a bug found by a user in production can take hours or days and damage your reputation.
  • Documentation: Well-written tests act as living documentation for your code. They describe exactly how a piece of code is intended to be used and what it's supposed to do.
  • Improved Design: The process of writing a test forces you to think about your code from the outside, often leading to better, more modular, and easier-to-use designs.

3. A Framework for Thinking About Tests: The Testing Pyramid

Not all tests are created equal. Different types of tests serve different purposes and have different costs in terms of how long they take to write and run. The "Testing Pyramid" is a popular model for thinking about how to balance different types of tests.

5 Types of Testing Software Every Developer Needs to Know!

This video from Alex Hyett introduces the concept of the Testing Pyramid, a fundamental model in software testing.

First, watch from the beginning to 00:32 to understand the pyramid structure. Then, watch from 05:54 to 06:10 to understand why it's better to catch bugs at the bottom of the pyramid.

The main takeaway from the pyramid is that you should have:

  • Many fast, simple Unit Tests at the base.
  • Fewer slower, more complex Integration Tests in the middle.
  • Very few slow, brittle End-to-End (E2E) or Manual Tests at the top.

Our focus in this course will be on Unit Tests and Integration Tests, which form the foundation of a solid testing strategy. Let's briefly define them.

Django Tutorial Part 10: Testing a Django web application

The MDN article again provides clear, concise definitions for different types of tests.

Read the short definitions under the 'Types of testing' heading for 'Unit tests' and 'Integration tests'.

  • Unit Tests: Verify the smallest possible piece of your code in isolation. For example: does a single function return the correct value? Does a specific method on your Task model work as expected?
  • Integration Tests: Verify that several components work together correctly. For example: when a user submits a form, is a new Task object correctly created in the database? Does a view render the correct template with the right data?

Django's built-in testing framework is powerful enough to handle both types of tests effectively.


4. What Exactly Should You Test?

This is a critical question. The answer is simple in principle: test your own code, not the framework's. You can trust that Django's CharField works; you don't need to write a test to prove it stores a string. However, you should test any custom logic or behavior you add to your application.

Django Tutorial Part 10: Testing a Django web application

Let's return to the MDN article one last time for excellent guidance on what to test within a Django model.

Read the section 'What should you test?'. The example using the Author model is a perfect illustration of this principle.

For your Task Manager project, this means you would test things like:

  • A custom method on your Task model (e.g., a method to check if a task is overdue).
  • Whether a view correctly restricts access so a user can only see their own tasks.
  • That your Task form correctly validates user input.

You would not test that Django's ForeignKey field correctly links two models. You assume the framework has already tested its own features.

Test your understanding!

Imagine you add a field priority to your Task model, which is an IntegerField. You also add a custom method called is_high_priority() that returns True if the priority is greater than 5, and False otherwise. According to the principle of "test your code, not the framework's," which part of this new feature should you write a test for?

Show answer

You should write a unit test for the is_high_priority() method. You would create a few Task objects with different priority values and assert that the method returns the correct boolean value for each. You do not need to test whether the IntegerField actually stores an integer; that is functionality provided and tested by Django itself.


5. Testing as a Professional Standard

In a professional development environment, automated tests are non-negotiable. They are often integrated into a process called Continuous Integration/Continuous Deployment (CI/CD). In a CI/CD pipeline, every time a developer pushes new code, a server automatically runs the entire test suite. If any test fails, the new code is blocked from being deployed to production. This creates a powerful safety net.

35 - Automated Test Basics - Python & Django 3.2 Tutorial Series

This final clip explains how automated testing enables robust, professional development workflows like CI/CD.

Watch from 13:36 to 14:44 to understand how tests act as a safeguard and enable CI/CD.

Including a solid test suite with your portfolio project demonstrates a level of professionalism and competence that goes far beyond just making the application work. It shows you know how to build software that is robust and maintainable.


Conclusion

Today, we've explored the "why" of automated testing. It might seem like extra work upfront, but it pays for itself many times over by giving you the confidence to grow and change your application without fear of breaking it.

Key Takeaways:

  • Automated testing prevents regressions by automatically verifying that your code still works after changes.
  • It provides confidence, acts as documentation, and catches bugs early, saving time and money.
  • The Testing Pyramid guides us to write many fast unit tests and fewer, more complex integration tests.
  • You should test your own business logic, not the underlying functionality of the Django framework.
  • Automated testing is a fundamental professional practice and a key component of modern CI/CD workflows.

Preview of the Next Lesson:

Now that you understand why testing is so important, it's time to get our hands dirty. In the next lesson, we will move from "why" to "how" by writing our first unit tests for our Django models. We will see how to use Django's TestCase to verify the custom logic within our application.