Repository navigation
Unit tests aren't discovered in sub-folders #11969
Description
Environment data
- VS Code version: 1.45.1
- Extension version (available under the Extensions sidebar): 2020.5.80290
- OS and version: Arch Linux (kernel: linux 5.6.14.arch1-1)
- Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.3 64-bit
- Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pipenv
- Relevant/affected Python packages and their versions: unittest
- Relevant/affected Python-related VS Code extensions and their versions: None
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"set to; more info How to update the language server to the latest stable version #3977): True - Value of the
python.languageServersetting: Microsoft
Actual behaviour
In main tests/ directory are some tests and some other files needed. Those work fine until a subdirectory is created, which contains 2 tests named test_decorators.py and test_pagination.py, this subdirectory also contains the __init__.py so that the tests can be recognized. After this subdirectory is created, the tests from main directory (tests/) disappear and the ones from subdirectory are not displayed either, a popup saying that no tests were discovered and asking me to configure the test framework (It is configured properly)
Once the __init__.py file is added and I rediscover the tests, this box pops up.

Expected behaviour
When I add the subdirectory to main tests/ directory and search for the tests, both the tests from my main directory and the subdirectory should be found.
Steps to reproduce:
- Create directory for tests in root of your project
tests/ - Configure the framework to use unittests and recognise files labeled: test_*.py
- Add some tests in it and let the framework discover them [To this point everything is as expected]
- Add a subdirectory in your
tests/directory and place some other tests in it - Run test discovery again
- No tests were found
Logs
Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)
[Extension Host] Info Python Extension: 2020-05-24 16:13:27: getActivatedEnvironmentVariables, Class name = E, completed in 1ms, has a truthy return value, Arg 1: <Uri:/home/itsdrike/Programming/Python/Projects/Discord-Bot>, Arg 2: undefined, Arg 3: undefined
[Extension Host] Info Python Extension: 2020-05-24 16:13:27: > ~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -c "import unittestloader = unittest.TestLoader()suites = loader.discover("./tests", pattern="test_*.py")print("start") #Don't remove this linefor suite in suites._tests: for cls in suite._tests: try: for m in cls._tests: print(m.id()) except: pass"
[Extension Host] Info Python Extension: 2020-05-24 16:13:27: cwd: ~/Programming/Python/Projects/Discord-Bot

Activity
ItsDrike commentedon May 26, 2020
Any ideas why this is happening or what ca I do to prevent it?
[-]Unit tests aren't discovered in sub-floders[/-][+]Unit tests aren't discovered in sub-folders[/+]ericsnowcurrently commentedon May 27, 2020
ItsDrike (@ItsDrike), thanks for the report. Test discovery definitely shouldn't be behaving this way.
What happens when you run
~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -m unittest discover -p 'test_*.py' -t '.' -s './tests'?(roughly equivalent to the command from the log,
~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -c 'import unittest; loader = unittest.TestLoader(); suites = loader.discover("./tests", pattern="test_*.py"); print("start"); for suite in suites._tests: for cls in suite._tests: for m in cls._tests: print(m.id())')ItsDrike commentedon May 27, 2020
When I run the first command (
~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -m unittest discover -p 'test_*.py' -t '.' -s './tests') all tests are found as seen in gif below (you can ignore that log message)but your second command has raised a

SyntaxError:ericsnowcurrently commentedon May 27, 2020
Yeah, that second command (from the log file) is actually multi-line:
The big question is if the two command actually are equivalent. I'm thinking they actually aren't. The "top_level_dir" argument to
loader.discover()wasn't passed in the logged script. It defaults to the start dir ("./tests"), which I expect makes the tests fail. The equivalent command would be~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -m unittest discover -p 'test_*.py' -s './tests'(note the missing-targ). What happens when you run that?ItsDrike commentedon May 27, 2020
Running it as multiline still produces and error, but now it's an
AttributeErrorRunning the `~/.local/share/virtualenvs/Discord-Bot-j5RKKR3Y/bin/python -m unittest discover -p 'test_*.py' -s './tests'` command as suggested produced very odd output:
Seems like all 9 test files were run, but and all of them resulted in an error since the required modules which were supposed to be tested weren't even found.
ItsDrike commentedon May 31, 2020
So is it a bug or should I do something to fix it on my machine?
ericsnowcurrently commentedon Jun 1, 2020
Right. Those failures are what I would expect You should see similar output in the "Python test log" output panel. What do you see there?
ItsDrike commentedon Jun 1, 2020
The initial output was without init, with

__init__.pythere, It only really saysstart10 remaining items
yhgu2000 commentedon Oct 23, 2020
I have came back to this page for about ten times, but nothing changed.......
This bug makes the unittest function of this plugin almost useless. I have already disable it in my projects........
ItsDrike commentedon Oct 24, 2020
Yeah, unit-testing doesn't work at all, I think this should have higher priority than it does now since testing is a very important part of every bigger python project and it's quite rare to have all of the tests in the same directory, not detecting this automatically is a pretty big issue, why is it taking so long to fix?
It's already been over 5 months since I've opened the issue and frankly, that's just unacceptable for an issue like this.
I believe I've done an okay job documenting it and providing enough details about it although I can give you more details if you need them.
I don't often push on issues being fixed, but considering it was almost half of a whole year that has already passed, I'd really appreciate it if you looked into this a little further Eric Snow (@ericsnowcurrently)
karrtikr commentedon Apr 6, 2021
Is this issue still happening with the latest version of the extension?
yhgu2000 commentedon May 11, 2021
Yes, it is.
You can use this minimal reproductive project to test: unittest_bug_example_project.zip.
karrtikr commentedon Jul 28, 2021
@AtomicGu I used your sample repo but tests are discovered just fine.

I see the following in
Python Test logoutput channel,Can you send the output?
karrtikr commentedon Jul 28, 2021
Note we're rewriting the whole testing code with #16769, this will probably be fixed.
karrtikr commentedon Sep 1, 2021
Should be fixed with the new testing work. If not, please open a new issue and we'll be happy to look into it.