---
title: Test Databricks notebooks
description: Test code directly in Databricks notebooks using built-in libraries like unittest, widgets to switch modes, scheduled runs, and Git folders.
canonical_url: https://docs.databricks.com/aws/en/notebooks/test-notebooks
---

# Test Databricks notebooks

You can test code directly in Databricks notebooks using built-in libraries like `unittest`, widgets to switch between test and normal runs, and scheduled jobs. Use these techniques separately or together.

For a detailed walkthrough of how to set up and organize functions and unit tests in Databricks notebooks, see [Unit testing for Databricks notebooks](https://docs.databricks.com/aws/en/notebooks/testing.md).

Many unit testing libraries work directly within the notebook. For example, you can use the built-in Python [`unittest`](https://docs.python.org/3/library/unittest.html) package to test notebook code.

```python
def reverse(s):
    return s[::-1]

import unittest

class TestHelpers(unittest.TestCase):
    def test_reverse(self):
        self.assertEqual(reverse('abc'), 'cba')

r = unittest.main(argv=[''], verbosity=2, exit=False)
assert r.result.wasSuccessful(), 'Test failed; see logs above'
```

Test failures appear in the output area of the cell.

![Unit test failure](https://docs.databricks.com/aws/en/images/notebooks/test-failure-output.png)

## Use Databricks widgets to select notebook mode

You can use [widgets](https://docs.databricks.com/aws/en/notebooks/widgets.md) to distinguish test invocations from normal invocations in a single notebook. The following code produces the example shown in the screenshot:

```python
dbutils.widgets.dropdown("Mode", "Test", ["Test", "Normal"])

def reverse(s):
  return s[::-1]

if dbutils.widgets.get('Mode') == 'Test':
  assert reverse('abc') == 'cba'
  print('Tests passed')
else:
  print(reverse('desrever'))
```

The first line generates the **Mode** drop-down menu:

![Widget customize execution](https://docs.databricks.com/aws/en/images/notebooks/test-mode-widget.png)

## Hide test code and results

To hide test code and results, select **Cell layout** from the [cell actions menu](https://docs.databricks.com/aws/en/notebooks/basic-editing.md#cell-actions), then select **Hide code** and clear **Show results**. Errors are displayed even if results are hidden.

## Schedule tests to run automatically

To run tests periodically and automatically, you can use [scheduled notebooks](https://docs.databricks.com/aws/en/notebooks/schedule-notebook-jobs.md). You can configure the job to send [notification emails](https://docs.databricks.com/aws/en/jobs/notifications.md) to an email address that you specify.

![Scheduled notebook test](https://docs.databricks.com/aws/en/images/notebooks/test-failure-notification.png)

## <a id="separate-test-code-from-the-notebook" />Separate test code from the notebook

You can keep your test code separate from your notebook using either `%run` or Databricks Git folders. When you use `%run`, test code is included in a separate notebook that you call from another notebook. When you use Databricks Git folders, you can [keep test code in non-notebook source code files](https://docs.databricks.com/aws/en/notebooks/share-code.md#reference-source-code-files-using-git).

This section shows some examples of using `%run` and Databricks Git folders to separate your test code from the notebook.

## Use `%run`

The screenshot below shows how to use `%run` to run a notebook from another notebook. For more information about using `%run`, see [Use `%run` to import a notebook](https://docs.databricks.com/aws/en/notebooks/notebook-workflows.md#run). The code used to generate the examples is shown following the screenshot.

![Initial test code](https://docs.databricks.com/aws/en/images/notebooks/notebook-test-shared.png)

![Separating test code](https://docs.databricks.com/aws/en/images/notebooks/notebook-test-shared-run.png)

Here is the code used in the example. This code assumes that the notebooks **shared-code-notebook** and **shared-code-notebook-test** are in the same workspace folder.

**shared-code-notebook**:

```python
def reverse(s):
  return s[::-1]
```

**shared-code-notebook-test**:

In one cell:

```python
%run ./shared-code-notebook
```

In a subsequent cell:

```python
import unittest

class TestHelpers(unittest.TestCase):
    def test_reverse(self):
        self.assertEqual(reverse('abc'), 'cba')

r = unittest.main(argv=[''], verbosity=2, exit=False)
assert r.result.wasSuccessful(), 'Test failed; see logs above'
```

## Use Databricks Git folders

For code stored in a [Databricks Git folder](https://docs.databricks.com/aws/en/repos/index.md), you can call the test and run it directly from a notebook.

![Notebook testing invocation](https://docs.databricks.com/aws/en/images/notebooks/sh-unittest-invocation.png)

You can also use [web terminal](https://docs.databricks.com/aws/en/compute/web-terminal.md) to run tests in source code files just as you would on your local machine.

![Git folders testing invocation](https://docs.databricks.com/aws/en/images/notebooks/test-code-repos.png)

## Set up a CI/CD-style workflow

For notebooks in a [Databricks Git folder](https://docs.databricks.com/aws/en/repos/index.md), you can set up a CI/CD-style workflow by configuring notebook tests to run for each commit. See [Databricks GitHub Actions](https://docs.databricks.com/aws/en/dev-tools/ci-cd/github.md).
