我想使用 Visual Studio 代码、Python 和单元测试为 Web 应用程序创建自动化测试。但是我的测试资源管理器无法发现测试(没有出现错误,它只是空的)。
尽管我需要扩展(Python、测试资源管理器 UI 和 Python 测试资源管理器)、准备好 settings.json 以及所需的方法和类的装饰,但诸如运行所有测试或发现之类的命令不起作用。 但是,调试整个文件实际上会运行测试(我在终端中看到输出)。
代码:
import unittest
class testClass(unittest.TestCase):
def test_method(self):
self.assertEqual(1, 1)
def test_method2(self):
self.assertEqual(1, 2)
if __name__ == '__main__':
unittest.main()
设置:
{
"testExplorer.showOnRun": true,
"python.testing.unittestEnabled": true,
"python.testing.pyTestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.pythonPath": "C:\\Users\\Cezary\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe"
}
最佳答案
需要考虑的事项列表:
- 您的测试文件名称应以
test_
开头。 - 在项目根目录中运行
python -m unittest discovery
。如果找不到您的测试,VSCode 也不会找到它们。 - 将测试文件放在根文件夹中,看看是否可以找到它/然后将其移动到子模块(考虑
__init__.py
文件)
关于python - 如何配置 vscode 以发现 python 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653264/