Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

mockpyspark

A drop-in mock for Apache PySpark 4.x for unit and integration testing. Runs entirely in Python — no JVM, no Hadoop, no cluster — using PyArrow as the compute engine and DuckDB for SQL.

pip install mock-pyspark

mockpyspark (the PyPI distribution is mock-pyspark; you still import mockpyspark) installs alongside real pyspark; there is no namespace collision. Tests opt in via a one-liner so that from pyspark.sql import ... in your production code transparently resolves to the mock.


Quick start

# conftest.py
import mockpyspark
mockpyspark.activate()
# tests/test_my_pipeline.py
from pyspark.sql import SparkSession, functions as F

def test_normalize(spark=None):
    spark = SparkSession.builder.getOrCreate()
    df = spark.createDataFrame([
        {"id": 1, "name": "  alice ", "score": 10},
        {"id": 2, "name": "BOB",      "score": 20},
    ])
    out = (
        df.withColumn("name", F.initcap(F.trim(F.col("name"))))
          .filter(F.col("score") > 15)
          .select("id", "name", "score")
          .collect()
    )
    assert out == [(2, "Bob", 20)]

No changes to your Spark logic. The alias just makes pyspark.* imports resolve to mockpyspark.* for the test process.


Version targeting

mockpyspark supports both PySpark 4.0.x and 4.1.x. APIs that only exist in 4.1 (e.g. current_time, TimeType) raise AttributeError when running in 4.0 mode.

It picks a target version automatically, in order:

  1. PYSPARK_VERSION env var
  2. The pyspark pin in pyproject.toml, requirements*.txt, poetry.lock, uv.lock, Pipfile.lock, or setup.cfg (walking up from the working directory)
  3. SparkSession.builder.config("spark.mock.targetVersion", "4.0").getOrCreate() — per-session override
  4. If nothing is found, defaults to the latest supported version and emits a UserWarning. Set MOCKPYSPARK_STRICT_VERSION=1 to raise instead.

So if your project pins pyspark==4.0.1 in pyproject.toml, the mock will refuse 4.1-only APIs without any configuration on your part.


Usage modes

Mode What to do
Mock for tests (default) mockpyspark.activate() in conftest.
Pytest plugin pytest_plugins = ["mockpyspark.pytest_plugin"] in conftest.
Real PySpark for a run Pass --no-mock-pyspark to pytest, or don't call activate().

The pytest plugin also accepts --no-mock-pyspark to bypass activation for a single invocation, useful for running the same suite against real PySpark for integration coverage.

See docs/usage.md for the full pattern, including switching between mock and real PySpark in the same suite, and known limitations.


Detecting the mock at runtime

import pyspark
if getattr(pyspark, "__mock__", False):
    print("running against mockpyspark")

Contributing

Run unit tests (no Java, no Docker):

git clone https://github.com/PeterDowdy/mock-pyspark.git && cd mock-pyspark
pip install pyarrow duckdb pandas numpy pytest
PYTHONPATH=. pytest -q -m unit

Run integration tests against real PySpark (requires Java 17):

# Via Docker (easiest)
./start.sh 4.1 --integration-test

# Or locally with a PySpark venv
python -m venv /opt/spark-venv && /opt/spark-venv/bin/pip install pyspark==4.1.0
PYTHONPATH=. PYSPARK_VERSION=4.1 PYSPARK_PYTHON=/opt/spark-venv/bin/python pytest -q -m integration

See docs/contributing.md for the full setup guide.


API coverage

Supports PySpark 4.0.x and 4.1.x — the complete PySpark 4.1 pyspark.sql.functions surface, full DataFrame/Column/Window/Catalog API, CSV/JSON/Parquet IO, Delta Lake (the delta.tables API plus Delta SQL: MERGE/UPDATE/OPTIMIZE/VACUUM/RESTORE/CLONE/time travel, via the delta extra) and Unity-Catalog-style three-level namespaces (catalogs, schemas, volumes, grants, information_schema).

Release files for mock-pyspark 0.0.1.dev20260926

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mock-pyspark 0.0.1.dev20260926
File Size Uploaded
mock_pyspark-0.0.1.dev20260926.tar.gz 357.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mock-pyspark 0.0.1.dev20260926
File Interpreter ABI Platform
mock_pyspark-0.0.1.dev20260926-py3-none-any.whl Python 3 none any Details

Total release size: 752.4 kB

Release files / mock_pyspark-0.0.1.dev20260926.tar.gz

Download URL mock_pyspark-0.0.1.dev20260926.tar.gz
Size 357.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8420615ce9801f95430190783b53e8f21ea64ff1d4edcfc416760e3b92699158
BLAKE2b-256 checksum
How to use checksums
b17a3dd28c410f547bb1ec4a8e1409e8a6fdb5ed1c1d065d3688b7adc9bcc9cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release files / mock_pyspark-0.0.1.dev20260926-py3-none-any.whl

Download URL mock_pyspark-0.0.1.dev20260926-py3-none-any.whl
Size 394.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be12c29e8681469168a2197086d0e5c857b260e9dba20a3d505a4a2be6635c29
BLAKE2b-256 checksum
How to use checksums
2bc79dee0f3b05ea94c44d5108423673cfdcde93bbba520789f4ae38881c81b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page