cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

unit testing

elsirya
New Contributor III
Currently I am creating unit tests for our ETL scripts although the test is not able to recognize sc (SparkContext).
Is there a way to mock SparkContext for a unit test?
 
Code being tested: df = spark.read.json(sc.parallelize([data]))
 
Error message received when running PyTest: NameError: name 'sc' is not defined 
1 ACCEPTED SOLUTION

Accepted Solutions

elsirya
New Contributor III

Was able to get this to work.

What I had to do was instantiate the "sc" variable in the PySpark notebook.

PySpark code:

  1. "sc = spark.SparkContext"

Then in the PyTest script we add a "@patch()" statement with the "sc" variable and create a "mock_sc" variable in the function header.

PyTest code:

  1. "@patch(<path_to_file>.sc)"
  2. "def test_function(mock_sc):"

This solution properly mocked SparkContext within the PyTest script.

View solution in original post

2 REPLIES 2

imsabarinath
New Contributor III

You could use Fixtures for SparkSession something like below 

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()

elsirya
New Contributor III

Was able to get this to work.

What I had to do was instantiate the "sc" variable in the PySpark notebook.

PySpark code:

  1. "sc = spark.SparkContext"

Then in the PyTest script we add a "@patch()" statement with the "sc" variable and create a "mock_sc" variable in the function header.

PyTest code:

  1. "@patch(<path_to_file>.sc)"
  2. "def test_function(mock_sc):"

This solution properly mocked SparkContext within the PyTest script.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group