Great Expectations Data Quality Testing Guide: A Production Tutorial

In modern data platforms and machine learning pipelines, silent data corruption is one of the most insidious risks. Whether an upstream API unexpectedly starts returning null values, schema datatypes shift without warning, or negative values appear in a strictly positive transaction column, bad data wreaks havoc on downstream models and analytics dashboards.

Enter Great Expectations. As the leading open-source data quality validation framework, Great Expectations allows data engineers and scientists to treat data testing like software testing—defining declarative expectations that automatically validate batches of data in your pipelines.

How Great Expectations Architecture Works

Great Expectations structures data quality testing around declarative assertions, data connectors, and automated validation suites:

  • Expectations: Declarative assertions about your data (e.g., column values must not be null, values must fall between 1 and 100, or column types must be integers).
  • Data Context: The central configuration hub that manages your data sources, checkpoint configurations, and expectation suites across your project repository.
  • Validation Checkpoints: Automated execution bundles that run your expectation suites against incoming batches of data (from Pandas, Spark, SQL databases, or S3/Parquet files) and generate rich Data Docs HTML reports.

Ad Hoc Assertions vs. Great Expectations

Evaluation MetricCustom Pandas AssertionsGreat Expectations Framework
Test CoverageAd hoc Python scripts written manually per pipelineComprehensive suite of 50+ out-of-the-box data assertions
DocumentationNone (requires manual Markdown summaries)Automatic generation of gorgeous HTML Data Docs reports
Pipeline IntegrationCustom exception throwing prone to inconsistent loggingNative integration with Airflow, Prefect, dbt, and Spark

Python Implementation Blueprint

Using the great_expectations library in Python, you can wrap a Pandas dataframe into a validator and test declarative expectations cleanly:

Python
Defining and validating expectations using Great Expectations in Python
import great_expectations as gx
import pandas as pd

# Sample dataframe
df = pd.DataFrame({
    "id": [1, 2, 3, 4, 5],
    "age": [25, 30, 22, 45, 29],
    "email": ["a@test.com", "b@test.com", "c@test.com", "d@test.com", "e@test.com"]
})

# 1. Convert Pandas DataFrame to Great Expectations Dataset
gx_df = gx.from_pandas(df)

# 2. Define declarative expectations
gx_df.expect_column_values_to_not_be_null("id")
gx_df.expect_column_values_to_be_between("age", min_value=18, max_value=100)
gx_df.expect_column_values_to_match_regex("email", r"^[^@]+@[^@]+\.[^@]+$")

# 3. Validate batch expectations
validation_result = gx_df.validate()
print("Data Validation Success Status:", validation_result["success"])

Production Best Practices

Deploying data quality checks with Great Expectations in production enterprise pipelines requires adherence to operational standards:

  • Integrate Checkpoints in Airflow/Prefect: Always run your validation checkpoints as a dedicated step in your orchestration DAG immediately after data ingestion and before model training.
  • Fail Fast on Critical Expectations: Configure your validation suites to raise critical exceptions (`raise_exception=True`) when core primary keys or target columns fail validation, halting broken pipelines immediately.
  • Publish Data Docs to S3/GCS: Automatically sync your generated HTML Data Docs reports to shared S3 or GCS buckets so stakeholders and analytics engineers can inspect data quality failure details instantly.