Hi,
Apologies, as I am trying to use Pytest first time. I know this question has been raised but I went through previous answers but the issue still exists.
I am following DAtabricks and other articles using pytest. My structure is simple as
-tests
--common [Folder]
---- __init__.py
---- utils.py
--test_tran.py
--test_utils.py
--exectest.ipynb
Common[Folder],test_tran,test_utils and exectest files are at same level. test_utils refers utils.py
My test files work fine [test_tran and test_utils].
When I try to execute exectest notebook, I am thrown error
_______________________ ERROR collecting test_tran.py _________________________ ImportError while importing test module '/Workspace/Users/...../test/tests/test_tran.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /usr/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) E ModuleNotFoundError: No module named 'test_tran'
This error is thrown for both test_tran and test_utils files.
I have tried with VS Code [bundle], and it is the same result.
Code of exectest.ipynb file:
pip install pytest
import pytest
import sys
import os, sys
repo_name = "unittest"
# Get the path to this notebook, for example "/Workspace/Repos/{username}/{repo-name}".
notebook_path = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()
# Get the repo's root directory name.
repo_root = os.path.dirname(os.path.dirname(notebook_path))
# Prepare to run pytest from the repo.
os.chdir(f"/Workspace/{repo_root}/{repo_name}")
print(os.getcwd())
# Skip writing pyc files on a readonly filesystem.
sys.dont_write_bytecode = True
# Run pytest.
retcode = pytest.main([".", "-v", "-p", "no:cacheprovider"])
# Fail the cell execution if there are any test failures.
assert retcode == 0, "The pytest invocation failed. See the log for details."