I know that py.test can test a single module if I do:
py.test mod1.py
Or, I can invoke pytest inside python:
import pytest
pytest.run(['mod1.py'])
Can I do it inside python, and let it to run the current module? I guess I can do:
import pytest
import os
pytest.main([os.path.basename(__file__)])
But wonder whether this is the most "pythonic" way to do it. Thanks!
Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .
Calling pytest through python -m pytest You can invoke testing through the Python interpreter from the command line: python -m pytest [...] This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.
The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.
Your versions do not allow passing extra arguments to pytest (e.g. allow ./test_file.py -v
). I tried simply
import sys
if __name__ == '__main__':
pytest.main(sys.argv)
and it seems to do the trick. sys.argv[0]
is the script name (i.e. __file__
, possibly as a relative path), so it restricts the call to the script, and sys.argv[1:]
contain extra arguments passed on the command-line.
Any better idea appreciated!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With