How to Unit Test in dbt
Yes, it's possible- here's how you can do it before dbt releases this new feature.
With dbt tests, we test our data, but never our code.
Why is that?
Well, quite frankly, testing code is a software engineering best practice that seems to be still making its way over to the analytics workflow.
But, just because it isn’t the norm in analytics, doesn’t mean we shouldn’t do it! Unit testing our dbt models can prevent us from pushing major code changes to production without truly understanding their effects.
This is especially important if you are the sole data team- a team of 1- and have nobody else to review your code!
What is unit testing?
Unit testing tests your code rather than the data produced by your code. It prevents you from pushing logic that doesn’t function as you expect it to.
Unit testing has existed as a software engineering best practice for a while but isn’t used as often in analytics.
Unit testing vs assertion testing
dbt tests are something called assertion tests, which means you are checking the actual output against an expectant output. You are asserting that a value should meet a certain characteristic, and if it doesn’t, the code should fail to build.
While assertion tests are extremely helpful, and I recommend that everyone use them, they are more reactive than proactive. They check for something after it has already occurred.
Unit testing, on the other hand, is more proactive. These tests compare the behaviors of your code to the expected behaviors of your code. This way, any detrimental changes can be detected before the code has a chance to affect your production data.
How to Unit Test in dbt
Keep reading with a 7-day free trial
Subscribe to Learn Analytics Engineering to keep reading this post and get 7 days of free access to the full post archives.