Unit testing in Snowflake verifies that queries, views, and stored procedures return expected results by running them against controlled test data and asserting outcomes.
Unit testing in Snowflake is the practice of running small, isolated checks that confirm a query, view, or stored procedure produces the expected result when fed deterministic data. It prevents silent logic errors from reaching production.
Tests catch broken transformations early, document intent, and enable safe refactoring. They also help CI/CD pipelines block faulty SQL before deployment, keeping dashboards and downstream services accurate.
Use an isolated schema (e.g., TESTING) to store fixtures and avoid polluting production tables.
Load minimal but representative rows into Customers
, Orders
, Products
, and OrderItems
. Keep IDs hard-coded for repeatability.
Wrap assertions in anonymous blocks or stored procedures so they can be invoked by CI tools.
Snowflake supports ASSERT <condition>
. When the condition is FALSE, execution stops with an error that CI can detect.
BEGIN
LET calc_total NUMBER := (
SELECT SUM(oi.quantity * p.price)
FROM OrderItems oi
JOIN Products p ON p.id = oi.product_id
WHERE oi.order_id = 100
);
ASSERT calc_total = 199.98;
END;
The block below tests that the view vw_order_totals
returns the correct sum for order 100.
BEGIN
LET expected NUMBER := 199.98;
LET actual NUMBER := (
SELECT total_amount
FROM vw_order_totals
WHERE id = 100
);
ASSERT actual = expected;
END;
Reset fixtures inside each test, keep tests idempotent, and test one assumption per block. Automate execution via Snowflake Tasks or external CI runners.
Using production data in tests causes flaky results. Always isolate fixtures in a test schema.
Relying on row order makes assertions brittle. Compare scalar results or ordered sets explicitly.
Yes. Snowpark or the Snowflake Python connector can execute test blocks and parse failures, making it easy to integrate with PyTest or similar frameworks.
Store test scripts in your repo, deploy to a CI runner, and call them with the SnowSQL CLI. Fail the pipeline if any ASSERT statement raises an error.
Open-source tools like dbt tests or Great Expectations can validate data but Snowflake Scripting offers the lowest-latency, in-database assertions.