Skip to main content

unittest-pyspark

Extensions for testing pyspark with unittest and doctest.

These utils can be used in standalone Python or in Databricks notebooks.

Usage With Doctest

from unittest_pyspark import get_spark
spark = get_spark()

def go_spark():
    """
    >>> spark.sql("SELECT 'hello world'").show()
    +-----------+
    |hello world|
    +-----------+
    |hello world|
    +-----------+
    <BLANKLINE>
    >>> spark.createDataFrame([{'hello':'world'}], 'hello:string').show()
    +-----+
    |hello|
    +-----+
    |world|
    +-----+
    <BLANKLINE>
    """
    pass

import doctest
doctest.testmod()

Usage With Unittest

Here is a simple unittest test case, which can be used as template for pySpark test case.

import unittest
from unittest_pyspark import as_list, get_spark
import pyspark.sql.types as pst

class Test_Spark(unittest.TestCase):
  def setUp(self):
      self.spark = get_spark()

  def test_i_can_fly(self):
    input = [ pst.Row(a=1, b=2)]
    input_df = self.spark.createDataFrame(input)

    expect = [{'a':1}]

    actual_df = input_df.select("a")
    actual = as_list(actual_df)

    self.assertEqual(actual, expect)

You can find this entire example in the tests.test_sample module. To execute it from the command line:

python -m unittest tests.test_sample

Usage With Unittest and Databricks

To execute the unittest test cases in Databricks, add following cell:

from unittest_pyspark.unittest import *
if __name__ == "__main__":
  execute_test_cases(discover_test_cases())

Above code will automatically discover all test cases (unittest.TestCase sub classes) defined in the global scope and execute them.

Build package

You will need setuptools and twine:

pip install --upgrade setuptools
pip install --upgrade wheel

Build and upload:

python setup.py sdist bdist_wheel
python -m twine upload dist/*

Release files for unittest-pyspark 0.0.5

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

Source distribution (sdist)

Source distribution for unittest-pyspark 0.0.5
File Size Uploaded
unittest-pyspark-0.0.5.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest-pyspark 0.0.5
File Interpreter ABI Platform
unittest_pyspark-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / unittest-pyspark-0.0.5.tar.gz

Download URL unittest-pyspark-0.0.5.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ca2ba2e859b8c564fa61c01ac9c6b163033fb902bfa15b4c25f607606ac9a8e2
BLAKE2b-256 checksum
How to use checksums
264f68bf4887b44f8ccb994d46cc732ead42555d3149b4869e88160e985cd945
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6rc1

Release files / unittest_pyspark-0.0.5-py3-none-any.whl

Download URL unittest_pyspark-0.0.5-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69a44fcf2e8359336bc67d379222012c4d1e5563db3b5c7e52e94370d5b2f8f3
BLAKE2b-256 checksum
How to use checksums
c435a91caa882cb811e75d6942117de81e9160899757734105ffb82f1f659270
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6rc1

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.2

2 release files

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