python - 如何配置 vscode 以发现 python 单元测试

标签 python visual-studio-code automated-tests

我想使用 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"
}

最佳答案

需要考虑的事项列表:

  1. 您的测试文件名称应以 test_ 开头。
  2. 在项目根目录中运行python -m unittest discovery。如果找不到您的测试,VSCode 也不会找到它们。
  3. 将测试文件放在根文件夹中,看看是否可以找到它/然后将其移动到子模块(考虑 __init__.py 文件)

关于python - 如何配置 vscode 以发现 python 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653264/

相关文章:

python - Sqlalchemy : InvalidRequestError: Object '<User at 0x3a970d0>' is already attached to session '2' (this is '3' )

reactjs - Visual Studio Code 中的 JSX 或 HTML 自动完成

c++ - 为 C++ 程序配置 VS 代码时遇到问题

comments - 在 VS Code 中包裹多行注释

java - 是否可以使用 Espresso 的 IdlingResource 等待某个 View 出现?

unit-testing - 应该测试内部实现,还是只测试公共(public)行为?

Python ftplib STORE 可靠吗?

python - 尝试对 8x8 矩阵进行形态变换时出错

python - 在 python 3 中以二进制模式打开带有通用换行符的文件

c# - 启用 "Specify a batch size"时,Azure DevOps 版本中缺少控制台输出