question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow pytest to be invoked as a module

See original GitHub issue

This 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:open
  • Created a year ago
  • Reactions:10
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
brettcannoncommented, Aug 25, 2022

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.

0reactions
hoylemdcommented, Jul 12, 2022

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 tests directory is just inside the project directory, alongside all the app source directories (lib, utils, etc. no all-encompassing src directory). 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 .env file in the project root (beside pytest.ini) that just contains

PYTHONPATH=.

but that feels like a bad way to handle this. I’d much prefer an actual setting for it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found