简体   繁体   English

如何在 Visual Studio 代码的 Python 扩展中选择一个子文件夹作为包含测试的目录

[英]How do I select a sub-folder as a directory containing tests in Python extension for Visual studio code

I am using VScode with python code and I have a folder with sub-directories (2-levels deep) containing python tests.我正在将 VScode 与 python 代码一起使用,并且我有一个包含 python 测试的子目录(2 级深)的文件夹。

When I try "Python: Discover Tests" it asks for a test framework (selected pytest) and the directory in which tests exist.当我尝试“Python: Discover Tests”时,它要求提供一个测试框架(选定的 pytest)和测试所在的目录。 At this option, it shows only the top-level directories and does not allow to select a sub-directory.在此选项中,它只显示顶级目录,不允许选择子目录。

I tried to type the directory path but it does not accept it.我试图输入目录路径,但它不接受。

Can someone please help on how to achieve this?有人可以帮助如何实现这一目标吗?

There are two options.有两种选择。 One is to leave the selection as-is and make sure your directories are packages by adding __init__.py files as appropriate.一种是保持选择__init__.py ,并通过适当添加__init__.py文件来确保您的目录是包。 The other is you can go into your workspace settings and adjust the "python.testing.pytestArgs" setting as appropriate to point to your tests.另一个是您可以进入工作区设置并根据需要调整"python.testing.pytestArgs"设置以指向您的测试。

Try opening the "Output" log (Ctrl+Shift+U) and run "Python: Discover Tests".尝试打开“输出”日志 (Ctrl+Shift+U) 并运行“Python: Discover Tests”。 Alternatively, you may type pytest --collect-only into the console.或者,您可以在控制台中键入pytest --collect-only Maybe you are experiencing some errors with the tests themselves (such as importing errors).也许您在测试本身时遇到了一些错误(例如导入错误)。

Also, make sure to keep __init__.py file in your "tests" folder.另外,请确保将__init__.py文件保留在“tests”文件夹中。

I am keeping the pytest "tests" folder within a subdirectory, and there are no issues with VS Code discovering the tests.我将 pytest“tests”文件夹保存在一个子目录中,并且 VS Code 发现测试没有问题。

暂无
暂无

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

相关问题 如何设置 Visual Studio Code Python 扩展的根目录? - How to set the root directory for Visual Studio Code Python Extension? Python - 将根目录名称与所有子文件夹名称连接起来 - Python - concatenate root directory name with all sub-folder names 如何在pycharm的另一个子文件夹中设置数据文件的python搜索目录? - How to set the python searching directory of data file in another sub-folder in pycharm? 如何使用Python扩展配置Visual Studio代码而不是抱怨无法导入模块? - How do I configure Visual Studio Code with Python extension to not complain about inability to import modules? 如何进行递归子文件夹搜索并返回列表中的文件? - How to do a recursive sub-folder search and return files in a list? 如何将子文件夹中的文件作为参数传递给python中的函数 - How to pass a file in sub-folder as parameter to a function in python 在 visual studio 代码中,如何在不同的文件夹中运行相同的 python conda 环境 - In visual studio code how do I run the same python conda environment in a different folder 我如何在使用 Visual Studio Code 的同时解决 Python 中的“FileNotFoundError: [Errno 2] No such file or directory:”? - How do i solve “FileNotFoundError: [Errno 2] No such file or directory:” in Python whilst using Visual Studio Code? python导入的子文件夹名称相同 - Same sub-folder names for python imports 从python中的子文件夹层次结构导入 - Importing from sub-folder hierarchy in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM