Loosely based on my other answer.
Count all tests
One-liner:
$ pytest --collect-only -q | head -n -2 | wc -l
Explanation
--collect-only
combined with -q
outputs one test per line, with a trailing info line. Example:
$ pytest --collect-only -q
test_eggs.py::test_bacon[1]
test_eggs.py::test_bacon[2]
test_eggs.py::test_bacon[3]
test_spam.py::test_foo
test_spam.py::test_bar
no tests ran in 0.00 seconds
The rest is just routine: head -n -2
strips the info line, wc -l
counts the lines.
Applying further filtering works as usual, e.g.
$ pytest --collect-only -q -k "fizz" | head -n -2 | wc -l
will count only tests containing fizz
in name,
$ pytest --collect-only -q buzz/ fuzz/ | head -n -2 | wc -l
will count only tests inside buzz
and fuzz
directories etc.
Count tests per test module
If you want to get the info about how many tests are in each module, use --collect-only
combined with -qq
:
$ pytest --collect-only -qq
test_eggs.py: 3
test_spam.py: 2
Count unique tests (test parametrizations counting as single test)
What the OP initially requested. This is a modification of the above command that strips the parametrization from test names and removes duplicate lines before counting:
$ pytest --collect-only -q | head -n -2 | sed 's/\[.*\]$//' | sort | uniq | wc -l