Czarek97
Czarek97

Reputation: 23

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

Upvotes: 2

Views: 2364

Answers (1)

ohlr
ohlr

Reputation: 1909

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)

Upvotes: 1

Related Questions