Call pytest from python only for current module

I know that py.test can test one module if I do this:

py.test mod1.py 

Or I can call pytest inside python:

 import pytest pytest.run(['mod1.py']) 

Can I do this inside python and let it run the current module? I think I can do:

 import pytest import os pytest.main([os.path.basename(__file__)]) 

But wonder if this is the most "pythonic" way to do this. Thanks!

+8
python
source share
1 answer

Your versions do not allow passing additional arguments to pytest (e.g. allow ./test_file.py -v ). I tried just

 import sys if __name__ == '__main__': pytest.main(sys.argv) 

and it seems like a trick. sys.argv[0] is the name of the script (that is, __file__ , possibly as a relative path), so it restricts the call to the script and sys.argv[1:] contains additional arguments passed to the command line.

Any best idea appreciated!

+7
source share

All Articles