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

Debugging unit tests in a sub directory #11587

Closed
martingbrown opened this issue May 5, 2020 · 5 comments
Closed

Debugging unit tests in a sub directory #11587

martingbrown opened this issue May 5, 2020 · 5 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster

Comments

@martingbrown
Copy link

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.

@ghost ghost added the triage-needed Needs assignment to the proper sub-team label May 5, 2020
@karthiknadig karthiknadig added triage bug Issue identified by VS Code Team member as probable bug labels May 5, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label May 5, 2020
@karrtikr
Copy link

karrtikr commented May 5, 2020

Unittest args require a -s flag to work. However the following didn't work because

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

Because ./src in python.testing.unittestArgs is resolved to ./src/src as cwd is set to ./src, and as that directory doesn't exist, discovering tests fail.

You can simply use the full path to specify the test location, or better use the variable ${workspaceFolder}

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

Let me know if that works.

@karrtikr karrtikr added the info-needed Issue requires more information from poster label May 5, 2020
@martingbrown
Copy link
Author

When you say "the following didn't work", it did work to a degree. With the settings like the following, it discovers the tests and runs them. But the tests are not run with the .\src directory as the working directory. And also debugging tests doesn't seem to work.

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

I changed my settings as you suggested to the following but it then fails to discover the tests:

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

@karrtikr karrtikr removed the info-needed Issue requires more information from poster label May 7, 2020
@karrtikr
Copy link

karrtikr commented 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 #8339.

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

For instance the following worked for me

  "python.testing.unittestArgs": [
    "-v",
    "-s",
    "C:/Users/karraj/Desktop/Desktop/test_demo/src",
    "-p",
    "*test*.py"
  ],

Apologies for so many issues, let me know if this helps. Yours reminded me of #7176, where the guy used the above to solve his issues.

@karrtikr karrtikr added the info-needed Issue requires more information from poster label May 7, 2020
@martingbrown
Copy link
Author

I have now the settings.json set like this:

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

And I also have this in launch.json

        {
            "name": "Unit Testing",
            "type": "python",
            "request": "test",
            "cwd": "${workspaceFolder}/src"
        },

And now it all seems to be working.

Thanks for that. I appreiate the effort. If I had time I'd try and crack open the source code and try to help you out.

@karrtikr
Copy link

karrtikr commented 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.

@karrtikr karrtikr closed this as completed May 7, 2020
@ghost ghost removed the triage label May 7, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

3 participants