English 中文(简体)
How to make test discovery work in VSCode in multi-root workspace?
原标题:

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.

问题回答

It seems that there is indeed a problem with the new version of Python extension and vscode.

I tried the old version of 1.76.0 and 1.78.2 VSCode and 2023.2.0 Python. It works.

I also tried the pre-release v2023.9.11461009 version of Python. It works as well.

You can try to use pre-release version.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签