Test Python
In this guide, you'll learn how to add data quality and validation tests to your Python Components, create dedicated Python Test Components, and implement specialized tests for custom validation logic.
Prerequisites
- Ascend Flow
For a comprehensive overview of test types and when to use them, see our Tests concept guide.
Native tests
Add native tests to Python Components by including them in the @transform
decorator.
from ascend.resources import transform, test
@transform(
# ... other parameters ...
tests=[
test("test_name", column="column_name", parameter="value"),
test("test_name", parameter="value"), # Component-level test
],
)
def my_transform(spark, ctx):
# ... transformation logic ...
Column-level tests
Validate individual columns by specifying the column
parameter. This example shows how to test that a specific column doesn't contain null values:
import ascend_project_code.transform as T
import ibis
from ascend.application.context import ComponentExecutionContext
from ascend.resources import ref, test, transform
@transform(
inputs=[ref("read_sales_stores", flow="extract-load-databricks")],
materialized="table",
tests=[test("not_null", column="timestamp")],
)
def sales_stores(read_sales_stores: ibis.Table, context: ComponentExecutionContext) -> ibis.Table:
sales_stores = T.clean(read_sales_stores)
return sales_stores
Component-level tests
Validate the entire dataset by omitting the column
parameter. This example demonstrates testing row counts and partition-level metrics:
Test Components
Create dedicated Python Test Components for custom validation logic that goes beyond native tests.
Generic test example
Generic tests are reusable functions that can be applied to any dataset. This example creates a statistical test that validates data skewness within acceptable ranges:
Singular test example
Singular tests validate specific datasets with custom business logic. This example checks that values in a particular column fall within expected bounds:
Next steps
- Explore SQL tests
- Learn about YAML tests
- Review the Tests concept guide for comprehensive test strategy guidance