简体   繁体   中英

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.

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.

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.

如果您在 VS Code 中打开project ,那么您希望将-s设置为./calc以获取您的测试(这实际上与 VS Code 无关,并且完全由unittest包驱动)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM