Reputation: 648
I have written pytest unit tests for a package I've written with a structure like below:
Now packagedir/mypackage/test/conftest.py establishes two required command line parameters to be created as fixtures for the tests. When I run something like:
pytest packagedir/mypackage/ --var1 foo --var2 bar
All the tests run and the command line parameters are read in correctly. However, when I pip install the package (which installs the test and unit_tests packages correctly) and then try something like this:
pytest --pyargs mypackage --var1 foo --var2 bar
I get an error like this:
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --var1 foo --var2 bar
If I run:
pytest --pyargs mypackage
the tests run but fail because the command line arguments are not present.
Can someone explain to me what might be going wrong here? I could guess and say this is because the --pyargs argument changes the interpretation of the command line parameters for pytest, but it's just a guess. I'd like to know if there is an actual way I could fix this.
The overarching goal would be to allow someone to pip install mypackage and then be able to easily test it without navigating to the site-packages location of the install.
Upvotes: 8
Views: 4879
Reputation: 14883
I added a __main__.py
to the mypackage/test
directory and wrote
import os
import sys
HERE = os.path.dirname(__file__)
if __name__ == "__main__":
import pytest
errcode = pytest.main([HERE] + sys.argv[1:])
sys.exit(errcode)
Now, I can execute the tests with
python -m mypackage.test --myoption=works
This works when the package is installed.
You can view this in action with a travis build:
Upvotes: 9