Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Structuring Test Files | Organizing and Running Tests
Python Unit Testing Fundamentals

bookStructuring Test Files

Swipe um das Menü anzuzeigen

Clear organization of unit tests makes your codebase easier to navigate and maintain. A common practice is to create a tests directory at the project root and place test files there. Test files are typically named with the test_ prefix and match the module they test, for example test_math_utils.py for math_utils.py.

# Example project directory structure
#
# my_project/
# ├── math_utils.py
# ├── string_utils.py
# └── tests/
#    ├── test_math_utils.py
#    └── test_string_utils.py
math_utils.py

math_utils.py

tests/test_math_utils.py

tests/test_math_utils.py

copy

This structure makes tests easy to find and understand, and helps other developers review or contribute to your code. Organizing tests by module or feature also simplifies maintenance and updates as the project grows.

# Adding a new test file for a new module

# Suppose you add a new module called data_processor.py.
# Create a corresponding test file in the tests directory:

# my_project/
# ├── data_processor.py
# └── tests/
#     └── test_data_processor.py
data_processor.py

data_processor.py

tests/test_data_processor.py

tests/test_data_processor.py

copy
Note
Note

Keeping your test code organized not only saves time but also reduces the risk of missing or duplicating tests. Well-structured test directories make automation and continuous integration setups much easier.

question mark

What is a common convention for naming test files?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 4. Kapitel 1
some-alt