VSCode Python extension 在 运行 unittest 测试用例时如何识别子模块?
How to get VSCode Python extension recognize submodules when running unittest test cases?
问题
我无法让 VSCode/unittest 识别我为其编写单元测试的子模块(即背景示例中的 common.nested_module
)。
VSCode 发现测试用例并执行 test_my_module.py
中的测试用例,但抱怨 test_nested_module.py
中的 import
语句行上没有模块 common.nested_module
(在输出中:ModuleNotFoundError: No module named 'common.nested_module'
)。
我将 print(sys.path)
添加到已执行的测试用例中,在 Python 测试日志中我什至在添加 .env
文件时得到以下输出,如下所述 What我试过 2.:
test_get_version (test_my_module.TestCase1) ... ['/workspaces/test_project/project/tests', '/workspaces/test_project/project/src', '/workspaces/test_project/project/common', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/home/vscode/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages']
背景
按照PyPA tutorial for packaging and distribution我创建了一个项目结构,省略了其他细节(例如,README.md
),如下:
project
|--src
|--common
|-- __init__.py
|-- nested_module.py
|-- __init__.py
|-- my_module.py
|--tests
|--__init__.py
|--test_my_module.py
|--test_nested_module.py
settings.json
中与测试相关的设置:
{
"python.testing.unittestArgs": [
"-v",
"-s",
"${workspaceFolder}/project/tests",
"-p",
"test_*.py"
],
"python.testing.cwd": "project/src",
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
}
我试过的
- 将以下内容添加到
project/tests
中我的 __init__.py
以及 here 中的 test_nested_module.py
。
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"project/src/common"
)
sys.path.append(SOURCE_PATH)
- 将
.env
添加到 project
包含以下内容,同时将 "python.envFile": "${workspaceFolder}/afinad/.env"
添加到 settings.json
类似于建议 here.
PYTHONPATH = ./common
- 将带有
${workspaceFolder}/project/src
和 .
的 -t
选项添加到 settings.json
for python.testing.unittestArgs
作为建议 here:
"python.testing.unittestArgs": [
"-v",
"-s",
"${workspaceFolder}/project/tests",
"-p",
"test_*.py",
"-t",
"${workspaceFolder}/project/src"
],
- 运行
python3 -m unittest discover
在终端中更改为 ./tests
后给我:
ModuleNotFoundError: No module named 'my_module'
感谢您花时间阅读。非常感谢任何帮助!
在尝试了将近两天的各种建议解决方案的不同组合后,我发现了我的错误。看来,像我这样的新手,我觉得 name shadowing trap.
最初,我打算在 tests
中使用与 src
中相同的文件夹结构。但是,当我认为 test_my_module.py
有效时,我将 test_nested_module.py
从 tests/common
移动到 tests
但我没有删除文件夹 tests/common
。这似乎导致了 ModuleNotFoundError
,因为删除文件夹后,它会按预期工作。
我希望这会节省其他人一些时间。
问题
我无法让 VSCode/unittest 识别我为其编写单元测试的子模块(即背景示例中的 common.nested_module
)。
VSCode 发现测试用例并执行 test_my_module.py
中的测试用例,但抱怨 test_nested_module.py
中的 import
语句行上没有模块 common.nested_module
(在输出中:ModuleNotFoundError: No module named 'common.nested_module'
)。
我将 print(sys.path)
添加到已执行的测试用例中,在 Python 测试日志中我什至在添加 .env
文件时得到以下输出,如下所述 What我试过 2.:
test_get_version (test_my_module.TestCase1) ... ['/workspaces/test_project/project/tests', '/workspaces/test_project/project/src', '/workspaces/test_project/project/common', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/home/vscode/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages']
背景
按照PyPA tutorial for packaging and distribution我创建了一个项目结构,省略了其他细节(例如,README.md
),如下:
project
|--src
|--common
|-- __init__.py
|-- nested_module.py
|-- __init__.py
|-- my_module.py
|--tests
|--__init__.py
|--test_my_module.py
|--test_nested_module.py
settings.json
中与测试相关的设置:
{
"python.testing.unittestArgs": [
"-v",
"-s",
"${workspaceFolder}/project/tests",
"-p",
"test_*.py"
],
"python.testing.cwd": "project/src",
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
}
我试过的
- 将以下内容添加到
project/tests
中我的__init__.py
以及 here 中的test_nested_module.py
。
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"project/src/common"
)
sys.path.append(SOURCE_PATH)
- 将
.env
添加到project
包含以下内容,同时将"python.envFile": "${workspaceFolder}/afinad/.env"
添加到settings.json
类似于建议 here.
PYTHONPATH = ./common
- 将带有
${workspaceFolder}/project/src
和.
的-t
选项添加到settings.json
forpython.testing.unittestArgs
作为建议 here:
"python.testing.unittestArgs": [
"-v",
"-s",
"${workspaceFolder}/project/tests",
"-p",
"test_*.py",
"-t",
"${workspaceFolder}/project/src"
],
- 运行
python3 -m unittest discover
在终端中更改为./tests
后给我:
ModuleNotFoundError: No module named 'my_module'
感谢您花时间阅读。非常感谢任何帮助!
在尝试了将近两天的各种建议解决方案的不同组合后,我发现了我的错误。看来,像我这样的新手,我觉得 name shadowing trap.
最初,我打算在 tests
中使用与 src
中相同的文件夹结构。但是,当我认为 test_my_module.py
有效时,我将 test_nested_module.py
从 tests/common
移动到 tests
但我没有删除文件夹 tests/common
。这似乎导致了 ModuleNotFoundError
,因为删除文件夹后,它会按预期工作。
我希望这会节省其他人一些时间。