14
Lesson 33 of 44

Debugging and testing

Learn to find and prevent errors the way professionals do. In this lesson you will learn how to read a traceback, how to debug with traces, logging and breakpoint(), how to check your code with assert, how to write tests with unittest and how to design good test cases, including boundary values.

  • 5 theory articles
  • 9 practice exercises
Start lesson

Fundamentals concepts 5

Types of errors and how to read a traceback

All programmers make mistakes, every day. The difference between a beginner and a professional is that the professional knows how…

Theory
Read more

Debugging: traces, logging and debugger

Debugging is finding and fixing the cause of an error. It does not consist of trying things at random: it is a method.

Theory
Read more

assert and test cases

The best way to find logic errors is to test the code with cases whose result you already know . The simplest tool in Python is…

Theory
Read more

Automated tests with unittest and pytest

With assert and a table of cases you can already test your code. But when a project grows, it is worth using a test framework …

Theory
Read more

Boundary values and good practices

Writing tests is easy. Writing good tests, the ones that really find errors, requires thinking about which cases to choose.

Theory
Read more

Practice exercises 9

Read a traceback

Create a Python program that extracts the important information from a traceback , the message that Python shows when an…

Exercise Medium
Solve it

Tracing a loop

Create a Python program that uses traces ( print messages) to follow the value of the variables step by step, the simplest…

Exercise Easy
Solve it

Testing with assert

Create a Python program that tests a function with a list of cases using assert .

Exercise Easy
Solve it

Text test table

Create a Python program that runs a test table : a list of inputs with the expected result of each one.

Exercise Medium
Solve it

Hunt the bug

This function has a hidden bug . Create a program that finds it by comparing its result with the correct one in several cases.

Exercise Medium
Solve it

Run tests with unittest

Create a Python program that defines a test with unittest , runs it from the program itself and counts the failures.

Exercise Medium
Solve it

Logging records

Create a Python program that uses the logging module to record messages with different levels and show only the important ones.

Exercise Medium
Solve it

Preconditions with assert

Create a Python program with a function that checks its preconditions with assert and an error message.

Exercise Easy
Solve it

Boundary values

Create a Python program with a function whose most important test cases are at the boundaries of each interval: just before…

Exercise Easy
Solve it