【发布时间】:2020-01-23 12:41:05
【问题描述】:
我正在尝试将单元测试添加到我的 python 项目中,但我无法让 VS Code 发现我的测试。问题是当我尝试导入我正在测试的class 时。
- 如果我尝试
run测试文件,它会通过。 - 如果我省略了
from A.myfile import MyFile,就会发现测试。
问题:我在这里做错了什么?
文件结构:
root
├── A
│ ├── __init__.py
│ └── myfile.py
└── tests
├── __init__.py
└── A
├── __init__.py
└── test_myfile.py
myfile.py:
class MyFile(object):
def __init__(self):
self.value = 1
test_myfile.py:
import unittest
from A.myfile import MyFile
class Test_MyFile(unittest.TestCase):
def test_my_file(self):
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
.env:
PYTHONPATH=PATH_TO_ROOT
settings.json:
{
"python.pythonPath": "PATH_TO_PYTHON\python.exe",
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py",
"-t",
"."
],
"python.envFile": "${workspaceFolder}/.env",
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
Python 测试日志:
start
VSCode 版本:1.38.1
Python 版本:2.7.14 (x64)
【问题讨论】:
-
你的根文件夹添加到路径了吗?
cd /path/to/root && export PATH=$PATH:$(pwd) -
仅限
PYTHONPATH- 这就是我拥有.env文件的原因。我在 Windows 10 上。
标签: python unit-testing visual-studio-code windows-10 python-unittest