简体   繁体   English

VS Code Python单元测试“未运行测试”

[英]VS Code Python unittest “No tests ran”

I'm setting up python unit tests in VS Code using unittest, and running into some issues. 我正在使用unittest在VS Code中设置python单元测试,并遇到一些问题。

I am able to run my tests by selecting Run Tests from the status bar, and by right-clicking my test file in Explorer. 我可以通过从状态栏中选择“运行测试”并在资源管理器中右键单击我的测试文件来运行测试。 When I do either of these, I see unit test output (eg "test_name ... ok" and "Ran 1 test in 0.002s") in the Python Test Log window, but I get a VS Code notification saying "No tests ran, please check the configuration settings for the tests." 当我执行这些操作时,在Python测试日志窗口中会看到单元测试输出(例如,“ test_name ... ok”和“ Ran 1 test in 0.002s”),但是我收到一条VS代码通知,内容为“未运行任何测试,请检查测试的配置设置。” I am also not seeing the Run Test | 我也没有看到运行测试| Debug Test code lens in the test file. 在测试文件中调试测试代码镜头。

I have followed the setup instructions in the VS Code docs here: https://code.visualstudio.com/docs/python/unit-testing , but I'm wondering if there are other steps I'll need to follow to get proper integration with the IDE? 我已经按照VS Code文档中的设置说明进行了操作: https : //code.visualstudio.com/docs/python/unit-testing ,但是我想知道是否还需要执行其他步骤来获得正确的设置与IDE集成?

I am using VS Code version 1.12.1, on Windows. 我在Windows上使用VS Code版本1.12.1。

See unittest test discovery 查看单元测试测试发现

Unittest supports simple test discovery. Unittest支持简单的测试发现。 In order to be compatible with test discovery, all of the test files must be modules or packages (including namespace packages) importable from the top-level directory of the project (this means that their filenames must be valid identifiers). 为了与测试发现兼容,所有测试文件都必须是可从项目的顶级目录导入的模块或包(包括名称空间包)(这意味着它们的文件名必须是有效的标识符)。

I read the unittest Basic Example there is not something about make tests files to a module. 我读了unittest Basic Example ,没有关于将测试文件添加到模块的内容。
So that's Why you can see the Run Test | Debug Test code lens in the test 这就是为什么您可以看到“ Run Test | Debug Test code lens in the test Run Test | Debug Test code lens in the test file Run Test | Debug Test code lens in the test 文件中 Run Test | Debug Test code lens in the test
But can't make test discovery discover it. 但是不能使测试发现发现它。

To make it to a module, you should create a __init__.py file in the same directory. 要使其成为模块,您应该在同一目录中创建__init__.py文件。

ps: for others who can't see Run Test | Debug Test ps:对于看不见Run Test | Debug Test其他人Run Test | Debug Test Run Test | Debug Test code lens above the test, which means you can't make the unittest discover the single file as a test file. Run Test | Debug Test代码镜头的测试,这意味着你不能做出上述unittest发现的单个文件作为测试文件。
Check the -p pattern maybe the right move. 检查-p 模式,也许是正确的举动。

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

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