Unit-Tests are a powerful tool for developers to ensure their code quality. They allow you to test functions in isolation and confirm that they work as intended. In this guide, I will show you how to write your first Unit Test in Python. You will learn not only the basics of unit testing but also how to structure your tests efficiently.

Key Insights

  • Unit tests help detect errors early and ensure that your functions work as intended.
  • The Python library unittest provides a simple way to implement tests.
  • Effective tests include both positive and negative scenarios and check the data types of the returns.

Step-by-Step Guide

Step 1: Prepare Your Project

First, you need a Python file where your function is defined. In our example, we will call the file preisberechnung.py. Here you will define a function get_verkaufspreis that takes the purchase price and adds a margin.

Python Unit-Test Basics: Your Introduction

Step 2: Create a File for Your Unit Tests

Next, create a new file that will contain the tests for this function. Let's call this file preisberechnung_test.py. Here you will work with the unittest module.

To use the framework, import it at the beginning of your file:

import unittest
from preisberechnung import get_verkaufspreis
Python Unit-Test Basics: Your Introduction

Step 3: Define a Test Class

A test class should inherit from unittest.TestCase. This allows you to organize all tests in a structured manner. Rename the class to PreisberechnungTest to clarify what is being tested.

Python Unit-Test Basics: Your Introduction

Step 4: Write Your Tests

Now it's time to write actual tests. Start with a test that checks whether the return of get_verkaufspreis has the expected type (float):

class PreisberechnungTest(unittest.TestCase): def test_preis_ist_float(self): vk = get_verkaufspreis(100) self.assertIsInstance(vk, float)
Python Unit-Test Basics: Your Introduction

Step 5: Test Invalid Inputs

It is equally important to test invalid inputs. Add a test that checks whether the function raises an exception for invalid inputs, such as None or a string:

def test_invalid_input(self): with self.assertRaises(TypeError): get_verkaufspreis("invalid")
Python Unit Test Basics: Your Introduction

Step 6: Run Tests

To run your tests, you can do this either via the terminal or directly in your IDE. If you use the terminal, execute the following:

python -m unittest preisberechnung_test.py

In your IDE, such as PyCharm, you can use the Run configuration to execute the tests.

Python Unit-Test Basics: Your Introduction

Step 7: Review Results

If you run the tests successfully, you will receive output indicating whether all tests passed. A green result means that your code works as intended.

Step 8: Expand Tests

In addition to the basic tests, you can add other relevant scenarios to increase code safety. Consider testing both positive and negative cases to ensure your logic works in all situations.

Python Unit-Test Basics: Your Introduction

Summary - First Unit Test with Python: Basics and Implementation

Writing unit tests is an essential skill that helps you make your code reliable. In this guide, you learned how to write your first unit test in Python, from defining the function to executing and reviewing the results. Thorough testing significantly improves code maintainability and detects issues early.

Frequently Asked Questions

How do I create a unit test in Python?To create a unit test in Python, create a test class that inherits from unittest.TestCase and write tests as methods of this class.

Why should I write unit tests?Unit tests help detect errors early and ensure that your functions work as intended.

How do I run unit tests?You can run unit tests via the terminal using python -m unittest [filename] or directly in your IDE.

What are good practices for unit tests?Good practice includes both positive and negative test scenarios as well as checking data types. Regular execution is also important to verify changes in code immediately.