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

时间:2019-06-18 16:15:50

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

我想使用Visual Studio代码,Python和unittest为Web应用程序创建自动化测试。但是,我的测试资源管理器无法发现测试(没有错误出现,只是空白)。

尽管我需要扩展(Python,Test Explorer UI和Python Test Explorer),准备了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 个答案:

答案 0 :(得分:1)

要考虑的事情清单:

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