Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2022 07:30 AM
Hi,
You can mock dbutils. An easy way to do it is to:
- Try to receive dbutils as a parameter in your functions (inject it) instead of using it globally. This way your code is more testable and you won't to do patching which is a bit more cumbersome.
- Use a mock library. Unittest.mock is the simplest approach
- Call your function passing down a mock instead of the actual dbutils
Example:
- your library under test
def my_function(dbutils):
...
dbutils.fs.ls("/tmp") # this is using the local variable received by parameter
...- your notebook
from my_library import my_function
...
my_function(dbutils) # this refers to the global dbutils variable- your test
from unittest.mock import MagicMock
from my_library import my_function
def test_my_function_calls_dbutils():
mock_dbutils = MagicMock()
my_function(dbutils=mock_dbutils)
mock_dbutils.fs.ls.assert_called_once_with("/tmp")