Skip to content

Testing - Test View for Python pytest #17040

Closed
@karthiknadig

Description

@karthiknadig

Refs: #15750

Complexity: 3

Create Issue


Requirements

  1. You will need python 3.6 or greater.
  2. Install python extension from here: https://pvsc.blob.core.windows.net/extension-builds/ms-python-insiders.vsix
  3. Make sure you have "python.experiments.optInto": ["All"], in your user settings. If you did not have it, add it and reload.

Testing

  1. Open an empty folder in VS Code.
  2. Add following pytest code:
import pytest


def test_passing():
    pass


@pytest.mark.parametrize(("x"), [[0], [1], [10]])
def test_parametrized_passing(x):
    pass


@pytest.mark.skip()
def test_skipping(x):
    pass


def test_failing():
    assert 0
  1. Be sure to name your file test_<name>.py.
  2. Create a virtual env in the project folder (we recommend this to avoid installing packages to your global environment). You can do this by running py -3 -m venv .venv (on windows), or python3 -m venv .venv (on linux/mac). Note: for nix/mac users you may have to install venv depending on your distribution.
  3. Once you have the virtual env, select it using the Python: select interpreter command or from the status bar.
  4. Now you have to configure tests using Python: configure tests... command from the command pallet, or go the test view and configure by using the test welcome view, or you can run tests to get a prompt to configure. Select pytest and follow the prompt.
  5. Depending on the scenario you may either get a prompt to install pytest or the extension will install pytest for you. If for some reason it does not install for you or get the prompt (file a bug). But do install it manually using this command from a activated terminal (see the .venv in the prompt indicated it is activated)
    image
  6. You should see the tests in the test view. Run the tests.
  7. Add your own test methods, or add another test file, the test UI should update accordingly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions