pyspark-test is a Python library designed to simplify unit testing for PySpark DataFrames. It provides a function, `assert_pyspark_df_equal`, inspired by the pandas testing module, which allows users to compare two Spark DataFrames and identify any differences. The library is currently at version 0.2.0 and has a stable, albeit infrequent, release cadence, focusing on its core DataFrame comparison functionality.
pip install pyspark-testVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `assert_pyspark_df_equal` to compare two PySpark DataFrames. It includes the necessary setup for a local SparkSession and shows both successful and intentionally failing assertions to illustrate its usage and error reporting. The `check_dtype`, `check_column_names`, `check_columns_in_order`, and `order_by` parameters are used for a strict comparison.
Set `check_column_names=True`, `check_columns_in_order=True`, and `check_dtype=True` for strict comparisons. Use `order_by` if row order is not guaranteed but data content should be the same, allowing internal sorting before comparison.
For `pytest`, use `session`-scoped fixtures to create a single SparkSession for all tests. For `unittest`, use `setUpClass` and `tearDownClass`. Consider suppressing `py4j` logging to `WARN` or `ERROR` levels in your test configuration.
Examine the detailed error output provided by `assert_pyspark_df_equal`, which highlights differing rows and columns. Verify your transformation logic or expected input/output data. Ensure `order_by` is set if row order is non-deterministic.
Review the schema definition of both DataFrames. Ensure that all column names, their exact data types, and nullability properties are identical. Set `check_dtype=False` if you only care about data values and not type strictness.
Ensure that both DataFrames have the exact same column names in the exact same order. If column order is not important, set `check_columns_in_order=False`.