question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Debugging unit tests in a sub directory

See original GitHub issue

I’m working on Windows 10 with VSCode and the Python plugin. These are all up to date as far as I can tell.

I’ve some unittest tests that sit in a subdirectory of my main working directory called .\src. These in return load files in a second sub directory .\src\test\test.dat. The tests simply refer to this as test\test.dat as they assume the current directory is .\src.

I’m having real trouble getting testing setup so that this works. I’ve been able to run these in the past but debugging a test wasn’t working. I changed my settings to:

    "python.linting.pydocstyleEnabled": true,
    "python.testing.unittestArgs": [
        "-p",
        "*test.py",
        "-s",
        "./src",
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,    
    "python.testing.unittestEnabled": true

And debugging started working, but the tests don’t find the data file. Because the current working directory is just .\. If I change the settings to this debugging stops working. Surprisingly though vscode can still find and run the tests, even though my reading of this is that it should be looking in .\src\src which does not exist.

     "python.testing.unittestArgs": [
        "-p",
        "*test.py",
        "-s",
        "./src",
    ],
    "python.testing.cwd": ".\\src",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,    
    "python.testing.unittestEnabled": true

If I change the settings to this it can’t even discover the tests:

    "python.testing.unittestArgs": [
        "-p",
        "*test.py"
    ],
    "python.testing.cwd": ".\\src",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,    
    "python.testing.unittestEnabled": true

I’m now totally confused as to how to set this up so that running tests works and debugging tests works and they both find the test files.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
karrtikrcommented, May 7, 2020

Hey @martingbrown , I am able to spot multiple issues here:

  • It seems like debugging tests doesn’t pick up the cwd from the setting python.testing.cwd. Instead you’ve to create the following entry in launch.json - I can confirm this works, so this should get your testing setup
   {
        "name": "Unit Tests",
        "type": "python",
        "request": "test",
        "cwd": "${workspaceFolder}/src"
    } 

Multiple places to set cwd is confusing, so we’ll address these as part of https://github.com/microsoft/vscode-python/issues/8339.

The above alone should get you working, but I found some more issues:

  • Looks like ${workspaceFolder} & . in ${workspaceFolder}/src & ./src is not being resolved correctly (1), this is an issue (created https://github.com/microsoft/vscode-python/issues/11671). For now, please try using the full path to the source directory instead, <full path to workspace folder>/src

For instance the following worked for me

  "python.testing.unittestArgs": [
    "-v",
    "-s",
    "C:/Users/karraj/Desktop/Desktop/test_demo/src",
    "-p",
    "*test*.py"
  ],
  • But surprisingly using backslashes is path (C:\\Users\\karraj\\Desktop\\Desktop\\test_demo\\src) is not working. That’s another issue (2). As it perfectly works with python.pythonPath and other settings. Created issue https://github.com/microsoft/vscode-python/issues/11670 ~I think the cwd is not being set for you because of (2). So try using "python.testing.cwd": "./src" instead of "python.testing.cwd": ".\\src". Running the tests using the play button should work after that.~ - This didn’t work, I’ll open up a new issue

  • Seems like python.testing.cwd is not working anymore. Created issue https://github.com/microsoft/vscode-python/issues/11669

Apologies for so many issues, let me know if this helps. Yours reminded me of https://github.com/microsoft/vscode-python/issues/7176, where the guy used the above to solve his issues.

0reactions
karrtikrcommented, May 7, 2020

No worries. But does running tests using the green play button work? From my understanding, as python.testing.cwd don’t work, it shouldn’t work either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debug unit tests with Test Explorer - Visual Studio (Windows)
In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.
Read more >
Trying to debug Python test cases from a file in a large project ...
Try setting source folders in Settings (Ctrl+Alt+S). Choose Project, then Project Structure. Highlight a folder then click the Sources ...
Read more >
Testing Python in Visual Studio Code
To customize settings for debugging tests, you can specify "purpose": ["debug-test"] in the launch.json file in the .vscode folder from your workspace. This ......
Read more >
Unit testing - Debugging and testing | Mbed OS 6 ...
This document describes how to write and test unit tests for Mbed OS. ... Unit tests are located in the tests/UNITTESTS subdirectory of...
Read more >
Using Visual Studio Code to debug Jest based unit tests
Step 1 Enable debugging in our project · We will click on the debug icon (left hand sidebar). · Click on the add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found