Running a single test from unittest.TestCase via the command line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python's unittest command-line runner lets you target one specific test method instead of executing the whole suite. The trick is to use the fully qualified dotted path to the module, test case class, and method.
Run One Test Method
Given a file named test_math.py:
Run only test_addition like this:
That is the canonical command-line pattern.
The Dotted Path Structure
The path has three pieces:
- module name
- '
TestCaseclass name' - test method name
So:
means:
- import module
test_math - find class
TestMath - run method
test_addition
If the test lives inside a package, include the package path too.
Run a Whole TestCase Class
If you want all tests in one class but not the rest of the suite:
This is useful when debugging one area of behavior without running unrelated tests.
Use Verbose Output While Debugging
For clearer output:
The -v flag prints more detail about what was run and whether it passed or failed.
Discovery and Import Rules Still Apply
The command works only if Python can import the test module. That means:
- the module path must be correct
- the current working directory or
PYTHONPATHmust make the package importable
If the runner says it cannot import the module, the problem is often path resolution rather than the test name itself.
A Useful Habit for Bigger Projects
When test packages are nested, it is often easiest to copy the dotted import path directly from the package layout. For example:
The command is typically:
Thinking in import paths rather than file paths avoids a lot of confusion.
You Can Also Use Discovery with -k in Newer Python
If you prefer pattern matching over a full dotted path, newer Python versions also support -k with unittest discovery:
That is broader than naming one exact test method, but it can be handy when you want to run a small subset of tests matching a pattern.
Why This Is Better Than Editing the Test File
Some developers temporarily comment out other tests or rename them just to run one method. The command-line selector is much better because:
- it keeps the test file unchanged
- it is repeatable
- it works well in shell history and scripts
That makes debugging faster and less error-prone.
Common Pitfalls
The biggest pitfall is using a filesystem path instead of a dotted Python import path. unittest wants module notation, not a shell file path with slashes.
Another common issue is misspelling the test method name. The method must exist exactly as written, including the test_ prefix.
People also forget the current working directory matters for imports. A correct dotted path may still fail if the command is run from the wrong place.
Summary
- Run one
unittestmethod withpython -m unittest module.TestCase.test_method. - Use the full dotted import path, not a slash-based file path.
- Add
-vfor more readable debugging output. - You can also target a whole
TestCaseclass instead of one method. - If the command fails, check import paths and the current working directory first.

