vendredi 24 avril 2020

VScode python unittest: no tests discovered

I can't seem to get vscode to detect tests for a simple project:

Folder structure:

~/Documents/My_proj$ tree
.
├── tests
│   ├── __init__.py
│   └── test_main.py
└── xyz
    ├── __init__.py
    ├── main.py
    ├── subpkg1
    │   ├── __init__.py
    │   └── modx.py
    └── subpkg2
        ├── __init__.py
        └── mody.py

main.py:

from xyz.subpkg1 import modx
from xyz.subpkg2 import mody

def a():
    return modx.x + mody.y

test_main.py:

from unittest import TestCase
from xyz import main

class TestA(TestCase):
    def test_a(self):
        self.assertEqual(main.a(), 30)

if __name__ == "__main__":
    unittest.main()

unittest config:

"python.unitTest.unittestArgs": [
    "-v",
    "-s",
    "./tests",
    "-p",
    "test_*.py"
]

Tests can be successfully run from the terminal with python3 -m unittest discover; tests also run fine when I click the "Run all tests" green triangle icon.

Yet when I click "Discover Tests", vscode tells me "No tests discovered, please check the configuration settings for the tests."

When I delete from xyz import main from test_main.py, vscode finally detects the test correctly, but this breaks the test.

I've gone through 10 similar StackOverflow questions (and a few github issues) without finding a solution.

What am I missing here?

1 commentaire:

  1. import your package in test functions ... like

    class TestA(TestCase):
    def test_a(self):
    from xyz import main
    self.assertEqual(main.a(), 30)

    dont know why it is the case in vs code

    RépondreSupprimer