Repository navigation
Description
Environment data
- VS Code version: 1.41.0
- Extension version (available under the Extensions sidebar): 2020.1.57204
- OS and version: MacOS 10.14.6
- Python version (& distribution if applicable, e.g. Anaconda): 3.7
- Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
- Relevant/affected Python packages and their versions: pytest-5.2.2
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"set to; more info How to update the language server to the latest stable version #3977): Language Server
Expected behaviour
When running a test with a settings.json that contains the "python.testing.cwd": "root/dir" option should result in a pytest command that uses "root/dir" as the --rootdir argument. If it is null, then fallback to existing behavior: --rootdir ${workspaceFolder}.
This is necessary for my use case since my code is in a monorepo and loads test fixtures from disk using paths relative to a project root rather than the repo root.
For example,
python [~]/.vscode/extensions/ms-python.python-2020.1.57204/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir root/dir -s --cache-clear [...testfolders]
jfyi, I've anonymized [~] and [...testfolders].
Actual behaviour
The "python.testing.cwd" setting has no visible effect on --rootdir. It is always the "workspace folder", which makes sense given the code I linked to below.
Relevant Code
| args.splice(0, 0, '--rootdir', options.workspaceFolder.fsPath); |
vscode-python/src/client/testing/pytest/main.ts
Lines 50 to 52 in 2b6a8f2
| const options: TestRunOptions = { | |
| workspaceFolder: this.workspaceFolder, | |
| cwd: this.rootDirectory, |
vscode-python/src/client/testing/pytest/main.ts
Lines 19 to 20 in 2b6a8f2
| constructor(workspaceFolder: Uri, rootDirectory: string, serviceContainer: IServiceContainer) { | |
| super(PYTEST_PROVIDER, Product.pytest, workspaceFolder, rootDirectory, serviceContainer); |
Steps to reproduce:
[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]
- Open a workspace folder containing python tests that are not in the root directory (i.e. a monorepo).
- Configure pytest to test a project directory.
2a. configure "python.testing.cwd" in settings.json to the project root. - Discover tests and observe that all tests are discovered.
- Run all tests.
4a. Observe the test session starts and all tests pass that do not need to load fixtures from disk.
4b. Observe the output logs look like this:
python [~]/.vscode/extensions/ms-python.python-2020.1.57204/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir [workspaceFolder] -s --cache-clear [project dir]
============================= test session starts ==============================
platform darwin -- Python 3.7.6, pytest-5.2.2, py-1.8.1, pluggy-0.13.1
rootdir: [workspaceFolder]
plugins: doubles-1.5.3, cov-2.8.1
Logs
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
XXX
Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)
XXX
Activity
chrisgervang commentedon Jan 12, 2020
Somewhat related to #9349 and #6881, but I don't believe I'd expect any auto detection.
ericsnowcurrently commentedon Jan 14, 2020
Chris Gervang (@chrisgervang), thanks for getting in touch with us. We'll take a look as soon as possible.
ericsnowcurrently commentedon Jan 14, 2020
Having looked this over, did you mean to file this as a bug or as a feature request? FWIW, I consider this a feature request. Just to be sure I understood, what you are asking for is that the "python.testing.cwd" setting be treated as the "root" directory against which the tests will be discovered/run. Is that correct?
Assuming yes, I see three options:
--rootdirfor pytest,--top-directoryfor unittest)If we do something about this then I expect it would be option 2. The reason is, "cwd" already has a specific meaning (essentially "the working directory of the executing program"). In the context of testing this often relates closely with the test root, which is part of the confusion here. (Sorry the help text for the setting,
Optional working directory for tests.isn't clear.) The semantics of CWD for program execution need to remain, but may be distinct from the test root, so the best solution will likely be to add a new setting to explicitly define an alternate test root, something like "python.testing.testroot".Oh, and what in particular connected the "python.testing.cwd" setting to pytest's
--rootdirfrom your perspective?ericsnowcurrently commentedon Jan 14, 2020
Also, have you tried the multi-root workspace functionality of VS Code. You create a workspace and then "Add Folder to Workspace...". Consider adding each project root as a workspace root to see if that solves the problem for you.
chrisgervang commentedon Jan 14, 2020
ericsnowcurrently commentedon Jan 14, 2020
Sounds good. We'll make a decision on the request.
As to multi-root workspace, the extension determines the current workspace based on the file in the active editor. Really it just calls the
workspace.getWorkspaceFolder()function in VS Code's API. I'm not sure how it decides it several roots could match for a file.14 remaining items
perseus-fyi commentedon Oct 27, 2020
related to #12538
ProLoser commentedon Nov 6, 2020
Couldn't this be fixed by removing
--rootdirfromoptionsWithArgsToRemove?This way if people wish to override the arg they can.
Don't override user provided `--rootdir` in pytest args. (#17509)
bhrutledge commentedon Nov 2, 2021
Revisiting this, because it's still not clear to me how to configure my project. I've put up a minimal project with the same layout at https://github.com/bhrutledge/vscode-pytest-workspace. The latest commit gets it working with a hack, but the commit history shows my other attempts to get it working.
Karthik Nadig (@karthiknadig) I think #17509 helped with test discovery by defaulting
--rootdirtocwd, but that doesn't seem carry over to running tests. It also seems to force the test discovery path to be the value of--rootdir, instead of the value provided inpytestArgsor thetestpathsoption inpytest.ini.This is on the latest Insiders builds of VS Code and the Python extension.
bhrutledge commentedon Nov 2, 2021
After some more digging, I think I've found two issues.
First, during test discovery, it seems that
--rootdirtakes precedence over any positional argments. It's not clear why this behavior exists, but it looks like it was added in June 2018 by Don Jayamanne (@DonJayamanne), and moved in Aug 2021.vscode-python/src/client/testing/testController/pytest/arguments.ts
Lines 137 to 141 in b6c0d3e
Second, during test running,
--rootdiris set to${workspaceFolder}if it's not set inpytestArgs:vscode-python/src/client/testing/testController/pytest/runner.ts
Lines 89 to 93 in b6c0d3e
However, this is different than test discovery, which uses
cwdor${workspaceFolder}:vscode-python/src/client/testing/testController/pytest/pytestController.ts
Lines 178 to 193 in b6c0d3e
It seems like this logic is already present in the
optionsobject, so this might just be usingoptions.cwdinstead ofoptions.workspaceFolder.fsPath:vscode-python/src/client/testing/testController/pytest/pytestController.ts
Lines 288 to 299 in b6c0d3e
eleanorjboyd commentedon Dec 6, 2023
Hello! I have just found and caught up on this issue and wanted to get feedback following the rewrite for our testing infrastructure.
I do not think this feature request is fixed. The code now has
--rootdirlikely taking precedence because I do not do any editing to user args as they are getting passed in. Deciding to take the cwd over the -rootdir is a more opinionated decision for the extension to take as we are ignoring a user inputted value. There is obviously interest here but I am unsure of if there are people that use it the reverse way (expecting --rootdir to take precedence).Luciana Abud (@luabud) and Karthik Nadig (@karthiknadig), we do not have a precedence set for which one is used if both are provided do we? What would the risks with adopting something like this be in terms of potentially breaking other users by changing the order of precedence.
add python.testing.cwd to pytest discovery if present (microsoft#24384)
eleanorjboyd commentedon Dec 2, 2024
Verification steps:
"python.testing.cwd"to a random path of your choosing--rootdirand it is equal to that random path of your choosinglog output should look something like this, as an example
2024-12-02 11:28:22.784 [info] > ~/.pyenv/versions/3.11.9/bin/python -m pytest -p vscode_pytest --collect-only . --rootdir=.