How to Unit Test PySpark Jobs With Local Spark in CI
A local[*] SparkSession runs Spark in-process, so you can test DataFrame logic without any cluster.
Create a session-scoped SparkSession fixture, build small DataFrames, run your transformation, and assert on collected rows.
Steps
- Install
pysparkand a JDK in the job. - Provide a
local[*]SparkSession pytest fixture. - Assert on the output of your transformation.
Test
tests/test_transform.py
import pytest
from pyspark.sql import SparkSession
from my_jobs.transform import add_total
@pytest.fixture(scope="session")
def spark():
return SparkSession.builder.master("local[*]").appName("ci").getOrCreate()
def test_add_total(spark):
df = spark.createDataFrame([(1, 2), (3, 4)], ["a", "b"])
out = add_total(df).collect()
assert out[0]["total"] == 3
assert out[1]["total"] == 7Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: pip install pyspark==3.5.1 pytest
- run: pytest tests -qGotchas
- PySpark needs a JDK; install Java before running tests.
- Reuse one session-scoped SparkSession so JVM startup is paid once.
Related guides
How to Test SQL Transformations With DuckDB in CIUnit-test SQL transformation logic in CI with an in-process DuckDB database, loading fixtures and asserting o…
How to Lint and Type-Check Python Data Code With ruff and mypy in CIRun ruff and mypy in CI over pipeline Python so lint errors and type mistakes in transformation and DAG code…