dbt: running unit test and data test

dbt data test is used to test validity of the data during the Transform and load stage and that can be easily setup using the followings example

Please note: The model is your file name which is  "snowflake_sample_data_store_sales". Remember dbt always use the filename as model name. 

Data test


models:
  - name: snowflake_sample_data_store_sales
    description: List of store sales records with basic cleaning and transformation applied.
    columns:
      - name: SS_ITEM_SK
        description: The unique key for each location.
        data_tests:
          - not_null

if you run dbt test now, you will get the following output here:


Unit test 

We also have unit test in dbt - that allow us to run unit test against our data. This is useful to ensure our sql used for our TL is working as expected. 

You need to ensure the model name matches your filename - otherwise dbt won't be able to discover your test cases. 

snowflake_sample_data_store_sales.yml


models:
  - name: snowflake_sample_data_store_sales ## <-- MUST MATCH WITH MODEL BELOW AND IT
IS YOUR FILENAME - IF DOES NOT MATCH, THEN YOUR TEST CASES WON'T BE DISCOVERED

    description: List of store sales records with basic cleaning and transformation applied.
    columns:
      - name: SS_ITEM_SK
        description: The unique key for each location.
        data_tests:
          - not_null

unit_tests:
  - name: test_does_location_opened_at_trunc_to_date
    description: "Check that opened_at timestamp is properly truncated to a date."
    model: snowflake_sample_data_store_sales ## <-- MUST MATCH WITH MODEL ABOVE
    given:
      - input: source('SNOWFLAKE_SAMPLE_DATA', 'store_sales')  # ← Fix: Use proper source reference
        rows:
          - SS_ITEM_SK: 1
            SS_SOLD_DATE_SK: 20160901
            SS_SOLD_TIME_SK: 123456
            SS_NET_PAID: 80.00
            SS_NET_PAID_INC_TAX: 100.00
            SS_NET_PROFIT: 30.00
    expect:
      rows:
        - SS_ITEM_SK: 1
          SS_SOLD_DATE_SK: 20160901
          SS_SOLD_TIME_SK: 123456
          SS_NET_PAID: 80.00
          SS_NET_PAID_INC_TAX: 100.00
          SS_NET_PROFIT: 30.00  

       

And as you can see here, the 2 tests are being discovered:-



Troubleshooting unit test not found can be hard. I tried to document those above where specifically mentioned that we should match the model name correctly







Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20