简体   繁体   中英

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. 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. 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_ .
  2. Run python -m unittest discover in your project root directory. If that does not find your tests, VSCode won't find them either.
  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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