0
0
PytestHow-ToBeginner ยท 3 min read

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:

  • pytest runs the test runner.
  • -v enables 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 -v and missing detailed test names in output.
  • Confusing -v with --verbose (both work the same, but -v is shorter).
  • Expecting -v to change test behavior; it only changes output detail.
bash
Wrong usage (no verbose):
pytest

Right usage (verbose):
pytest -v
๐Ÿ“Š

Quick Reference

FlagDescription
-v or --verboseShow detailed test names and results
-q or --quietShow less output
-sDisable output capture to see print statements
--maxfail=numStop 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.