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:
def my_function(dbutils):
...
dbutils.fs.ls("/tmp") # this is using the local variable received by parameter
...
from my_library import my_function
...
my_function(dbutils) # this refers to the global dbutils variable
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")