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.

Unit tests aren't discovered in sub-folders

See original GitHub issue

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 #3977): True
  • Value of the python.languageServer setting: 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. image

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:

  1. Create directory for tests in root of your project tests/
  2. Configure the framework to use unittests and recognise files labeled: test_*.py
  3. Add some tests in it and let the framework discover them [To this point everything is as expected]
  4. Add a subdirectory in your tests/ directory and place some other tests in it
  5. Run test discovery again
  6. No tests were found

Peek 2020-05-24 16-07

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:23 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ItsDrikecommented, Oct 24, 2020

Any progress on this?

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…

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 @ericsnowcurrently

1reaction
ericsnowcurrentlycommented, May 27, 2020

Yeah, that second command (from the log file) is actually multi-line:

~/.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())
'

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 -t arg). What happens when you run that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python unittest discovery with subfolders - Stack Overflow
The python documentation says that it should automatically discover my test in the subfolders. But it does not.
Read more >
Python Unittest Test Explorer no longer searching subfolders ...
I have unit tests in <project>\data\tests and that's been fine until the update this morning but now I have to explicitly set Root...
Read more >
Unit Test Quickstart - Gramps
First, some general procedural and structural notes. unit tests are usually created to verify correct functional behavior of a single module of source...
Read more >
matlab.unittest.TestSuite.fromFolder - MathWorks
Whether to include tests from any of the subfolders, excluding class and private folders, specified as false or true ( logical 0 or...
Read more >
Running Tests in Subdirectories - Laracasts
Class Tests\Unit\UnitTest located in ./tests/Unit/UnitsTest.php does not comply with psr-4 autoloading standard. Skipping. Copy Code
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