Understanding Debugging & Testing Code In Python

This is the 8th post in a series of learning the Python programming language.

Debugging and testing are important aspects of the software development process. In Python, there are several tools and techniques that can be used to debug and test code.


Debugging is the process of identifying and fixing errors or bugs in your code.


One of the most common ways to debug Python code is using the print() function. By adding print statements to your code, you can output the values of variables at different points in the program, which can help you identify where the error is occurring. However, this method can become tedious and time-consuming for larger and more complex programs.

pdb library

The built-in pdb library in Python is a command-line debugging tool that provides a set of commands for stepping through your code, inspecting variables, and controlling the execution of your program.

One of the most useful commands provided by pdb is the set_trace() function. This function can be used to set a breakpoint in your code, which will pause the execution of the program and allow you to inspect the state of the program at that point.

For example, you can use the pdb.set_trace() function in the following way:

import pdb

def my_function():
a = 1
b = 2
c = a + b


When you run this code, the program will pause at the pdb.set_trace() line, and you will be prompted with the (Pdb) command prompt in the terminal. At this point, you can enter various commands to inspect the state of the program and control the execution.

Some of the most commonly used pdb commands are:

  • n or next: Execute the next line of code and move to the next line.
  • s or step: Step into a function call.
  • c or continue: Continue execution until a breakpoint is hit or the program exits.
  • l or list: List the source code for the current file.
  • w or where: Print the stack trace and line number of the current line.
  • p or print: Print the value of an expression.
  • h or help: Show a list of available commands.

For example, after the execution is paused, you can inspect the value of variable c by entering p c command.

(Pdb) p c

Additionally, you can use the function to start the pdb debugger on a specific function, and the pdb.post_mortem() function to start the pdb debugger after an unhandled exception has occurred in your program.

pdb is a powerful tool for debugging Python code and it provides a lot of flexibility and control over the execution of the program. However, it can be a bit more complex to use than the print() function, and it may take some time to become familiar with all of the commands and features that it provides.


Testing is the process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not. In Python, there are several popular testing frameworks such as unittestpytestdoctest, etc.


The unittestis a testing framework included in the Python standard library. It provides a solid set of tools for writing and running automated tests, including the ability to create test cases and test suites, as well as the ability to assert that certain conditions are met.

To use unittest, you’ll need to create a test class that inherits from unittest.TestCase, and then define test methods within that class. Test methods should start with the word test, and you can use the various assert methods provided by the TestCase class to check the results of your code.

For example, let’s say you have a simple function called add that takes two numbers and returns their sum:

def add(a, b):
return a + b

You can create a test case for this function by creating a test class that inherits from unittest.TestCase and defining test methods within that class:

import unittest

class TestAdd(unittest.TestCase):
def test_add_integers(self):
result = add(1, 2)
self.assertEqual(result, 3)

def test_add_floats(self):
result = add(0.1, 0.2)
self.assertAlmostEqual(result, 0.3)

def test_add_strings(self):
result = add('hello', 'world')
self.assertEqual(result, 'helloworld')

In the above example, there are three test methods: test_add_integerstest_add_floats, and test_add_strings. Each method calls the add function with different inputs and then uses an assert method provided by unittest.TestCase to check the result.

You can run the test case by calling unittest.main() in your script, or by running the script with the -m unittest command line option:

if __name__ == '__main__':

unittest provides several assert methods that you can use to check the results of your code. Some of the most commonly used assert methods include:

  • assertEqual(a, b): checks that a == b
  • assertTrue(x): checks that bool(x) is True
  • assertFalse(x): checks that bool(x) is False
  • assertIs(a, b): checks that a is b
  • assertIsNone(x): checks that x is None
  • assertIn(a, b): checks that a is in b
  • assertIsInstance(a, b): checks that isinstance(a, b) is True

You can also use setUp() and tearDown() methods to perform any setup or cleanup tasks that need to be done before or after each test method is run.

Additionally, unittest also provides a way to group test methods together in a TestSuite and run them together. This can be done by creating an instance of unittest.TestSuite and adding test cases to it.

import unittest

def suite():
suite = unittest.TestSuite()
return suite

if __name__ == '__main__':
runner = unittest.TextTestRunner()

In the above example, we’ve created a suite function that creates an instance of unittest.TestSuite and adds our test methods to it. The suite function is responsible for creating an instance of unittest.TestSuite and adding our test methods to it. The methods added to the suite are the test cases that we want to run. We can add as many test methods as we want to the suite using the addTest method.

Once the suite is created and the test cases are added, we use a TextTestRunner to run the suite. The TextTestRunner is a class provided by unittest that is responsible for running our tests and displaying the results in a human-readable format. When we call the run method on the TextTestRunner object, it will execute all the test cases that are added to the suite.


pytest is a powerful, feature-rich testing framework for Python. It is designed to make it easy to write and run repeatable tests for your code. One of the key features of pytest is its simple and easy-to-use syntax, which makes it easy to write and understand tests.

To get started with pytest, you will need to install it using pip:

pip install pytest

Once you have pytest installed, you can start writing tests for your code. One of the most important things to understand about pytest is that it uses a convention-over-configuration approach. This means that, by default, pytest will look for test files in your project that match certain patterns and run the test functions defined in those files.

For example, if you have a file called that contains your code, pytest will look for a file called in the same directory and run any test functions defined in that file. The test functions should be defined with the prefix test_. So for example, if you have a function called add in, you should have a test function called test_add in

Here is an example of a simple test function using pytest:

def test_add():
assert add(1, 2) == 3

You can run the tests by simply running pytest in the command line:


Pytest will automatically discover and run all the test functions in your project. It will also provide detailed information about any failures or errors that occur during the tests, making it easy to identify and fix any issues.

Another powerful feature of pytest is its ability to use fixtures. Fixtures are small bits of code that can be used to set up and clean up resources used by your tests. For example, if your tests rely on a database connection, you can use a fixture to set up the connection before the test runs, and then close the connection after the test is complete.

pytest also provides a number of built-in features such as parameterized test, assertion introspection, and rich test report. It also has a large number of plugins available that can be used to extend its functionality.

Overall, pytest is a great choice for anyone looking for a powerful, easy-to-use testing framework for Python. Its simple syntax and rich feature set make it a great choice for testing any Python project, large or small.


The doctest is a built-in testing framework in Python that allows you to include test cases directly in your documentation. It works by parsing the docstrings of your functions and classes, looking for code examples in the format of an interactive Python session, and then executing those examples to see if they produce the expected output.

To use doctest, you simply write test cases in the form of interactive Python sessions within the docstrings of your functions or classes. Here’s an example of a simple function with a doctest:

def add(a, b):
Add two numbers together.

>>> add(1, 2)
return a + b

In this example, the doctest looks for the line >>> add(1, 2) in the docstring, runs the code, and compares the output to the next line, which should be 3. If the output matches the expected value, the test is considered to have passed.

You can run the doctests in your code by using the doctest module. For example, you can run all the doctests in your code by running the following command:

python -m doctest -v

The advantage of using doctest is that it allows you to write tests that are directly tied to the documentation of your code. This makes it easy to ensure that your code is working as intended and that your documentation is accurate. Additionally, since the tests are included in the documentation, they serve as examples of how to use the code, making it easier for others to understand how it works.

One thing to keep in mind when using doctest is that it can be less powerful than other testing frameworks. It does not have the ability to test for exceptions or set up and tear down test fixtures. Additionally, it can be less flexible and harder to configure than other testing frameworks.

Overall, doctest is a useful tool for including simple tests in your documentation. It is easy to use and understand, and it helps to ensure that your code is working correctly and that your documentation is accurate. However, for more complex or advanced testing needs, it may be necessary to use a more powerful testing framework such as unittest or pytest.

If you like the post, don’t forget to clap. If you’d like to connect, you can find me on LinkedIn.


Leave a Reply

Your email address will not be published.