Unit testing in Databricks notebooks

1.2k views Asked by At

The following code is intended to run unit tests in Databricks notebooks, using pytest.

import pytest
import os
import sys

repo_name = "Databricks-Code-Repo"

# 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."

This code snippet is in the guide provided by Databricks. However, it produces the following error:

PermissionError: [Errno 1] Operation not permitted: '/Workspace//Repos/<email_address>/Databricks-Code-Repo/Databricks-Code-Repo'

This notebook is inside Databricks Repos. I have two other notebooks:

  • functions (where I have defined three data transformation functions);
  • test_functions (where I have defined test function for each of the data transformation functions from the previous notebook).

I get that the error has something to do with permissions, but I can't figure out what is causing it. I will appreciate any suggestions.

1

There are 1 answers

0
Henrique Brisola On

I know it is silly, but I restarted the cluster and it worked.