How do I run all Python unit tests in a directory?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Running all Python unit tests in a directory is usually a test-discovery problem, not a loop-writing problem. The tool walks the directory tree, finds files that match its naming rules, imports them, and executes the discovered tests. In Python, the two most common ways to do this are the built-in unittest discovery command and pytest.
If you want the standard-library answer, use python -m unittest discover. If the project already uses pytest, run pytest against the target directory instead. The right choice depends on the test framework the repository is already using.
Use unittest Discovery
Python's built-in unittest module can discover tests recursively from a starting directory.
The flags mean:
- '
-s testssets the start directory' - '
-p 'test*.py'defines the filename pattern'
If your tests follow the default naming conventions, even this may be enough:
A simple layout might look like this:
And a minimal test file:
Use pytest If the Project Already Depends on It
Many Python projects use pytest because it has simpler test syntax and richer reporting.
Or just run it from the project root:
A pytest test can be very small:
pytest will also run many unittest.TestCase tests, which makes it a good bridge when a codebase is gradually modernizing its tests.
Run Tests From the Correct Working Directory
Test discovery depends on imports working. The safest habit is to run the command from the project root so the package layout matches what the tests expect.
For example:
If you run discovery from the wrong directory, Python may fail to import the application package or may discover the wrong files.
Narrow or Expand What Gets Discovered
You can control scope by changing the start directory or pattern.
Run only API tests with unittest:
Run only files matching a custom pattern:
This is useful when the repository separates unit, integration, and end-to-end suites into different folders.
Keep Naming and Imports Consistent
Discovery works best when the project follows clear conventions. Common patterns are:
- test files named
test_*.py - test classes inheriting from
unittest.TestCase - test methods named
test_* - packages structured so imports work from the project root
The more predictable the layout, the less time you spend debugging discovery behavior.
Common Pitfalls
The most common mistake is running discovery from the wrong directory and then chasing import errors that are really path issues.
Another issue is mixing frameworks without noticing. A repository may contain pytest tests, unittest tests, or both, and the command should match the project.
A third problem is using a filename pattern that does not match the actual test files, which makes it look as though there are no tests to run.
Summary
- Use
python -m unittest discoverfor the standard-library test discovery workflow. - Use
pytestwhen the repository already depends onpytestconventions or plugins. - Run the command from the project root so imports resolve correctly.
- Adjust the start directory and filename pattern to control which tests are discovered.
- Keep naming conventions consistent so discovery works predictably.
Related reading
- How do I run celery status/flower without the -A option?
- How do I run Python code from Sublime Text 2?
- How do I sample a line across a blob at a perpendicular angle? in Python/OpenCV unless you suggest switching to something else
- How do I select certain columns of a 2D tensor in TensorFlow?
- How do I run/test my Flutter app on a real device?
- How do I store a fitted PCA so that I may transpose unseen testing dataset? I do not wish to keep the large training dataset on my CPU
- How do I select rows from a DataFrame based on column values?
- How do I send something to connected websocket clients from another thread?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.