Register to get access to free programming courses with interactive exercises

Development through testing Python: Automated testing

At what point is it better to write tests? In general, there are three approaches:

  • Write tests after your code
  • Write tests while you're writing your code
  • Write tests before you write code

In this lesson, we'll figure out the features of each approach.

Testing after code

In some situations, there isn't much choice. For example, in system testing, tests should simulate user behavior and perform actions in the browser. We write these tests after the code. The programmer can choose from the options above in integration, unit, and other low-level tests.

Testing before code

The tests-after-code approach is one of the less useful ones. Let's figure out why this is so. The very process of writing code involves constantly running the code and checking that it works. In the simplest tasks, the program can run quite quickly:

# Its is easy to run and to check results
capitalize('hello')  # Hello

Preparing data to check the code can take dozens of minutes in real-life code. Moreover, the results produced after testing the code may be quite complex, for example, many records in a database or a complex structure.

The code test runs turn into a whole adventure:

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.