Skip to content

Add "Run Tests" to launch.json #11932

Open

Description

@ronif

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.

Activity

  1. added
    triage-neededNeeds assignment to the proper sub-team
    feature-requestRequest for new features or functionality
    on May 21, 2020
  2. karthiknadig commented on May 22, 2020

    @karthiknadig
  3. karthiknadig commented on May 22, 2020

    @karthiknadig
    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.

  4. karthiknadig commented on May 22, 2020

    @karthiknadig
    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.

  5. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on May 22, 2020
  6. 0az commented on May 27, 2020

    @0az

    Does this apply to the envFile key?

  7. abid-mujtaba commented on Mar 4, 2021

    @abid-mujtaba

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

  8. NickVeld commented on Mar 31, 2021

    @NickVeld

    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.
  9. SebastianB12 commented on Jan 19, 2022

    @SebastianB12

    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.

  10. karthiknadig commented on Jan 19, 2022

    @karthiknadig
    Member

    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.

  11. SebastianB12 commented on Jan 19, 2022

    @SebastianB12

    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?

  12. boatcoder commented on Oct 6, 2022

    @boatcoder

    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)

  13. karthiknadig commented on Oct 6, 2022

    @karthiknadig
    Member

    Mark Jones (@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"]
            },
    
  14. ViswanathB commented on Jan 9, 2023

    @ViswanathB

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

  15. NiKeYiGuN commented on Apr 13, 2023

    @NiKeYiGuN

    Mark Jones (@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?

  16. moleary-gsa commented on Jul 14, 2023

    @moleary-gsa

    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?

  17. rvanlaar commented on Jun 14, 2024

    @rvanlaar

    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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions