简体   繁体   English

vscode python无法递归地发现单元测试

[英]vscode python fail to discover unit tests recursively

I have a simple python unit test like this...我有一个像这样简单的python单元测试......

project/ calc/ calc.py calc_test.py项目/ 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. calc.py 有两个函数 sum() 和 diff(),calc_test.py 有两个测试用例来测试这些函数,使用 python 内置的 unittest 框架。

I'm using Visual Studio code as my editor.我使用 Visual Studio 代码作为我的编辑器。 If I set the top-level workspace as "project" directory, then VS Code fails the discover any tests.如果我将顶级工作区设置为“项目”目录,那么 VS Code 将无法发现任何测试。 However, if reopen VS Code under "calc" directory, then it identifies the tests and displays nice overlays ("Run test", "Debug Test" etc..).但是,如果在“calc”目录下重新打开 VS Code,它会识别测试并显示很好的覆盖(“运行测试”、“调试测试”等)。

If I move the unit tests one level above (to proj/), then it works too.如果我将单元测试移动到上一级(到 proj/),那么它也可以工作。 My recollection is that the argument "-s" and the directory "."我记得是参数“-s”和目录“。” should look for " test .py" files recursively from the top level directory where "python -m unittest <..>" command is run.应该从运行“python -m unittest <..>”命令的顶级目录递归查找“ test .py”文件。

"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.从 proj/ 我手动运行“python -m unittest discover -s . -p” test .py” -v,测试被发现并运行没有任何问题。我想 VS Code 也应该在内部运行类似的命令,但有些东西是这里不见了。

This makes it look like that VS Code fails to look for tests recursively from the top directory.这使得 VS Code 看起来无法从顶级目录递归查找测试。 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.我正要在 VS Code 的 Python 扩展( https://github.com/Microsoft/vscode-python/issues/new )上打开一个问题,但想根据贡献指南首先在这里检查。

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.我将此作为答案发布,因为我对无法以我当前的声誉级别发表评论感到沮丧,但这并不能通过将__init__.py添加到您正在测试的文件夹中来解决。 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).当您在大型项目的文件夹中测试模块时,只需使用file菜单打开文件夹,将实际文件夹作为 VSC 视图打开,然后在根目录(或最多一个目录深度)中执行Python Test Configuration ,它会生成那个init .py 文件(这显然不是 VSC 无法找到嵌套测试的原因)。

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.是的,我承认这个问题很古老,但显然仍与最新版本的 VSC 相关。

The resolution is to add __init__.py in each folder where test case are located.解决方法是在测试用例所在的每个文件夹中添加__init__.py

From Python testing in Visual Studio Code :Visual Studio Code 中的 Python 测试

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.要使它们可导入,请在该文件夹中创建一个名为__init__.py的空文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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