https://github.com/portovep/dbt-testing-examples
Examples of how to implement unit, component, and contract tests for dbt data apps
https://github.com/portovep/dbt-testing-examples
analytics-engineering contract-testing data-engineering dbt sql tutorial-code tutorial-exercises unit-testing
Last synced: 3 days ago
JSON representation
Examples of how to implement unit, component, and contract tests for dbt data apps
- Host: GitHub
- URL: https://github.com/portovep/dbt-testing-examples
- Owner: portovep
- Created: 2023-02-17T11:44:14.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-18T14:23:47.000Z (8 months ago)
- Last Synced: 2025-05-18T15:26:24.623Z (8 months ago)
- Topics: analytics-engineering, contract-testing, data-engineering, dbt, sql, tutorial-code, tutorial-exercises, unit-testing
- Homepage: https://portovep.github.io/dbt-testing-examples/
- Size: 155 MB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dbt-testing-examples
[](https://github.com/portovep/dbt-testing-examples/actions/workflows/cd-pipeline.yml)
This repository contains examples of how to implement real unit tests for a dbt data application by using the [dbt-unit-testing](https://github.com/EqualExperts/dbt-unit-testing) package. You can also check the articles I wrote on testing data pipelines and data products with dbt
- [How to Improve The Code Quality of your Dbt Models with Unit Tests and TDD](https://medium.com/@pablo.porto/improving-the-code-quality-of-your-dbt-models-with-unit-tests-and-tdd-203ed0be791e).
- [A Complete Guide to Effectively Scale your Data Pipelines and Data Products with Contract Testing and dbt](https://medium.com/towards-data-science/how-to-scale-your-data-pipelines-and-data-products-with-dbt-and-contract-testing-10c92ea9a443).
## Features
- Unit test and mocking examples with the dbt-unit-testing package
- Katas to get started unit testing models
- Component test examples with the dbt-unit-testing package
- Sources contract test examples with the dbt-expectations package
- Model contracts example with dbt 1.5
- CI/CD pipeline example with Github Actions
## Roadmap
- [x] ~Add unit test examples~
- [x] ~Add katas and improve README~
- [x] ~Add component test examples~
- [x] ~Add support for Github Codespaces~
- [x] ~Add contract test examples~
- [ ] Add data quality test examples
## Sample dbt app
Our sample dbt app is called health-insights. It takes weight and height data from upstream sources and calculates the metric body mass index. It follows the usual layered architecture commonly found in dbt projects.

## Types of tests
The repository contains examples for both unit and component tests.

## Katas
The repo contains two katas (small exercises) to learn how to implement unit tests for dbt models with the [dbt-unit-testing](https://github.com/EqualExperts/dbt-unit-testing) package:
- [Kata 1 - Adding support for imperial units](exercises/kata1.md) ([Solution](exercises/kata1-solution.md))
- [Kata 2 - Categorize body mass index (BMI) following WHO guidelines](exercises/kata1.md)
> A code kata is a software development exercise in which the focus is not on solving a task or problem, but on learning new skills and developing successful routines.
## Local setup
Install dbt following the [official documentation](https://docs.getdbt.com/docs/get-started/installation)
Spin up a Postgres database in a container
```
docker-compose up
```
Setup the environment variables
```
cp .env.example .env
```
Setup the dbt profile
```
cp profiles.example.yml profiles.yml
```
Install dbt dependencies
```
dbt deps
```
Check that everything works
```
dbt debug
```
Seed the database
```
dbt seed
```
## Running the tests
All tests
```
dbt test
```
Unit tests
```
dbt test --select tag:unit-tests
```
Component tests
```
dbt test --select tag:component-test
```
Contract tests
```
dbt test --select tag:contract-test-source
```
## Running data quality tests (Coming soon)
```
dbt test --select tag:data-quality
```
## Setup data observability
Build Elementary models. It will create empty tables, that will be updated with artifacts, metrics and test results in future dbt executions.
```
dbt run --select elementary
```
Run the tests
```
dbt test
```
Generate data observability report
```
edr report
```