Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Run only a Single test in Visual Studio Code and Python with output

Does anyone know how to run only one python test with Test Explorer in Visual Studio Code?

I have installed https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter.

I am using pytest.

In the picture below:

enter image description here

If I press "Run Test" then only one test runs, but I do not see any log / output.

To see the output I have to press "Run", but this runs all my tests. This is incredibly frustrating.

I would like to be able to run only the one test with the Visual Studio Code interface and see the log for this run.

like image 704
Geir Ivar Jerstad Avatar asked Sep 05 '25 03:09

Geir Ivar Jerstad


2 Answers

I did not find one elegant way to just run one test and then see the correct sampled/collected output from that test, but I found a way to run only the test I was working on by tagging it with:

@pytest.mark.run_this_test

and hit F5 anywhere in VSCode to just run this test over and over again. The PyTest debug config looks like this:

{
    "name": "PyTest",
    "type": "python",
    "request": "launch",
    "console": "integratedTerminal",
    "module": "pytest",
    "args": [
        "-s", "-m run_this_test"
    ],
    "justMyCode": false,
    "pythonPath": "${config:python.pythonPath}",
    "env": {
        "PYTHONPATH": "${workspaceRoot}"
}

This 'tagging' does not scale in a team obviously, but it works for me alone.

I created a repo containing my solution: https://bitbucket.org/geircode/vscode-testsetup-python

To test it:

  • run the script "requirements.install.bat"
  • open vscode in the root folder
  • select "PyTest" in the "debug and run" tab
  • Hit F5 to start debugging and see the correct output from the test
like image 177
Geir Ivar Jerstad Avatar answered Sep 09 '25 15:09

Geir Ivar Jerstad


From VS Code test explorer it's possible. Hope this helps.

enter image description here

like image 33
Saikat Chakraborty Avatar answered Sep 09 '25 15:09

Saikat Chakraborty