dbt-labs / docs.getdbt.com

The code behind docs.getdbt.com
https://docs.getdbt.com/
Apache License 2.0
113 stars 865 forks source link

[Core] Assert an expected failure by overriding `error_if` #5719

Open dbeatty10 opened 5 days ago

dbeatty10 commented 5 days ago

Link to the page(s) on docs.getdbt.com requiring updates

We have docs for configuring severity for data tests:

https://docs.getdbt.com/reference/resource-configs/severity

We just want to add an example for using error_if to assert the expectation that a test will fail. This is helpful in integration testing for dbt packages like dbt_utils.

Tell us more about this update

Question:

Is there a way to add integration tests to validate that the test fails appropriately?

Answer:

Here is an example of using error_if in a clever way.

Explanation

By default, dbt treats:

  • 0 row as PASS
  • >0 rows as FAIL

Overriding the config to error_if: "<1" will invert that. To the best of my knowledge, this feature was discovered rather than designed, and it also requires adding warn_if: "<0" to work properly.

Reviewers/Stakeholders/SMEs

Dee bEighty Ten

Related GitHub issues

No response

Additional information

By default for dbt data tests:

Overriding the config to error_if: "<1" will invert the default logic so that:

This also requires adding warn_if: "<0" to work properly. Otherwise, the default warning condition will still take effect. Overriding warn_if in this way takes it out of play since there should never be less than 0 rows returned.