0

I'm trying to set up Pytest in VSCode to test a package I'm developing, and keep running into the error "pytest test discovery error". Running pytest --collect-only in the terminal successfully collects the file test_read_data.py, and debugging the test file shows no errors. There was an issue with imports (ModuleNotFoundError) but it was fixed by creating a conda env and installing the dependencies there.

My file tree looks like this, all contained in the project folder MY-PACKAGE:

.vscode
---> settings.json
docs
my-package
---> __init__.py
---> predict.py
tests
---> __init__.py
---> test_read_data.py
requirements.txt
setup.py
README.md

Both init files in the tree are empty. Predict.py contains a function read_data() which is tested by the test_read_data.py file.

My settings.json file looks like this:

{
    "files.autoSave": "onFocusChange",
    "python.testing.pytestArgs": [
        "tests","--no-cov","--rootdir","$MY-PACKAGE/tests"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.cwd": "$MY-PACKAGE/tests",
    "python.testing.autoTestDiscoverOnSaveEnabled": true,
}

I've tried specifying the directory in python.testing.pytestArgs and python.testing.cwd as suggested in a similar StackOverflow question though the error didn't change. I was using Python 3.11 as the interpreter and have tried an earlier version, Python 3.7, to no avail. One issue I had earlier was the test file not running as I had from .predict import read_data which was fixed by changing to import my-package, however this installs the version on pip rather than the local one. Any suggestions? Happy to provide more info or code if needed.

  • Can [this answer](https://stackoverflow.com/a/75565473/18359438) solve your issue? – MingJie-MSFT Aug 29 '23 at 02:41
  • @MingJie-MSFT thank you very much, that answer worked, I needed to specify the paths at the start of each test file before importing my package. – Sophie Frankel Aug 31 '23 at 19:20
  • Hi Sophie Frankel, glad to know you've found the solution to resolve this issue! Please consider answering it and accepting it as an answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – MingJie-MSFT Sep 01 '23 at 01:18

0 Answers0