Reputation: 1254
I'm trying to make the self-running feature of Visual Studio Code unit tests work. I recently made a change in the directory structure of my Python project that was previously like this:
myproje\
domain\
__init__.py
repositories\
tests\
__init__.py
guardstest.py
utils\
__init__.py
guards.py
web\
And my setup for unittest was like this:
"python.unitTest.unittestArgs": [
"-v",
"-s",
"tests",
"-p",
"*test*.py"
]
After the changes, the structure of the project was as follows:
myprojet\
app\
controllers\
__init__.py
models\
__init__.py
entities.py
enums.py
tests\
res\
image1.png
image2.png
__init__.py
guardstest.py
utils\
__init__.py
guards.py
views\
static\
templnates\
__init__.py
uml\
After that the extension does not discover my tests anymore. I've tried to change the '-s' parameter to "./app/tests"
, ".tests"
, "./tests"
, "app/tests"
, "/app/tests"
, "app.tests"
, unsuccessfully .
Upvotes: 18
Views: 58371
Reputation: 103
In my case, I had same identical issue on Visual Studio Code, but my resolution was slightly different.
I looked at .vscode/settings.json file.
I noticed unittestEnabled and pytestEnabled, but unittestEnabled=true and pytestEnabled=false. Didn't know what the difference was, but I used pytest in my CLI test. So, I turned pytestEnabled = true. Then I went to Testing icon on the left. I clicked a button to discover test again and had to choose couple of options like setting test folder. Now it discovered all my tests and working as expected. Hope this helps someone.
Upvotes: -1
Reputation: 8187
There are 2 reasons that this might not work:
The python Testing
plugin won't find your tests if there is an error in the test script.
To check for a potential error, click the Show Test Output
, then run the tests using Run All Tests
(both buttons are located in the top left, just above where the tests should appear).
If there is an error, it will appear in the OUTPUT
tab.
Check your .vscode/settings.json
, and take the python.testing.unittestArgs
list.
You can debug the discovery of tests in the command line by adding args to the python3 -m unittest discover
command in the command line.
So with this config:
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*test*.py"
]
}
You would launch the command:
python3 -m unittest discover -v -s . -p "*test*.py"
You can play with the args unitl you discover your tests, and modify the args in .vscode/settings.json
accordingly .
Here are the docs for unittest
A common reason for this is that you're trying to run tests with dependencies. If this is the case, you can select your interpreter by running ctrl
+ shift
+ p
and searching Python: Select Interpreter
, then selecting the correct interpreter.
Upvotes: 9
Reputation: 61
It is because some of the imports in the test is not discoverable. when running python -m unittest -h
, the last line of the output is
For test discovery all test modules must be importable from the top level directory of the project.
it is likely that VSCode is running the command without the right PYTHONPATH and other environment variables.
I created __init__.py
and put the following code into it.
import sys
import os
import unittest
# set module path for testing
sys.path.insert(0, "path_in_PYTHONPATH")
# repead to include all paths
class TestBase(unittest.TestCase):
def __init__(self, methodName: str) -> None:
super().__init__(methodName=methodName)
then in the test file, instead of extending unittest.TestCase, do
from test import TestBase
class Test_A(TestBase):
...
Upvotes: 6
Reputation: 1254
The problem was that I was using relative imports in the test module (from ..utils import guards
).
I just changed it to absolute import (from app.utils import guards
) and it all worked again.
Upvotes: 13