4

There are lots of similar questions (e.g. 1, 2, 3, 4, 5, 6, …) but none have quite this combination of issues (i.e. the unittest discovery working at the shell prompt), and the solutions to those questions have not worked for me.

When I run Python: Discover Tests in Visual Studio Code I get the error

No tests discovered, please check the configuration settings for the tests.

My settings.json file looks like this

{
    "python.testing.unittestArgs": [
        "--verbose",
        "--start-directory",
        "./preprocessing/",
        "--pattern",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}

But I know the unittests are discoverable, because if I run this command from the root of my project all the tests are discovered and run:

python -m unittest discover --verbose --start-directory "./preprocessing/" --pattern "test_*.py"

My tests clearly are discoverable by Python's unittest framework, but VS Code does not find them. How might I fix this?

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • 1
    at first, comment all import statements and discover tests. If this works, check your import statements. – alexzshl May 20 '20 at 10:33

3 Answers3

1

You'll need to set the PYTHONPATH in the .env file in the root directory of your project. Make sure that all directories containing src files and tests are on the pythonpath. For details see here.

MathKid
  • 1,903
  • 1
  • 21
  • 21
0

I would try running without --verbose. It's not necessary since the extension is doing the discovering for you, plus I bet the extra output from it is breaking something.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • Thanks for the suggestion, it didn't work but was worth a try. It looks like @alexzshl is correct in his suggestion that it is my module imports – dumbledad May 26 '20 at 07:08
0

I struggled with this as well for a while. My solution was to rename the class to Test_MyTest(unittest.TestCase) instead of just TestMyTest(unittest.TestCase. This matches the "test_*.py" file name that I configured in VSCode but I don't know if the file name and class name need to match.

A.Sully
  • 21
  • 4