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

Add "Run Tests" to launch.json #11932

Open
ronif opened this issue May 21, 2020 · 15 comments
Open

Add "Run Tests" to launch.json #11932

ronif opened this issue May 21, 2020 · 15 comments
Assignees
Labels
area-testing feature-request Request for new features or functionality needs proposal Need to make some design decisions

Comments

@ronif
Copy link

ronif commented May 21, 2020

Hi,

Some of the tests (via unittest) I want to run locally require specific env variables. These tests also run in CI with different variables, so I would like to set them only locally in vscode. The "Debug Tests" launch.json feature allows me to do that, but only when debugging. I think a "Run Tests" option in launch.json would be really helpful for this.

@ronif ronif added triage-needed Needs assignment to the proper sub-team feature-request Request for new features or functionality labels May 21, 2020
@karthiknadig

This comment was marked as outdated.

@karthiknadig
Copy link
Member

@ronif Ah! Just realized this is for "Run Tests" specifically. The previous comment applies specifically to debugging. We should make sure that if there is a test configuration in launch.json we should just run it under the debugger with noDebug set to true.

@karthiknadig
Copy link
Member

Thank you for the suggestion! We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision.

@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label May 22, 2020
@0az
Copy link

0az commented May 27, 2020

Does this apply to the envFile key?

@abid-mujtaba
Copy link

This feature will also allow one to use preLaunchTask while running tests, not just while debugging.

@NickVeld
Copy link

NickVeld commented Mar 31, 2021

Continuing the previous comment:

... and will also allow one to set in a distinct configuration PYTEST_ADDOPTS for the coverage counting with custom arguments.

Now the only option at least to enable coverage and do not break debugging exists. It is to add --no-cov to the debug configuration : https://code.visualstudio.com/docs/python/testing#_pytest-configuration-settings

Some troubles connected with current approach:

  • It triggers WARNING: Coverage disabled via --no-cov switch! (that cannot be fully disabled with --disable-pytest-warnings or -p no:warnings). And you cannot see a green tick near a passed test only a gray question mark.
  • It does not work if you are trying to overwrite PYTEST_ADDOPTS in .env, pytestArg must be used instead of .env . Somewhy the content of .env cannot be overwritten.

@SebastianB12
Copy link

So this is currently just working for test debugging, but not for just running the test with the Testing-Ribbon, right?

        {
            "name": "Test config",
            "type": "python",
            "request": "test", // <--- this is the magic
            "env": {
            // add env variables here
            }
        }

Do I have any other chance to set environment variables before running all tests with the Testing-Ribbon? I'm using Python with Pytest and can now debug tests with the Testing-Ribbon from VSCode, but the config seems not to be used when just running the test, without debugging.

@karthiknadig
Copy link
Member

karthiknadig commented Jan 19, 2022

For run scenario, the best option would be set up all of the environment variables you need in a terminal and then launch VS code from there using code . or code <project-dir>.

We are currently working on improving the test adapter #17242, after that we should be in a better place to tackle these scenarios.

@SebastianB12
Copy link

Another solution I found, was just to add an python.envFile to the settings.json:

    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.envFile": "${workspaceFolder}/.env"

not ideal, because I guess that is now also setting/overwriting the envs for normal debugging, which I defined before in the launch.json file?

@boatcoder
Copy link

In 2022, this appears to no longer function:

        {
            "name": "Test Config",
            "type": "python",
            "request": "test",  // <--- this is the undocumented magic
            "env": {
                "PYTEST_ADDOPTS": "--numprocesses 10 --make-pytest-bail"
            },
        },

Pytest launches without any of these options added (and 10 processes are not created, verified with ps -ax)

@karthiknadig
Copy link
Member

@boatcoder This is now changed a bit, try this:

        {
            "name": "Test Config",
            "type": "python",
            "request": "launch",
            "env": {
                "PYTEST_ADDOPTS": "--numprocesses 10 --make-pytest-bail"
            },
            "purpose": ["debug-test"]
        },

@ViswanathB
Copy link

"purpose": ["debug-test"] is the key.

@NiKeYiGuN
Copy link

@boatcoder This is now changed a bit, try this:

        {
            "name": "Test Config",
            "type": "python",
            "request": "launch",
            "env": {
                "PYTEST_ADDOPTS": "--numprocesses 10 --make-pytest-bail"
            },
            "purpose": ["debug-test"]
        },

what do I need to do for unittest?

@moleary-gsa
Copy link

Our team are moving to vscode because of the dev-container support and this is the biggest sticking point they have when moving from pycharm - any update?

@rvanlaar
Copy link

My goal is to have a debugging session where I can use "debug test" from a test and be able to set breakpoints in library code.

Adding the "purpose" option helps. However the behavior between regular debugging and with this is different.
When at a breakpoint it doesn't get to the right point in the call stack automatically.

What am I missing here?

        {
            "name": "Debug Tests",
            "type": "debugpy",
            "request": "launch",
            "console": "internalConsole",
            "justMyCode": false,
            "django": true,
            "purpose": ["debug-test"]
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing feature-request Request for new features or functionality needs proposal Need to make some design decisions
Projects
None yet
Development

No branches or pull requests