Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode python fail to discover unit tests recursively

I have a simple python unit test like this...

project/ calc/ calc.py calc_test.py

calc.py has two functions sum() and diff(), calc_test.py has two test cases to test those functions, using python inbuilt unittest framework.

I'm using Visual Studio code as my editor. If I set the top-level workspace as "project" directory, then VS Code fails the discover any tests. However, if reopen VS Code under "calc" directory, then it identifies the tests and displays nice overlays ("Run test", "Debug Test" etc..).

If I move the unit tests one level above (to proj/), then it works too. My recollection is that the argument "-s" and the directory "." should look for "test.py" files recursively from the top level directory where "python -m unittest <..>" command is run.

"python.unitTest.unittestEnabled": true,
"python.unitTest.unittestArgs": [
    "-v",
    "-s",
    ".",
    "-p",
    "*test*.py"
],

From proj/ I manually ran "python -m unittest discover -s . -p "test.py" -v, the tests were discovered and ran without any issues. I suppose VS Code should also be running similar commands internally, but something is missing here.

This makes it look like that VS Code fails to look for tests recursively from the top directory. I was about to open a issue at the Python extension for VS Code (https://github.com/Microsoft/vscode-python/issues/new), but wanted to check here first as per contribution guidelines.

like image 903
dnates Avatar asked Feb 25 '18 05:02

dnates


2 Answers

I'm posting this as an answer because I'm frustrated that I can't post comments with my current reputation level, but this is not solved by adding __init__.py to the folder in which you're testing. I'll go ahead and attempt to answer this since it is now an answer.

When you're testing modules within a folder of a large project, simply use the file menu to Open Folder, open that actual folder as a VSC view, then do your Python Test Configuration in the root directory (or up to one directory deep), which generates that init.py file (which clearly isn't the cause of VSC not being able to find nested tests).

This solves the problem (although not allowing you to keep the entire project open), and it also simplifies your file tree to the specific task at hand.

I have tried several approaches including various commands but this seems like a fairly elegant solution to this weird behaviour.

Yes I recognize this question is ancient but clearly still relevant to the most recent version of VSC.

like image 85
Liam Duncan Avatar answered Nov 20 '22 09:11

Liam Duncan


The resolution is to add __init__.py in each folder where test case are located.

From Python testing in Visual Studio Code:

Tip: Sometimes tests placed in subfolders aren't discovered because such test files cannot be imported. To make them importable, create an empty file named __init__.py in that folder.

like image 23
xiaoguazh Avatar answered Nov 20 '22 09:11

xiaoguazh