<?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 file testing using pytest in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/45399#M1155</link>
    <description>&lt;P&gt;Thanks Kaniz or your answer. However I think i am struggling to understand some basics.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently i just wrote a simple test function.&lt;/P&gt;&lt;P&gt;def test_something():&lt;/P&gt;&lt;P&gt;assert 1==1&lt;/P&gt;&lt;P&gt;This test passes , but when i make any small change in this function ,it doesnt reflect .&lt;/P&gt;&lt;P&gt;Example i change assert to 1==0, but my output is still same.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to refresh main notebook which i am using to run tests files to pick latest change from tests files please? One solution is to restart cluster every time , but that's very bad solution&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Sep 2023 06:25:12 GMT</pubDate>
    <dc:creator>sudhanshu1</dc:creator>
    <dc:date>2023-09-20T06:25:12Z</dc:date>
    <item>
      <title>Python file testing using pytest</title>
      <link>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/45345#M1147</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a requirement in my project, where we will be writing some python code inside databricks . Please note we will not be using pyspark . It will plain pythin with polars.&lt;/P&gt;&lt;P&gt;I am looking into ho to create test files for main file. Below is simple example which i am trying&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inside repo - i created a file called function_file.py. I wrote a simple function&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;def&lt;/SPAN&gt;&lt;SPAN&gt; name_function(name):&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;return&lt;/SPAN&gt;&lt;SPAN&gt; name + &lt;/SPAN&gt;&lt;SPAN&gt;'test'&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Now i created another file called test_file.py and wrote below code&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; pytest&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from&lt;/SPAN&gt;&lt;SPAN&gt; function_file &lt;/SPAN&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; *&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;def&lt;/SPAN&gt;&lt;SPAN&gt; test_name():&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;assert&lt;/SPAN&gt; &lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;==&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Now i create a notebook and try to run below code&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; pytest&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; os&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; sys&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;repo_name = &lt;/SPAN&gt;&lt;SPAN&gt;"databricks_test_practice"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# Get the path to this notebook, for example "/Workspace/Repos/{username}/{repo-name}".&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;notebook_path = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# Get the repo's root directory name.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;repo_root = os.path.dirname(os.path.dirname(notebook_path))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(repo_root)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# # Prepare to run pytest from the repo.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;os.chdir(&lt;/SPAN&gt;&lt;SPAN&gt;f"/Workspace&lt;/SPAN&gt;&lt;SPAN&gt;{repo_root}&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(os.getcwd())&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# # Skip writing pyc files on a readonly filesystem.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;sys.dont_write_bytecode = &lt;/SPAN&gt;&lt;SPAN&gt;True&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# # Run pytest.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;retcode = pytest.main([&lt;/SPAN&gt;&lt;SPAN&gt;"."&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"-v"&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;])&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# # Fail the cell execution if there are any test failures.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;assert&lt;/SPAN&gt;&lt;SPAN&gt; retcode == &lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"The pytest invocation failed. See the log for details."&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;----------------------------&lt;/DIV&gt;&lt;DIV&gt;But i am getting&amp;nbsp; below error .&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;file /Workspace/Repos/sudhanshu.raj@zuhlke.com/databricks_test_practice/tests/test_file.py, line 4 def test_name(): E fixture 'name' not found &amp;gt; available fixtures: capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory &amp;gt; use 'pytest --fixtures [testpath]' for help on them.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 19 Sep 2023 16:11:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/45345#M1147</guid>
      <dc:creator>sudhanshu1</dc:creator>
      <dc:date>2023-09-19T16:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python file testing using pytest</title>
      <link>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/45399#M1155</link>
      <description>&lt;P&gt;Thanks Kaniz or your answer. However I think i am struggling to understand some basics.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently i just wrote a simple test function.&lt;/P&gt;&lt;P&gt;def test_something():&lt;/P&gt;&lt;P&gt;assert 1==1&lt;/P&gt;&lt;P&gt;This test passes , but when i make any small change in this function ,it doesnt reflect .&lt;/P&gt;&lt;P&gt;Example i change assert to 1==0, but my output is still same.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to refresh main notebook which i am using to run tests files to pick latest change from tests files please? One solution is to restart cluster every time , but that's very bad solution&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 06:25:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/45399#M1155</guid>
      <dc:creator>sudhanshu1</dc:creator>
      <dc:date>2023-09-20T06:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Python file testing using pytest</title>
      <link>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/55334#M2000</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/82578"&gt;@sudhanshu1&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Try running dbutils.library.restartPython() in your notebook after you make any changes to the &lt;SPAN&gt;function_file.py file&lt;/SPAN&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 16:24:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/python-file-testing-using-pytest/m-p/55334#M2000</guid>
      <dc:creator>tomcorbin</dc:creator>
      <dc:date>2023-12-15T16:24:16Z</dc:date>
    </item>
  </channel>
</rss>

