Running tests with dbt test - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When running tests with dbt test, we want to know how the time it takes grows as the number of tests or data size increases.
We ask: How does running more tests or bigger data affect the total time?
Analyze the time complexity of the following dbt test command snippet.
-- Run all tests defined in the project
dbt test
-- Example of a simple test definition
select * from {{ ref('my_model') }} where id is null
This runs all tests defined in the dbt project, checking data quality rules like null values.
Look for repeated actions that take time.
- Primary operation: Running each test query against the database.
- How many times: Once per test defined in the project.
As the number of tests grows, the total time grows roughly in direct proportion.
| Input Size (number of tests) | Approx. Operations (test queries run) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: Doubling the number of tests roughly doubles the total time.
Time Complexity: O(n)
This means the total time grows linearly with the number of tests run.
[X] Wrong: "Running more tests will take the same time because they run in parallel."
[OK] Correct: While some parallelism may happen, each test still requires database work, so total time grows with test count.
Understanding how test execution time grows helps you plan and optimize data quality checks in real projects.
"What if we only run tests on changed models instead of all models? How would the time complexity change?"
Practice
dbt test in a dbt project?Solution
Step 1: Understand the role of
dbt testdbt testruns tests defined in your project to check data quality and catch errors.Step 2: Differentiate from other dbt commands
Building models is done withdbt run, deployment is outsidedbt test, and documentation is generated withdbt docs.Final Answer:
To check data quality and find errors in your data models -> Option CQuick Check:
dbt test = data quality checks [OK]
- Confusing
dbt testwithdbt run - Thinking
dbt testdeploys models - Assuming
dbt testgenerates docs
customers?Solution
Step 1: Identify the correct flag for running tests on specific models
The flag--selectis used withdbt testto specify which models to test.Step 2: Check other options for correctness
dbt runbuilds models, not tests.--modelsis not a valid flag fordbt test.--onlyis not a valid flag.Final Answer:
dbt test --select customers -> Option AQuick Check:
Use --select to target tests [OK]
- Using
dbt runinstead ofdbt test - Using invalid flags like
--modelsor--only - Confusing
--selectwith other flags
schema.yml test definition:models:
- name: orders
tests:
- unique:
column_name: order_id
- not_null:
column_name: order_dateWhat will
dbt test check for the orders model?Solution
Step 1: Read the test types for each column
The testuniqueapplies toorder_id, ensuring no duplicates. The testnot_nullapplies toorder_date, ensuring no missing values.Step 2: Match tests to their meaning
uniquemeans no duplicates;not_nullmeans no nulls. So the checks are:order_idis unique andorder_datehas no null values.Final Answer:
It checks thatorder_idis unique andorder_datehas no null values -> Option AQuick Check:
unique = no duplicates, not_null = no nulls [OK]
- Mixing up unique and not_null tests
- Assuming both columns have the same test
- Ignoring the column_name key in test definitions
dbt test but get an error: Compilation Error: Could not find test 'uniquee'. What is the likely cause?Solution
Step 1: Analyze the error message
The error says it cannot find test 'uniquee', which looks like a misspelled test name.Step 2: Identify common causes of compilation errors
Typos in test names inschema.ymlcause dbt to fail to find the test. Model name or connection errors produce different messages.Final Answer:
A typo in the test name in schema.yml -> Option DQuick Check:
Compilation errors often mean typos [OK]
- Ignoring typo errors and rerunning blindly
- Assuming connection issues cause compilation errors
- Confusing model name errors with test name errors
email column in your users model is unique and not null. You also want to run tests only on this model. Which schema.yml snippet and command combination is correct?Solution
Step 1: Identify correct test syntax in
Tests on columns are defined underschema.ymlcolumnswithnameandtestslist.models: - name: users columns: - name: email tests: - unique - not_null
Command:dbt test --select usersshows the correct format.Step 2: Choose the correct command to run tests on the
usersmodeldbt test --select usersruns tests only on the users model.models: - name: users columns: - name: email tests: - unique - not_null
Command:dbt test --select usersuses this command correctly.Final Answer:
Option B with column-level tests and dbt test --select users -> Option BQuick Check:
Column tests + --select flag = correct [OK]
- Defining tests directly under model without column_name (invalid syntax)
- Using
dbt runinstead ofdbt test - Using incorrect flags like
--models
