<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python mocking dbutils in unittests in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24663#M17170</link>
    <description>&lt;P&gt;Hi @Jake P​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We'd love to hear from you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Jan 2023 06:13:34 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2023-01-16T06:13:34Z</dc:date>
    <item>
      <title>Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24660#M17167</link>
      <description>&lt;P&gt;I am trying to write some unittests using pytest, but I am coming accross the problem of how to mock my dbutils method when dbutils isn't being defined in my notebook.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to do this so that I can unit test individual functions that are utilizing dbutils?&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2022 19:43:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24660#M17167</guid>
      <dc:creator>confused_dev</dc:creator>
      <dc:date>2022-10-31T19:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24661#M17168</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can mock dbutils. An easy way to do it is to:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;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.&lt;/LI&gt;&lt;LI&gt;Use a mock library. &lt;A href="https://docs.python.org/3/library/unittest.mock.html" alt="https://docs.python.org/3/library/unittest.mock.html" target="_blank"&gt;&lt;B&gt;Unittest.mock&lt;/B&gt;&lt;/A&gt; is the simplest approach&lt;/LI&gt;&lt;LI&gt;Call your function passing down a mock instead of the actual dbutils&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;your library under test&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;def my_function(dbutils):
   ...
   dbutils.fs.ls("/tmp")  # this is using the local variable received by parameter
   ...&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;your notebook&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;from my_library import my_function
&amp;nbsp;
...
my_function(dbutils)  # this refers to the global dbutils variable&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;your test&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;from unittest.mock import MagicMock
&amp;nbsp;
from my_library import my_function
&amp;nbsp;
&amp;nbsp;
def test_my_function_calls_dbutils():
   mock_dbutils = MagicMock()
&amp;nbsp;
   my_function(dbutils=mock_dbutils)
&amp;nbsp;
   mock_dbutils.fs.ls.assert_called_once_with("/tmp")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2022 15:30:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24661#M17168</guid>
      <dc:creator>fermin_vicente</dc:creator>
      <dc:date>2022-11-21T15:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24662#M17169</link>
      <description>&lt;P&gt;above reponse is a pure mock,  and below is another example from dbx about a fixture with local filesystem, you can also add mock on dbutils.secrets:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/databrickslabs/dbx/blob/b2989213b5f67e2b7ccf8adeba97da70e88ffff2/dbx/templates/projects/python_basic/render/%7B%7Bcookiecutter.project_name%7D%7D/tests/unit/conftest.py#L32" alt="https://github.com/databrickslabs/dbx/blob/b2989213b5f67e2b7ccf8adeba97da70e88ffff2/dbx/templates/projects/python_basic/render/%7B%7Bcookiecutter.project_name%7D%7D/tests/unit/conftest.py#L32" target="_blank"&gt;https://github.com/databrickslabs/dbx/blob/b2989213b5f67e2b7ccf8adeba97da70e88ffff2/dbx/templates/projects/python_basic/render/%7B%7Bcookiecutter.project_name%7D%7D/tests/unit/conftest.py#L32&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2022 15:40:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24662#M17169</guid>
      <dc:creator>xiangzhu</dc:creator>
      <dc:date>2022-11-21T15:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24663#M17170</link>
      <description>&lt;P&gt;Hi @Jake P​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We'd love to hear from you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2023 06:13:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24663#M17170</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-01-16T06:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/95950#M39190</link>
      <description>&lt;P&gt;How can I add locally type annotation to the example provided &lt;A href="https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/24661/highlight/true#M17168" target="_self"&gt;above&lt;/A&gt;&amp;nbsp;by&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/43547"&gt;@fermin_vicente&lt;/a&gt;?&lt;/P&gt;&lt;P&gt;The type of `dbutils` seems to be `dbruntime.dbutils.DBUtils` but I'm not sure how to get it available in local dev.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 11:36:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/95950#M39190</guid>
      <dc:creator>drorata</dc:creator>
      <dc:date>2024-10-24T11:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96077#M39209</link>
      <description>&lt;P&gt;I personally went without type hint for the dbutils param to avoid extra dependencies just for that, but I think you can actually get that type from the Databricks SDK, according to the docs:&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/en/dev-tools/databricks-connect/python/databricks-utilities.html" target="_blank"&gt;https://docs.databricks.com/en/dev-tools/databricks-connect/python/databricks-utilities.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pypi.org/project/databricks-sdk/#description" target="_blank"&gt;https://pypi.org/project/databricks-sdk/#description&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I would worry in this case about matching the proper SDK with the DBR you're using to ensure the type validations match (although I guess dbutils doesn't evolve a lot)&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2024 05:57:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96077#M39209</guid>
      <dc:creator>fermin_vicente</dc:creator>
      <dc:date>2024-10-25T05:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96081#M39211</link>
      <description>&lt;P&gt;import unittest&lt;BR /&gt;from unittest.mock import MagicMock, patch&lt;/P&gt;&lt;P&gt;# Import the function to be tested&lt;BR /&gt;from your_notebook import my_function&lt;/P&gt;&lt;P&gt;class TestMyFunction(unittest.TestCase):&lt;BR /&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/91022"&gt;@patch&lt;/a&gt;('your_notebook.dbutils')&lt;BR /&gt;def test_my_function(self, mock_dbutils):&lt;BR /&gt;# Create a mock for dbutils.fs&lt;BR /&gt;mock_fs = MagicMock()&lt;BR /&gt;mock_dbutils.fs = mock_fs&lt;/P&gt;&lt;P&gt;# Define the behavior of the mock methods&lt;BR /&gt;mock_fs.mkdirs.return_value = None&lt;BR /&gt;mock_fs.ls.return_value = ["file1", "file2"]&lt;/P&gt;&lt;P&gt;# Call the function to be tested&lt;BR /&gt;result = my_function()&lt;/P&gt;&lt;P&gt;# Assertions&lt;/P&gt;&lt;P&gt;if __name__ == '__main__':&lt;BR /&gt;unittest.main()&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2024 06:56:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96081#M39211</guid>
      <dc:creator>saurabh18cs</dc:creator>
      <dc:date>2024-10-25T06:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96109#M39217</link>
      <description>&lt;P&gt;Fermin_vicente's answer is pretty good already. Below is how you can do something similar with conftest.py&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# conftest.py
import pytest
from unittest.mock import MagicMock
from pyspark.sql import SparkSession

@pytest.fixture(scope="session")
def dbutils():
    dbutils = MagicMock()
    yield dbutils
    dbutils.stop()

# Also for the SparkSession
@pytest.fixture(scope="session")
def spark():
    spark = (
        SparkSession.builder.master("local[*]").appName("app-name").getOrCreate()
    )
    yield spark
    spark.stop()&lt;/LI-CODE&gt;&lt;P&gt;Then for the testing code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def test_query_generation(spark, dbutils):
    """
    Test example
    """
    test_obj = QueryGenerator(test_config, spark, dbutils)

    expected_query = (
        "SELECT * from community.databricks.com"
    )
    assert (
        test_obj.get_query() == expected_query
    )&lt;/LI-CODE&gt;&lt;P&gt;Your class object has to get input a dbutils object. I think the same can be done if you want to use just functions.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2024 09:44:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/96109#M39217</guid>
      <dc:creator>pavlosskev</dc:creator>
      <dc:date>2024-10-25T09:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: Python mocking dbutils in unittests</title>
      <link>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/142485#M51955</link>
      <description>&lt;P&gt;If this helps anyone here is how we do this:&lt;/P&gt;&lt;P&gt;We rely on &lt;A href="https://github.com/algattik/databricks_test" target="_self"&gt;databricks_test&lt;/A&gt; for injecting dbutils into the notebooks that we're testing (which is a 3rd party package mind you and hasn't been updated in a while but still works). And in our notebooks we put this as the first code cell:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os

# for databricks environment only, dbutils is not immediately available.
if "DATABRICKS_RUNTIME_VERSION" in os.environ:
    print("Found databricks runtime, importing dbutils for etl.utils.load_table ...")
    from databricks.sdk.runtime import dbutils


# Instrument for unit tests. This is intended to only be executed in local unit tests, not in Databricks.
if "dbutils" not in locals() and "DATABRICKS_RUNTIME_VERSION" not in os.environ:
    print("Unable to locate dbutils")
    import databricks_test

    databricks_test.inject_variables()&lt;/LI-CODE&gt;&lt;P&gt;The first conditional is mainly for when you're running this on databricks and probably not necessary since dbutils should be available anyway, but I think it helped with our IDE's understanding what dbutils was and stopped throwing flake errors.&lt;/P&gt;&lt;P&gt;The second conditional is for injecting the databricks_test mocks into the notebook when a test is running.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 20:01:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-mocking-dbutils-in-unittests/m-p/142485#M51955</guid>
      <dc:creator>kenmyers-8451</dc:creator>
      <dc:date>2025-12-23T20:01:16Z</dc:date>
    </item>
  </channel>
</rss>

