Allow pytest to be invoked as a module
See original GitHub issueThis is basically a duplicate of #7207, but with different rationale.
pytest have the options to be invoked as plain pytest and python -m pytest. The difference between these two is that the latter adds the current directory to sys.path. So, letβs say I have a project with layout like this:
βββ module_foo
β βββ __init__.py
βββ common
β βββ __init__.py
βββ requirements.txt
βββ tests
β βββ __init__.py
β βββ test_common
β β βββ test_stuff.py
β βββ conftest.py
If I write in conftest.py something like this:
import common
VS Code test runner will fail because it would not be able to import my code. But, if I invoke it as a module, because of sys.path everything will just work.
So, I am asking to give an option to run pytest as module, or option to prepend working directory to PYTHONPATH variable.
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Allow pytest to be invoked as a module #7207 - GitHub
I have tests in my Python project (Nunavut) that use a shared fixture that requires the "test" directory be on the sys path....
Read more >How to invoke pytest β pytest documentation
In general, pytest is invoked with the command pytest (see below for other ways to invoke pytest). This will execute all tests in...
Read more >Invoke pytest from python for current module only
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...
Read more >Effective Python Testing With Pytest
With pytest, you can make your test suites fast, effective, ... a special configuration module called conftest.py will allow you to do that....
Read more >pytest usage - manpages.ubuntu!
pytest allows one to drop into the PDB prompt immediately at the start of each test via a command line option: pytest --trace...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future.
We do encourage people to continue π the first/opening comment as it helps us prioritize our work based on what the community seems to want the most.
Iβve just switched over to VSCode from vim, and this would be a huge help for me and my team.
Our project is set up such that
testsdirectory is just inside the project directory, alongside all the app source directories (lib,utils, etc. no all-encompassingsrcdirectory). We used to do some gross gymnastics with setting pythonpath to the project directory in the app code to make sure we could import all of the local modules from the other ones (including for tests), but we were able to completely remove that by mandating that every python command we run is run as a module (i.e.python -m pytest).I was able to get our test suite working with the vscode plugin by making a
.envfile in the project root (besidepytest.ini) that just containsbut that feels like a bad way to handle this. Iβd much prefer an actual setting for it.