Package: dbt-coverage
dbt-coverageOne-stop-shop for docs and test coverage ofdbtprojects.Optimized for dbt 1.0, seefull support matrix.Why do I need something like this?dbt-coverageis todbtwhatcoverage.pyandinterrogateare to Python.It is a single CLI tool which checks yourdbtproject for missing documentation and tests.Keeping documentation and tests close to the actual SQL code that generates the final model is one of the best design choices ofdbt. It ensures documentation is actually useful and tests are actually used. But how do you make adding those a habit in yourdbtproject?That is exactly wheredbt-coveragecomes in. It willGive you a better sense of the level of documentation and test coverage in your project;Help your CI/CD pipeline make sure new changes include documentation and tests;Let you quickly assess the documentation and tests of a newdbtproject you get your hands on.Still not convinced? Here are some more features:✨zero-config: just install it and run it, there is nothing to set up🏁minimal dependences: the only dependencies areclick(already installed withdbt) andtyper📦very small: at ~480SLOC, you can easily validate it works as advertisedDemoThe package was presented duringCoalesce,
the annual dbt conference, as a part of the talkFrom 100 spreadsheets to 100 data analysts: the story of dbt at Slido.
Watch a demo in the video below.Installationpip install dbt-coverageUsagedbt-coveragecomes with two basic commands:computeandcompare. The
documentation for the individual commands can be shown by using the--helpoption.ComputeCompute coverage fromtarget/catalog.jsonandtarget/manifest.jsonfiles
found in a dbt project, e.g.jaffle_shop.To choose between documentation and test coverage, passdocortestas the CLI argument.$cdjaffle_shop$dbtrun# Materialize models$dbtdocsgenerate# Generate catalog.json and manifest.json$dbt-coveragecomputedoc--cov-reportcoverage-doc.json# Compute doc coverage, print it and write it to coverage-doc.json fileCoverage report=====================================================================jaffle_shop.customers                                  6/7      85.7%jaffle_shop.orders                                     9/9     100.0%jaffle_shop.raw_customers                              0/3       0.0%jaffle_shop.raw_orders                                 0/4       0.0%jaffle_shop.raw_payments                               0/4       0.0%jaffle_shop.stg_customers                              0/3       0.0%jaffle_shop.stg_orders                                 0/4       0.0%jaffle_shop.stg_payments                               0/4       0.0%=====================================================================Total                                                 15/38     39.5%$dbt-coveragecomputetest--cov-reportcoverage-test.json# Compute test coverage, print it and write it to coverage-test.json fileCoverage report=====================================================================jaffle_shop.customers                                  1/7      14.3%jaffle_shop.orders                                     8/9      88.9%jaffle_shop.raw_customers                              0/3       0.0%jaffle_shop.raw_orders                                 0/4       0.0%jaffle_shop.raw_payments                               0/4       0.0%jaffle_shop.stg_customers                              1/3      33.3%jaffle_shop.stg_orders                                 2/4      50.0%jaffle_shop.stg_payments                               2/4      50.0%=====================================================================Total                                                 14/38     36.8%Filtering model paths with--model-path-filterYou can also choose a subset of tables to compare using one or multiple--model-path-filteroptions.$cdjaffle_shop$dbtrun# Materialize models$dbtdocsgenerate# Generate catalog.json and manifest.json$dbt-coveragecomputedoc--cov-reportcoverage-doc.json--model-path-filtermodels/staging/# Compute doc coverage for a subset of tables, print it and write it to coverage-doc.json fileCoverage report======================================================jaffle_shop.stg_customers              0/3       0.0%jaffle_shop.stg_orders                 0/4       0.0%jaffle_shop.stg_payments               0/4       0.0%======================================================Total                                  0/11      0.0%$dbt-coveragecomputedoc--cov-reportcoverage-doc.json--model-path-filtermodels/orders.sql--model-path-filtermodels/staging/# Compute doc coverage for a subset of tables, print it and write it to coverage-doc.json fileCoverage report======================================================jaffle_shop.orders                     0/9       0.0%jaffle_shop.stg_customers              0/3       0.0%jaffle_shop.stg_orders                 0/4       0.0%jaffle_shop.stg_payments               0/4       0.0%======================================================Total                                  0/20      0.0%Markdown output with--cov-formatYou can also choose to print the output in the Markdown table format by specifying the--cov-formatoption.
This can be especially useful when usingdbt-coveragein CI/CD pipelines.$cdjaffle_shop$dbtrun# Materialize models$dbtdocsgenerate# Generate catalog.json and manifest.json$dbt-coveragecomputedoc--model-path-filtermodels/staging/--cov-formatmarkdown#Coveragereport| Model | Columns Covered | % ||:------|----------------:|:-:|| jaffle_shop.stg_customers                         |     0/3     |   0.0% || jaffle_shop.stg_orders                            |     0/4     |   0.0% || jaffle_shop.stg_payments                          |     0/4     |   0.0% || Total                                             |     0/11    |   0.0% |Custom run artifacts path with--run-artifacts-dirTo compute the coverages,dbt-coveragelooks up the artefacts from thedbt runexecution in the./target/folder in the current directory. You can specify a custom path via the--run-artifacts-diroption.$dbt-coveragecomputedoc--run-artifacts-dirjaffle_shop/target--cov-reportcoverage-doc.json# Compute doc coverage from the artefacts located in jaffle_shop/target, print it and write it to coverage-doc.json fileCoverage report================================================jaffle_shop.customers             0/7       0.0%jaffle_shop.orders                0/9       0.0%jaffle_shop.raw_customers         0/3       0.0%jaffle_shop.raw_orders            0/4       0.0%jaffle_shop.raw_payments          0/4       0.0%jaffle_shop.stg_customers         0/3       0.0%jaffle_shop.stg_orders            0/4       0.0%jaffle_shop.stg_payments          0/4       0.0%================================================Total                             0/38      0.0%CompareCompare twocoverage.jsonfiles generated by thecomputecommand. This is
useful to ensure that the coverage does not drop while making changes to the
project.$dbt-coveragecomparecoverage-after.jsoncoverage-before.json#Coveragedeltasummarybefore     after            +/-=============================================Coverage      39.47%    38.46%         -1.01%=============================================Tables             8         8          +0/+0Columns           38        39          +1/+0=============================================Hits              15        15          +0/+0Misses            23        24          +1/+0=============================================#Newmisses=========================================================================Catalog                         15/38   (39.47%)  ->    15/39   (38.46%)=========================================================================- jaffle_shop.customers          6/7    (85.71%)  ->     6/8    (75.00%)-- new_col                       -/-       (-)    ->     0/1     (0.00%)=========================================================================Combined use-case$cdmy-dbt-project$dbtrun# Materialize models$dbtdocsgenerate# Generate catalog.json and manifest.json$dbt-coveragecomputedoc--cov-reportbefore.json--cov-fail-under0.5# Fail if coverage is lower than 50%#Makechangestothedbtproject,e.g.addsomecolumnstotheDWH,documentsomecolumns,etc.$dbtrun# Materialize the changed models$dbtdocsgenerate# Generate catalog.json and manifest.json$dbt-coveragecomputedoc--cov-reportafter.json--cov-fail-comparebefore.json# Fail if the current coverage is lower than coverage in before.json$dbt-coveragecompareafter.jsonbefore.json# Generate a detailed coverage delta reportSupporteddbtversionsDifferent version ofdbt-coveragesupport different versions ofdbt. Here is
the support matrix.dbtdbt-coverage<0.20not tested0.20 - 0.210.11.0 - 1.60.2, 0.3Related packageshttps://github.com/mikaelene/dbt-test-coverageinterrogate(docs coverage for Python)coverage.py(execution coverage for Python)LicenseLicensed under the MIT license (seeLICENSE.mdfile for more
details).