4
votes

I followed this tutorial to set up unit tests in VS Code for Python:

I have a problem in the section "Test discovery". When I execute the command "Python: Discover Unit Tests" from the command palette in VS Code, absolutely nothing happens.

As shown in the tutorial, I did enable the unit test framework, and I created unit test files. Using the commands for unit testing from the command palette does not work. When I execute my unit test files manually from the command line, it works:

python -m unittest test_my_code.py

That means that my code, and the code that tests it, are fine, the problem is somewhere in the connection between the VS Code editor and unit test, test framework.

Other issues are:

  • when I open my project in VS Code, in the status bar it says "Discovering Tests", but it keeps going forever, nothing happens
  • when I right-click on a test file and I call command for running the unit tests, also nothing happens

As requested, I also attach settings.json as image.

enter image description here

Thank you

2
Can you add your settings.json settings related to testing? - Brett Cannon
@BrettCannon I edited my post and attached settings.json. Thank you. - user8038245
You can drop the leading * from your *test_*.py line. Also try out the latest release of the extension that came out today to see if that made a difference. Otherwise you can always delete all of the python.unittest settings and try setting it up again. - Brett Cannon

2 Answers

0
votes

I had the same issue as you. In my case opening the project folder in VSCode solved the problem, then magically all the tests were discoverable and could be run.
Before I came out with this idea I only had my single test.py file opened using VSCode and also all settings configured as the tutorial stated.

0
votes

I had the exact same problem.

I needed to add pytest.ini to the folder. It was successful after that.

I tried adding __init__.py but it did not make any difference.

Adding pytest.ini helped.

See Run Test | Debug Test code lens above the test!