How do I run one unittest test while debugging a function?
Quick answer
Use unittest’s dotted test name to run one method: python -m unittest test_pricing.TestPrice.test_discount. Replace the module, class, and method with the names in your project. You can also select tests by name pattern with -k. After investigating, run python -m unittest for discovery or python -m unittest discover to check the broader suite. These commands rely on importable test modules and a compatible project layout. [S1]
The question, the details, the next step.
A practical checklist
Copy the module, class, and method names from the test you want to run.
Run
python -m unittest module.Class.methodfor one specific test.Use
python -m unittest -k patternto select tests by a name pattern or substring.Change one thing at a time and rerun the focused command.
After debugging, run
python -m unittestorpython -m unittest discover.If a command cannot find tests, check that the modules are importable and fit the project layout.
A few more useful answers
Do I include `.py` in the dotted test name?
The documented selection form uses an importable test module name, such as test_module.TestClass.test_method, rather than a filename with .py. Adapt the example to the module name available in your project. [S1]
Does one passing test prove the function is fully correct?
No. It shows only that the selected case passes. Run the broader suite afterward, and add or run focused cases for other behaviors you need to check.
What if the exact command doesn’t find my test?
Check that the module name, class, and method are spelled as they appear in your tests, and that the test module is importable from where you run the command. The documented commands depend on importability and project layout. [S1]
Sources
References used for this guide.
- [S1] Python 3.12 unittest: Command-line test selection and discovery docs.python.org · Checked
