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.