gpt4 book ai didi

VSCode Pytest discovery error but pytest --collect-only works(VSCode PYTEST发现错误,但PYTEST--仅收集有效)

转载 作者:bug小助手 更新时间:2023-10-24 19:02:34 36 4
gpt4 key购买 nike



I'm trying to set up Pytest in VSCode to test a package I'm developing, and keep running into the error "pytest test discovery error". Running pytest --collect-only in the terminal successfully collects the file test_read_data.py, and debugging the test file shows no errors. There was an issue with imports (ModuleNotFoundError) but it was fixed by creating a conda env and installing the dependencies there.

我试图在VSCode中设置PYTEST来测试我正在开发的包,但一直运行错误“PYTEST TEST DISCOVERY ERROR”。在终端运行pytest--Collect-Only成功采集文件test_read_data.py,调试测试文件没有错误。导入(ModuleNotFoundError)有一个问题,但通过创建一个Conda环境并在那里安装依赖项修复了这个问题。


My file tree looks like this, all contained in the project folder MY-PACKAGE:

我的文件树如下所示,全部包含在项目文件夹My-Package中:


.vscode
---> settings.json
docs
my-package
---> __init__.py
---> predict.py
tests
---> __init__.py
---> test_read_data.py
requirements.txt
setup.py
README.md

Both init files in the tree are empty. Predict.py contains a function read_data() which is tested by the test_read_data.py file.

树中的两个初始化文件都是空的。Py包含一个函数Read_Data(),该函数通过test_read_data.py文件进行测试。


My settings.json file looks like this:

我的settings.json文件如下所示:


{
"files.autoSave": "onFocusChange",
"python.testing.pytestArgs": [
"tests","--no-cov","--rootdir","$MY-PACKAGE/tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.cwd": "$MY-PACKAGE/tests",
"python.testing.autoTestDiscoverOnSaveEnabled": true,
}

I've tried specifying the directory in python.testing.pytestArgs and python.testing.cwd as suggested in a similar StackOverflow question though the error didn't change. I was using Python 3.11 as the interpreter and have tried an earlier version, Python 3.7, to no avail. One issue I had earlier was the test file not running as I had from .predict import read_data which was fixed by changing to import my-package, however this installs the version on pip rather than the local one. Any suggestions? Happy to provide more info or code if needed.

我尝试按照类似的StackOverflow问题中的建议在python.testing.pyestArgs和python.testing.cwd中指定目录,但错误没有改变。我使用了Python3.11作为解释器,并尝试了更早的版本,也就是Python3.7,但没有效果。我之前遇到的一个问题是测试文件没有运行,就像我从.recast导入READ_DATA中一样,通过更改为IMPORT MY-Package修复了该问题,但是这会将版本安装在pip上,而不是安装在本地版本上。有什么建议吗?乐于提供更多的信息或代码,如果需要。


更多回答

Can this answer solve your issue?

这个答案能解决你的问题吗?

@MingJie-MSFT thank you very much, that answer worked, I needed to specify the paths at the start of each test file before importing my package.

@Mingjie-MSFT非常感谢,这个答案起作用了,在导入我的包之前,我需要在每个测试文件的开头指定路径。

优秀答案推荐

You need to specify the paths at the start of each test file before importing your package. You can do this by adding the following code at the beginning of each test file:

在导入包之前,您需要在每个测试文件的开头指定路径。您可以通过在每个测试文件的开头添加以下代码来完成此操作:


import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

This code adds the parent directory of the test file to the Python path, which allows you to import your package from the test file.

此代码将测试文件的父目录添加到Python路径,这允许您从测试文件导入包。


Make sure to replace .. with the correct relative path to the parent directory of your package.

请务必更换..具有包的父目录的正确相对路径。


更多回答

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com