简体   繁体   English

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

[英]How to configure vscode in order to discover python unittests

I want to create automation tests for web application with Visual Studio code, Python and unittest.我想使用 Visual Studio 代码、Python 和单元测试为 Web 应用程序创建自动化测试。 However my Test Explorer fails to discover tests (no error appears it is just empty).但是我的测试资源管理器未能发现测试(没有错误出现,它只是空的)。

Despite that i have needed extensions (Python, Test Explorer UI and Python Test Explorer), prepared settings.json and required methods' and classes' decoration, commands like run all tests or discover doesn't work out.尽管我需要扩展(Python、测试资源管理器 UI 和 Python 测试资源管理器)、准备好的 settings.json 和必需的方法和类的装饰,但运行所有测试或发现之类的命令不起作用。 However debugging whole file actually runs tests (i see output in terminal).然而调试整个文件实际上运行测试(我在终端看到输出)。

CODE:代码:

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()

SETTINGS:设置:

{
    "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"
}

List of things to consider:需要考虑的事项清单:

  1. You should name your test files starting with test_ .您应该以test_开头命名您的测试文件。
  2. Run python -m unittest discover in your project root directory.在项目根目录中运行python -m unittest discover If that does not find your tests, VSCode won't find them either.如果没有找到您的测试,VSCode 也不会找到它们。
  3. Place a test file at the root folder to see if it can be found / afterwards move it to submodules (consider __init__.py files)在根文件夹中放置一个测试文件以查看是否可以找到它/之后将其移动到子模块(考虑__init__.py文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM