cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
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.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now