Lesson 4 of 9 Data Engineering Practices

Testing Pipeline Code and the Data It Produces

This lesson explains how to test both the code that transforms data and the data that results, since a pipeline can run without errors and still produce wrong data.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish unit tests, local job runs and integration tests of pipeline code.
  • Test the data with schema, row count and quality checks.
  • Prepare safe, representative test data.

Testing the code

Pipeline code is tested at three levels, each catching different problems. Unit tests check individual transformation functions with pytest and small sample DataFrames, called fixtures. They run in seconds and catch logic errors early. Local job runs execute a complete Glue or Spark script with the Glue Docker image or a local Spark session, catching errors in how the pieces fit together without cloud costs. Integration tests run the pipeline against real services with test data in a test account, catching permission, configuration and service-behaviour problems before production. The following table summarises the levels.

LevelWhat it testsHow
Unit testsIndividual transformation functionspytest with small sample DataFrames (fixtures)
Local job runsA complete Glue or Spark scriptThe Glue Docker image or a local Spark session
Integration testsThe pipeline against real servicesA run on test data in a test account
Sort into groups

Which test level?

Exam skills 1.4.4

Decide which test level catches each problem.

Log in to try this activity Interactive activities are free with an account.

Testing the data

Code tests show that transformations behave as intended on sample inputs; data tests show that the actual outputs are correct and complete. Schema checks confirm that the output has the expected columns and types. Row counts are compared with the source, or with the output of the previous pipeline version, to detect lost or duplicated records.

Business rules check the meaning of the data, for example that amounts are never negative and keys are unique; AWS Glue Data Quality and the open-source Deequ library express such rules declaratively. Before a change to a pipeline is promoted, key metrics, such as total revenue per day, are compared between the old and new outputs, so that an unintended change in results is caught before consumers see it.

The following diagram shows the tests at each stage and a data check catching a lost half of the rows.

The tests at each stage of the delivery pipeline. Unit tests run on every commit, linting and local Glue runs on pull requests, a run on sample real data after deployment to test, and data checks that compare row counts and key metrics with the old output before promotion. A change that passes unit tests but drops half the rows is caught at the data checks.
The metric values are illustrative.
Match the pairs

Data tests

Exam skills 3.4.1 · 1.4.4

Match each risk with the data test that catches it.

Log in to try this activity Interactive activities are free with an account.

Test data

Tests need representative data that is safe to use. Small, hand-made fixtures cover edge cases such as nulls, duplicates and malformed values. Larger samples of production data must have personal data masked or be generated synthetically, so that test environments do not hold sensitive data.

Worked example

Testing a transformation before it reaches production

A data transformation should be tested at each stage of the delivery pipeline. The following table identifies the tests performed at each stage.

StageTest
Every commitUnit tests of transformation functions on small sample DataFrames (pytest)
Pull requestLint SQL and Python; run Glue scripts locally in the Glue Docker image
Deploy to testRun the pipeline on a sample of real data
Before promotionData quality checks: row counts, nulls, uniqueness compared with expectations
ProductionDeploy through the pipeline with approval

Testing data pipelines means testing the data as well as the code: a code change that passes unit tests can still drop half the rows. Comparing row counts and key metrics between the old and new outputs catches that before analysts do.

Check yourself

How can Glue scripts be tested before deployment?

Show answer

By running them locally in the Glue Docker image, using small fixture datasets.

Exam Tips

Remember for the exam

  • Test transformations with small fixture datasets.
  • Run Glue scripts locally in the Glue Docker image before deployment.
  • Test the data as well as the code: row counts, schemas and business rules.
  • Compare outputs of the old and new pipeline before promoting a change.
  • Mask or synthesise personal data used in test environments.