# Adding tests that stop creation of the model

**URL:** <https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432>\
**Category:** Help\
**Created:** [January 6, 2024, 9:27am UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432 "2024-01-06T09:27:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![moseleyi](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@moseleyi](https://discourse.getdbt.com/u/moseleyi)\
**Post date:** [January 6, 2024, 9:27am UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/1 "2024-01-06T09:27:24Z")

</div>

Hi,

Just wondering if things changed in the last year when it comes to tests and best practices. I would like to add simple, or more complex tests, to a model and configure it so that the model doesn’t get created if it doesn’t pass the test?

---

<div class="post-metadata">

**Author:** ![WyzeGwendolyn](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/wyzegwendolyn/32/3417_2.png) [@WyzeGwendolyn](https://discourse.getdbt.com/u/WyzeGwendolyn)\
**Post date:** [January 7, 2024, 3:11am UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/2 "2024-01-07T03:11:16Z")

</div>

Oooh… I love this question, @moseleyi ! I’d also appreciate knowing if best practices have changed over time. 😄

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 8, 2024, 7:12pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/3 "2024-01-08T19:12:44Z")

</div>

There are several ways I can think of.

**Option 1:**  
You can create a dummy dependency on the model, then put the test on that dependency. If the test fails, nothing downstream would run. Mention that dependency in a SQL comment.

`-- this is a sql comment. {{ref('my_thing')}} will still compile and show in the dag. But it won't get executed in the database`

`{# this is a jinja comment. nothing would compile #}`

The only shortcoming of this approach is that it only works when you build the upstream stuff as well. If you only build the one target model, the test would not build.

**Approach 2**  
use the dbt `run_query` macro ([About run\_query macro | dbt Developer Hub](https://docs.getdbt.com/reference/dbt-jinja-functions/run_query)) at the top of your model to test some result. Load the result to a variable and evaluate pass/fail. Then do something like this:

```auto
{% if execute %}
  {% if my_test_has_a_failed_condition %}
      {{ exceptions.raise_compiler_error("The test failed.") }}
  {% endif %}
{% endif %}

```

Basically dbt will stop running at that point.

There are a few other approaches I can think of, but option 1 is the most “dbtoncic”… so I would start there.

---

<div class="post-metadata">

**Author:** ![moseleyi](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@moseleyi](https://discourse.getdbt.com/u/moseleyi)\
**Post date:** [January 8, 2024, 7:28pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/4 "2024-01-08T19:28:37Z")

</div>

Ahh yes, I guess the approach #1 is basically to create an intermediate layer on which we run tests to stop the models used by BI/people

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 9, 2024, 1:42pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/5 "2024-01-09T13:42:47Z")

</div>

Yes. I assumed this model had no existing upstream dependencies, which is why I suggested a dummy upstream model. But since you are talking about models consumed by BI, certainly there are upstream models where the test can be applied, right.

And don’t know why I didn’t think of this before, but you could just put the custom test on the source if it is the first model in the chain. So really there is no scenario where you need a dummy model with the comment approach. Just regular old dbt testing. Test the thing that is upstream, source or model.

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 9, 2024, 1:48pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/6 "2024-01-09T13:48:31Z")

</div>

I’m creating a new reply so this thread has a super clear solution for future folks who stumble on it.

The correct way to do this in dbt is to put the test on the upstream object, source or model.

If you need it to work when the model is run in isolation and not just when the entire DAG is run, then a more complicated approach needs to be taken.

But best practice would say solve at the simplest and most standard way.

Please mark this is the solution if it helped you.

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 18, 2024, 4:30pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/7 "2024-01-18T16:30:14Z")

</div>

dbt is adding a feature called “unit tests”, which solves your question in a new way. Unit Tests will run **before** the creation of a model, which is what you are looking for.

It should be released in dbt 1.8, around April 2024 timeframe.

Cheers!

---

<div class="post-metadata">

**Author:** ![moseleyi](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@moseleyi](https://discourse.getdbt.com/u/moseleyi)\
**Post date:** [January 18, 2024, 4:42pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/8 "2024-01-18T16:42:28Z")

</div>

Interesting… and definitely worth the wait!

---

<div class="post-metadata">

**Author:** ![lucas-ciandt](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/lucas-ciandt/32/5640_2.png) [@lucas-ciandt](https://discourse.getdbt.com/u/lucas-ciandt)\
**Post date:** [May 7, 2025, 2:44pm UTC](https://discourse.getdbt.com/t/adding-tests-that-stop-creation-of-the-model/11432/9 "2025-05-07T14:44:05Z")

</div>

our colleague is trying to implement what is called the “write-audit-publish” pattern, in which - as he said - you only make the model available (publish - or write to its final destination) IF auditing passes (data tests)

unit tests are _not_ meant to accomplish this  
unit tests are meant to run in a CI/CD pipeline with dummy data - just like unit test work for any programming language… it should _not_ run during actual model creation

it seems up to today dbt does not support an elegant way to avoid bad data to persist even tough the data test may have failed
