Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest xdist should run with -n0 in debug #336

Closed
elad-eyal opened this issue May 6, 2024 · 9 comments · Fixed by microsoft/vscode-python#23539
Closed

pytest xdist should run with -n0 in debug #336

elad-eyal opened this issue May 6, 2024 · 9 comments · Fixed by microsoft/vscode-python#23539
Assignees
Labels
feature-request Request for new features or functionality needs community feedback

Comments

@elad-eyal
Copy link

elad-eyal commented May 6, 2024

when debugging a (single) test with pytest, invocation of pytest should add -n 0 . This way multiple runners will not be used and start-up time is reduced.

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label May 6, 2024
@eleanorjboyd
Copy link
Member

Hi! We are looking to make changes to how we pass args so you could have multiple config types: microsoft/vscode-python#21845. Would this solve your issue? Otherwise it would need to be a separate feature request. Our general policy at this time is we are trying to edit the pytest args as little as possible since it is very hard to do right. With this in mind, I am hesistant to do a feature request that causes arg editing and this would require a larger team conversation if you do want to make this a separate feature request. Thanks

@elad-eyal
Copy link
Author

i guess a separate python.testing.pytestArgs for debug would be a suitable workaround. i would think you'd want this behavior to be out of the box but user configuration is fine too.

cheers

@DetachHead
Copy link

ideally, it should dynamically decide how many runners to use based on how many tests are selected: pytest-dev/pytest-xdist#853

i came up with a workaround that works well with vscode's new pytest test adapter: pytest-dev/pytest-xdist#853 (comment)

// .vscode/settings.json

{
    "python.testing.pytestArgs": ["-n", "auto"]
}
# conftest.py

@hookimpl(wrapper=True)
def pytest_xdist_auto_num_workers(config: Config) -> Generator[None, int, int]:
    """determine how many workers to use based on how many tests were selected in the test explorer"""
    num_workers = yield
    if "vscode_pytest" in config.option.plugins:
        return min(num_workers, len(config.option.file_or_dir))
    return num_workers

this works because vscode passes each test to pytest as separate arguments (eg. pytest test_foo.py::test_foo test_foo.py::test_bar), so you can guess how many tests are going to run before they get collected.

edit: looks like this relies on vscode's new test adapter, which isn't yet enabled for all users. to enable it, you need to add this to your user settings.json (doesn't work in the workspace one unfortunately):

{
    "python.experiments.optInto": ["pythonTestAdapter"]
}

@paulacamargo25 paulacamargo25 added feature-request Request for new features or functionality needs community feedback and removed triage-needed Needs assignment to the proper sub-team labels May 30, 2024
@paulacamargo25
Copy link
Contributor

Thanks for the feature request! We are going to give the community 60 days to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

@DetachHead
Copy link

im going to try to make a PR adding this feature, because i'm pretty sure that smalll pytest_xdist_auto_num_workers hook in my workaround is all that's needed

@DetachHead
Copy link

@eleanorjboyd
Copy link
Member

Thank you @DetachHead for contributing!

DonJayamanne pushed a commit to DonJayamanne/pythonVSCode that referenced this issue Jun 24, 2024
…e based on count of selected tests (#23539)

fixes microsoft/vscode-python-debugger#336

---------

Co-authored-by: detachhead <[email protected]>
Co-authored-by: Karthik Nadig <[email protected]>
eleanorjboyd pushed a commit to eleanorjboyd/vscode-python that referenced this issue Jun 28, 2024
…e based on count of selected tests (microsoft#23539)

fixes microsoft/vscode-python-debugger#336

---------

Co-authored-by: detachhead <[email protected]>
Co-authored-by: Karthik Nadig <[email protected]>
@shakhovski
Copy link

shakhovski commented Jul 12, 2024

@DetachHead, It doesn't work with pytest 6.2.5.

2024-07-12 19:34:07.753 [error] /home/???/test_venv/lib/python3.10/site-packages/_pytest/config/__init__.py:318: PluggyTeardownRaisedWarning: A plugin raised an exception during an old-style hookwrapper teardown.
Plugin: helpconfig, Hook: pytest_cmdline_parse
AttributeError: module 'pytest' has no attribute 'Config'
...
2024-07-12 19:34:07.755 [error]     def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]:
AttributeError: 
2024-07-12 19:34:07.755 [error] module 'pytest' has no attribute 'Config'

Commenting out your change allows Test Explorer to work.

@eleanorjboyd
Copy link
Member

@shakhovski we are looking to no longer support any pytest version below 7, if possible it is best to update to a newer version. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality needs community feedback
Projects
None yet
5 participants