# Testing sources .denser-text[ You can add tests to sources! And you should! ] .denser-text[ ```yml version: 2 sources: - name: jaffle_shop database: raw schema: jaffle_shop tables: - name: customers identifier: customers columns: - name: id tests: - not_null - unique ``` ] .denser-text[ Being thoughtful about where you test helps you understand whether tests are failing because: - The assumptions about your source data are no longer true - Or your SQL has changed This allows you to debug more quickly! ] ??? Demo: While looking at the source yml ask the group what is being tested in this source (look for the table name, column name, and specific tests) ??? Can you add tests to sources? Yes you can and you absolutely should! If there's something wrong with your data, you will be able to see if it was caused by something going wrong in your source data, or in one of your models. --

demo

??? Demo Time: - add tests to your sources.yml - run `dbt test` - Acknowledge this will run all tests (models and sources) --- # Testing Sources We can run tests on _only_ sources with the following command `dbt test -s source:* ` This can be helpful in deployment - coming up soon --

demo

??? Demo Time: - run `dbt test -s source:*` --- # In deployment A common approach: Test the schema integrity of raw tables _before_ replacing production models. ```bash dbt test -s source:* # ensure no duplicates or unexpected nulls -- the job won't continue if these tests fail dbt run # only runs if test above succeeds dbt test --exclude source:* # or you can even skip these! dbt source freshness # powers freshness viz in dbt Cloud ``` Another option: `source freshness` first, so that your job won't run on stale data. ??? More complex deployments: - Blue/green that renames schema/database at last step (if all tests pass) or leaves old objects in place (if a test fails)