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.

[pytest] If present, "python.testing.cwd" expected to change `pytest --rootdir` arg value.

See original GitHub issue

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

https://github.com/microsoft/vscode-python/blob/2b6a8f2d439fe9d5e66665ea46d8b690ac9b2c39/src/client/testing/pytest/services/discoveryService.ts#L53

https://github.com/microsoft/vscode-python/blob/2b6a8f2d439fe9d5e66665ea46d8b690ac9b2c39/src/client/testing/pytest/main.ts#L50-L52

https://github.com/microsoft/vscode-python/blob/2b6a8f2d439fe9d5e66665ea46d8b690ac9b2c39/src/client/testing/pytest/main.ts#L19-L20

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. Open a workspace folder containing python tests that are not in the root directory (i.e. a monorepo).
  2. Configure pytest to test a project directory. 2a. configure “python.testing.cwd” in settings.json to the project root.
  3. Discover tests and observe that all tests are discovered.
  4. 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 (ViewOutput, 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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:24
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ProLosercommented, Nov 6, 2020

Couldn’t this be fixed by removing --rootdir from optionsWithArgsToRemove?

This way if people wish to override the arg they can.

2reactions
dcrystaljcommented, Aug 28, 2020

There is issue in our company we use old pytest v3 and giving --rootdir param is causing problems that we cannot use pytest discovery module. Please add option "python.testing.cwd": null to completely remove --rootdir parameter!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration — pytest documentation
pytest determines a rootdir for each test run which depends on the command line arguments (specified test files, paths) and on the existence...
Read more >
Change pytest rootdir - Stack Overflow
It instructs py.test to set its rootdir property to the current directory (i.e., /home/pi/bluepy ). py.test is usually capable of finding ...
Read more >
pytest Documentation - Read the Docs
pytest discovers all tests following its Conventions for Python test discovery, so it finds both test_ prefixed functions.
Read more >
Effective Python Testing With Pytest
If you've written unit tests for your Python code before, then you may have used Python's built-in unittest module. unittest provides a solid ......
Read more >
Python testing with Pytest: Order of test functions - fixtures
By default pytest will hide any output generated during the tests. So if ... Python 3.5.2, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 rootdir: ...
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