Search code examples
pythonvisual-studio-codepython-unittest

How would I test from one subdirectory to another in vscode using unittest?


I have a project that is put together in the format of the python packaging tutorial in the documentation.

Project_Directory/
├── src/
│   └── PROJECT_NAME/
│       ├── __init__.py
│       └── exampleModule.py
└── tests/
    ├── __init__.py
    └── Test_exampleModule.py

I've been using unittest to run tests so far (with a workaround), but I'm moving to vscode and was hoping to use the built-in testing features.

Before now, I was able to cd into src/ and run python3 -m unittest discover .. -p "Test_*.py" to get my tests to run with just from PROJECT_NAME import exampleModule.

Coming to VSCode, the internal testing system can't seem to find my tests while also being able to import the modules. I changed the python testing arguments for unittest, and nothing seemed to be able to replicate the functionality I had before. I don't want to have to change all my testing to be from src.PROJECT_NAME or something like that.


Solution

  • Finally figured it out after looking through multiple other questions, github issues, and poking around in the actual file itself. For some reason, the value passed into python.testing.cwd is... I'm not actually sure, but unittest didn't like what setting "python.testing.cwd": "src" ended up doing.

    To fix the problem and re-create what I had before, my settings.json file needed "python.testing.cwd": "${workspaceFolder}/src". Now my tests run in vscode just like they do in my cli.