<?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: pytest unit testing help in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149727#M53162</link>
    <description>&lt;P&gt;Hi there, I wanted to briefly follow-up on this. From what I'm seeing, the error is due to&amp;nbsp;pytest trying to write __pycache__ under /Workspace/..., and that filesystem doesn’t support that write mode.&lt;/P&gt;&lt;P&gt;In order to resolve this, I tried adding two command line arguments:&lt;/P&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;PYTHONDONTWRITEBYTECODE=1&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;--assert=plain&lt;/STRONG&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;(I also added &lt;STRONG&gt;-p&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt; no:cacheprovider&lt;/STRONG&gt;, but this is only to avoid writing out the&amp;nbsp;.pytest_cache folder in my Workspace)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;After adding these CLI args in pytest, I am finally seeing successful tests!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The same fix works in the equivalent pytest python logic, including:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;sys.dont_write_bytecode = True&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;result &lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; pytest.&lt;/SPAN&gt;&lt;SPAN&gt;main&lt;/SPAN&gt;&lt;SPAN&gt;([&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;STRONG&gt;--assert=plain&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"-p"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"no:cacheprovider"&lt;/SPAN&gt;&lt;SPAN&gt;, "-q"&lt;/SPAN&gt;&lt;SPAN&gt;])&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;See attached examples.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 03 Mar 2026 18:43:56 GMT</pubDate>
    <dc:creator>ChristianRRL</dc:creator>
    <dc:date>2026-03-03T18:43:56Z</dc:date>
    <item>
      <title>pytest unit testing help</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149322#M53074</link>
      <description>&lt;P&gt;Hi there, I am hoping someone can help me understand why I'm having issues with a simple pytest unit test... test?&lt;/P&gt;&lt;P&gt;When attempting to run a `run_tests_notebook` in our all-purpose compute cluster (Runtime 14.3) with the following:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import pytest
import os
import sys

# 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."&lt;/LI-CODE&gt;&lt;P&gt;I get the following error message:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-9.0.2, pluggy-1.6.0 -- /local_disk0/.ephemeral_nfs/envs/pythonEnv-9d50e02c-01b0-47de-95e6-456ef5c86d1c/bin/python
rootdir: /Workspace/Users/username@company.com/sandbox/unit_test_sample
plugins: anyio-3.5.0
collecting ... collected 0 items / 1 error

==================================== ERRORS ====================================
____________________ ERROR collecting test_my_functions.py _____________________
ImportError while importing test module '/Workspace/Users/username@company.com/sandbox/unit_test_sample/test_my_functions.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'test_my_functions'
=========================== short test summary info ============================
ERROR test_my_functions.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.52s ===============================
AssertionError: The pytest invocation failed. See the log for details.&lt;/LI-CODE&gt;&lt;P&gt;However, when I switch to the Serverless cluster and attempt to `Run tests` directly on my `test_my_functions.py` file (feature not available in Runtime 14.3), I am seeing that my unit tests are passing successfully:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 23:48:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149322#M53074</guid>
      <dc:creator>ChristianRRL</dc:creator>
      <dc:date>2026-02-25T23:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: pytest unit testing help</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149324#M53075</link>
      <description>&lt;P&gt;My serverless successful unit test picture didn't go through for some reason in my original post:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="serverless-successful-unit-test.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/24339iAE40FBB3DD6FA5ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="serverless-successful-unit-test.png" alt="serverless-successful-unit-test.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 23:51:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149324#M53075</guid>
      <dc:creator>ChristianRRL</dc:creator>
      <dc:date>2026-02-25T23:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: pytest unit testing help</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149492#M53109</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/96188"&gt;@ChristianRRL&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;&lt;P&gt;If you have your scripts in a regulat Workspace folder you might need to add the script you intend to import to the sys path:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
sys.path.append("/Workspace/Users/your_email/my_project")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But if you have your code in a Databricks Repo, that should not be required if you are running on Databricks Runtime 14.3 and above (&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/repos/what-happened-repos#python-syspath-behavior" target="_self"&gt;check here&lt;/A&gt;).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Feb 2026 10:21:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149492#M53109</guid>
      <dc:creator>wesleyfelipe</dc:creator>
      <dc:date>2026-02-27T10:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: pytest unit testing help</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149727#M53162</link>
      <description>&lt;P&gt;Hi there, I wanted to briefly follow-up on this. From what I'm seeing, the error is due to&amp;nbsp;pytest trying to write __pycache__ under /Workspace/..., and that filesystem doesn’t support that write mode.&lt;/P&gt;&lt;P&gt;In order to resolve this, I tried adding two command line arguments:&lt;/P&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;PYTHONDONTWRITEBYTECODE=1&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;--assert=plain&lt;/STRONG&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;(I also added &lt;STRONG&gt;-p&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt; no:cacheprovider&lt;/STRONG&gt;, but this is only to avoid writing out the&amp;nbsp;.pytest_cache folder in my Workspace)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;After adding these CLI args in pytest, I am finally seeing successful tests!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The same fix works in the equivalent pytest python logic, including:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;sys.dont_write_bytecode = True&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;result &lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; pytest.&lt;/SPAN&gt;&lt;SPAN&gt;main&lt;/SPAN&gt;&lt;SPAN&gt;([&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;STRONG&gt;--assert=plain&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"-p"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"no:cacheprovider"&lt;/SPAN&gt;&lt;SPAN&gt;, "-q"&lt;/SPAN&gt;&lt;SPAN&gt;])&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;See attached examples.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 03 Mar 2026 18:43:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/149727#M53162</guid>
      <dc:creator>ChristianRRL</dc:creator>
      <dc:date>2026-03-03T18:43:56Z</dc:date>
    </item>
    <item>
      <title>Hi @ChristianRRL, Your follow-up solution is spot on. To...</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/150336#M53372</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/96188"&gt;@ChristianRRL&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;Your follow-up solution is spot on. To summarize and add some context for others who land here:&lt;/P&gt;
&lt;P&gt;THE ROOT CAUSE&lt;/P&gt;
&lt;P&gt;The ModuleNotFoundError you initially saw happens because the Workspace filesystem (/Workspace/...) does not fully support certain write operations that Python and pytest rely on by default. Specifically:&lt;/P&gt;
&lt;P&gt;1. pytest's assertion rewriting mechanism tries to write compiled .pyc files into __pycache__ directories, which fails on the Workspace filesystem.&lt;BR /&gt;
2. The cache provider plugin tries to write a .pytest_cache folder, which also fails.&lt;/P&gt;
&lt;P&gt;When either of these writes fails, pytest cannot properly collect and import your test modules, which surfaces as the misleading "ModuleNotFoundError: No module named 'test_my_functions'" error.&lt;/P&gt;
&lt;P&gt;THE FIX&lt;/P&gt;
&lt;P&gt;As you discovered, two settings resolve this. The official Databricks documentation for running pytest in notebooks actually recommends both of these:&lt;/P&gt;
&lt;PRE&gt;import pytest
import sys

# Prevent Python from writing .pyc files to the read-only workspace filesystem
sys.dont_write_bytecode = True

# Run pytest with assertion rewriting disabled and cache provider off
retcode = pytest.main([".", "-v", "--assert=plain", "-p", "no:cacheprovider"])

assert retcode == 0, "The pytest invocation failed. See the log for details."&lt;/PRE&gt;
&lt;P&gt;Here is what each setting does:&lt;/P&gt;
&lt;PRE&gt;sys.dont_write_bytecode = True
Prevents Python from creating __pycache__/*.pyc files.

--assert=plain
Disables pytest's assertion rewriting, which also requires writing .pyc files.

-p no:cacheprovider
Disables the .pytest_cache directory that pytest uses to track test state between runs.&lt;/PRE&gt;
&lt;P&gt;WHY SERVERLESS WORKS WITHOUT THESE FLAGS&lt;/P&gt;
&lt;P&gt;On Serverless compute, the "Run tests" button uses a built-in test runner that is already configured to handle these filesystem constraints. That is why your tests passed on Serverless without any extra arguments.&lt;/P&gt;
&lt;P&gt;ADDITIONAL TIPS FOR PYTEST IN DATABRICKS&lt;/P&gt;
&lt;P&gt;1. sys.path for Workspace folders: As another commenter mentioned, if your source code is in a regular Workspace folder (not a Git folder/Repo), you may need to add its path to sys.path so pytest can find your modules:&lt;/P&gt;
&lt;PRE&gt;import sys
sys.path.append("/Workspace/Users/your_email/your_project")

For code stored in Git folders (Repos), this is handled automatically on Runtime 14.3 and above.&lt;/PRE&gt;
&lt;P&gt;2. Organize tests alongside source files: Databricks recommends storing functions and tests as Python files (.py) rather than inside notebooks, since test frameworks work best with standard Python files.&lt;/P&gt;
&lt;P&gt;3. Documentation reference: The Databricks docs have a full walkthrough of running pytest in notebooks here:&lt;BR /&gt;
&lt;A href="https://docs.databricks.com/en/notebooks/testing.html" target="_blank"&gt;https://docs.databricks.com/en/notebooks/testing.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;* This reply used an agent system I built to research and draft this response based on the wide set of documentation I have available and previous memory. I personally review the draft for any obvious issues and for monitoring system reliability and update it when I detect any drift, but there is still a small chance that something is inaccurate, especially if you are experimenting with brand new features.&lt;/P&gt;
&lt;P&gt;If this answer resolves your question, could you mark it as "Accept as Solution"? That helps other users quickly find the correct fix.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2026 05:48:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-unit-testing-help/m-p/150336#M53372</guid>
      <dc:creator>SteveOstrowski</dc:creator>
      <dc:date>2026-03-09T05:48:15Z</dc:date>
    </item>
  </channel>
</rss>

