I have the following folder structure in python:
├──project1
│ ──venv
│ ──src
| ─main.py
│ ──test
| ─test.py
├──project2
│ ──venv
│ ──src
| ─main.py
│ ──test
| ─test.py
├──shared
│ ──shared-library-1
| ─spam.py
│ ──shared-library-2
| ─very-common.py
|
Project1 & project2 are FastAPI services with their own virtual environments, dependencies, tests and they have some shared code too in the shared folder. I ve created a multi-root VS Code code workspace to have all this under one umbrella, so I can run and debug both service at the some time conveniently. It works nicely for running and debugging the projects, but I m having issues with setting up testing.
I can create launch configurations to run all pytests from terminal, but the built-in VS Code test discovery fails with error (on test.py):
"ModuleNotFoundError: No module named src "
I think I know why, it s the classic import error because python can t import main.py from test.py unless pythonpath environment variable is not set up correctly. This is the launch configuration to run tests for project1:
{
"name": "Project1 Tests",
"type": "python",
"request": "launch",
"module": "pytest",
"python":"${workspaceRoot:project1}/venv/bin/python",
"cwd": "${workspaceFolder:project1}",
"env": {"PYTHONPATH": "${workspaceFolder:project1}/..${pathSeparator}${env:PYTHONPATH}"},
"jinja": true,
"justMyCode": true
}
This sets up the pythonpath nicely for each of the projects so imports can work and I can run my tests. How can I do the same for the built-in VS Code discoverer? I d like my tests to show up nicely in the testing tab to be able run and debug them one-by-one.