【问题标题】:How to configure vscode in order to discover python unittests如何配置 vscode 以发现 python 单元测试
【发布时间】:2019-06-19 01:51:08
【问题描述】:

我想使用 Visual Studio 代码、Python 和 unittest 为 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"
}

【问题讨论】:

  • 您使用的是什么版本的 Visual Studio、Python 和 Unittest?另外,请明确说明您的问题,以便没有人猜测。
  • 我正在使用 Visual Studio 代码 1.35.1 和 Python 3.7.3。 Unitest 包我只是通过 import 关键字导入的,这里我没有做任何更改。
  • 你检查过“测试发现部分”吗? code.visualstudio.com/docs/python/unit-testing#_test-discovery 尝试将您的文件命名为“test_yourtest.py”
  • 我试过了,可惜没用。我还有一个 init.py 文件添加到与测试文件相同的文件夹中,但仍然没有效果。

标签: python visual-studio-code automated-tests


【解决方案1】:

要考虑的事项列表:

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

【讨论】:

  • 成功了! :D 一直以来的问题是我在编辑器中打开 .py 测试文件作为文件而不是整个文件夹。非常感谢!
猜你喜欢
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 2011-12-05
  • 2011-03-18
  • 2021-03-14
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多