How to Unit Test in Snowflake

Galaxy Glossary

How do I create unit tests in Snowflake?

Unit testing in Snowflake verifies that queries, views, and stored procedures return expected results by running them against controlled test data and asserting outcomes.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What is unit testing in Snowflake?

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.

Why should data teams unit-test Snowflake objects?

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.

How do I set up a Snowflake unit-test framework?

Step 1 – Create a dedicated test schema

Use an isolated schema (e.g., TESTING) to store fixtures and avoid polluting production tables.

Step 2 – Insert deterministic fixture data

Load minimal but representative rows into Customers, Orders, Products, and OrderItems. Keep IDs hard-coded for repeatability.

Step 3 – Write Snowflake Scripting test blocks

Wrap assertions in anonymous blocks or stored procedures so they can be invoked by CI tools.

What syntax does Snowflake Scripting use for assertions?

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;

Example: Verify order total calculation

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;

Best practices for reliable Snowflake tests

Reset fixtures inside each test, keep tests idempotent, and test one assumption per block. Automate execution via Snowflake Tasks or external CI runners.

Common mistakes and how to avoid them

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.

Why How to Unit Test in Snowflake is important

How to Unit Test in Snowflake Example Usage


-- Validate that a customer’s order count is correct
BEGIN
  LET expected INT := 3;
  LET actual INT := (
      SELECT COUNT(*)
      FROM Orders
      WHERE customer_id = 42
  );
  ASSERT actual = expected;
END;

How to Unit Test in Snowflake Syntax


-- Snowflake Scripting assertion syntax
BEGIN
  -- Arrange test data (optional load into TEMP table)
  LET expected NUMBER := 199.98;

  -- Act: run the function / query under test
  LET actual NUMBER := (
      SELECT total_amount
      FROM vw_order_totals
      WHERE id = 100
  );

  -- Assert: fail if values differ
  ASSERT actual = expected;
END;

-- Run multiple tests in CI
CALL run_all_tests();

Common Mistakes

Frequently Asked Questions (FAQs)

Can I use Python to run Snowflake unit tests?

Yes. Snowpark or the Snowflake Python connector can execute test blocks and parse failures, making it easy to integrate with PyTest or similar frameworks.

How do I automate tests on every pull request?

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.

Is there a third-party testing framework for Snowflake?

Open-source tools like dbt tests or Great Expectations can validate data but Snowflake Scripting offers the lowest-latency, in-database assertions.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo