<?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: unit testing in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79140#M7787</link>
    <description>&lt;P&gt;You could use&amp;nbsp;Fixtures for SparkSession something like below&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pytest
from pyspark.sql import SparkSession

@pytest.fixture(scope="session")  # Consider "function" for reuse within a test function
def spark_session():
    spark = SparkSession.builder.appName("test").getOrCreate()
    yield spark
    spark.stop()&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 17 Jul 2024 16:27:03 GMT</pubDate>
    <dc:creator>imsabarinath</dc:creator>
    <dc:date>2024-07-17T16:27:03Z</dc:date>
    <item>
      <title>unit testing</title>
      <link>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79125#M7786</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Currently I am creating unit tests for our ETL scripts although the test is not able to recognize sc (SparkContext).&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Is there a way to mock SparkContext for a unit test?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Code being tested: df = spark.read.json(sc.parallelize([data]))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Error message received when running PyTest:&amp;nbsp;&lt;SPAN&gt;NameError: name 'sc' is not defined&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 17 Jul 2024 13:25:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79125#M7786</guid>
      <dc:creator>elsirya</dc:creator>
      <dc:date>2024-07-17T13:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: unit testing</title>
      <link>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79140#M7787</link>
      <description>&lt;P&gt;You could use&amp;nbsp;Fixtures for SparkSession something like below&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pytest
from pyspark.sql import SparkSession

@pytest.fixture(scope="session")  # Consider "function" for reuse within a test function
def spark_session():
    spark = SparkSession.builder.appName("test").getOrCreate()
    yield spark
    spark.stop()&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 17 Jul 2024 16:27:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79140#M7787</guid>
      <dc:creator>imsabarinath</dc:creator>
      <dc:date>2024-07-17T16:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: unit testing</title>
      <link>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79246#M7788</link>
      <description>&lt;P&gt;Was able to get this to work.&lt;/P&gt;&lt;P&gt;What I had to do was instantiate the "sc" variable in the PySpark notebook.&lt;/P&gt;&lt;P&gt;PySpark code:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;"sc = spark.SparkContext"&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Then in the PyTest script we add a "@patch()" statement with the "sc" variable and create a "mock_sc" variable in the function header.&lt;/P&gt;&lt;P&gt;PyTest code:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;"@patch(&amp;lt;path_to_file&amp;gt;.sc)"&lt;/LI&gt;&lt;LI&gt;"def test_function(mock_sc):"&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This solution properly mocked SparkContext within the PyTest script.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2024 13:06:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/unit-testing/m-p/79246#M7788</guid>
      <dc:creator>elsirya</dc:creator>
      <dc:date>2024-07-18T13:06:10Z</dc:date>
    </item>
  </channel>
</rss>

