How to Use -v Flag in pytest for Verbose Test Output
Use the
-v flag with the pytest command to run tests in verbose mode, which shows detailed information about each test executed. This helps you see the names of tests and their results clearly in the output.Syntax
The basic syntax to use the verbose flag in pytest is:
pytest -v [options] [test_paths]Here:
pytestruns the test runner.-venables verbose mode to show detailed test names and results.[options]are other optional pytest flags.[test_paths]is the path to your test files or directories.
bash
pytest -v
Example
This example shows a simple test file and how running pytest with -v displays detailed test names and results.
python
def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 2 - 1 == 1
Output
test_example.py::test_addition PASSED
test_example.py::test_subtraction PASSED
============================== 2 passed in 0.01s ==============================
Common Pitfalls
Some common mistakes when using -v include:
- Not using
-vand missing detailed test names in output. - Confusing
-vwith--verbose(both work the same, but-vis shorter). - Expecting
-vto change test behavior; it only changes output detail.
bash
Wrong usage (no verbose): pytest Right usage (verbose): pytest -v
Quick Reference
| Flag | Description |
|---|---|
| -v or --verbose | Show detailed test names and results |
| -q or --quiet | Show less output |
| -s | Disable output capture to see print statements |
| --maxfail=num | Stop after num failures |
Key Takeaways
Use
-v with pytest to see detailed test names and results.-v does not affect test execution, only output verbosity.You can use
-v alone or with other pytest options.Verbose output helps quickly identify which tests passed or failed.
Remember
pytest -v is the same as pytest --verbose.