pytest -k <keyword> --co | grep "collected" | tail -1 | awk '{print $2}'
Tag: pytest
Count how many tests each module has via pytest
python -m pytest --co -q | grep "::" | awk -F'::' '{print $1}' | uniq -c
How to check the root cause of the difference between “pytest” and “python -m pytest”?
| Command | Uses which Python? | Risk |
|---|---|---|
pytest | Whatever executable is first in PATH | May run wrong environment |
python -m pytest | The Python you explicitly called | Much safer |
cat $(which pytest)
The shebang line can show which “python” that pytest uses.
python -c "import sys; print(sys.executable)"
or
which python
This shows the interpreter you’re explicitly invoking.
unittest setUp vs. pytest setup_class
unittest:
import unittest
class MyTest( unittest.TestCase ):
def setUp( self ):
self.variable = ...
pytest:
import pytest
class MyTest:
@classmethod
def setup_class( cls ):
cls.variable = ...
Assert exception during python unit test
If using unittest:
import unittest
with self.assertRaises( Exception ) as error:
...
If using pytest:
import pytest
with pytest.raises( Exception ) as error:
...