{"id":19963265,"url":"https://github.com/bilbottom/dbt-unit-test-coverage","last_synced_at":"2025-05-03T22:31:54.909Z","repository":{"id":189526561,"uuid":"680792154","full_name":"Bilbottom/dbt-unit-test-coverage","owner":"Bilbottom","description":"Code coverage metrics for dbt models' unit tests.","archived":false,"fork":false,"pushed_at":"2024-03-21T07:30:42.000Z","size":254,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-03-21T09:37:28.477Z","etag":null,"topics":["dbt","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bilbottom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-08-20T12:23:52.000Z","updated_at":"2023-08-20T17:13:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"b80ac913-0697-4a9b-b92e-31ed3f109fc3","html_url":"https://github.com/Bilbottom/dbt-unit-test-coverage","commit_stats":null,"previous_names":["bilbottom/dbt-unit-test-coverage"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fdbt-unit-test-coverage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fdbt-unit-test-coverage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fdbt-unit-test-coverage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fdbt-unit-test-coverage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bilbottom","download_url":"https://codeload.github.com/Bilbottom/dbt-unit-test-coverage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224374655,"owners_count":17300691,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbt","python"],"created_at":"2024-11-13T02:15:26.191Z","updated_at":"2024-11-13T02:15:26.972Z","avatar_url":"https://github.com/Bilbottom.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![application-tests](https://github.com/Bilbottom/dbt-unit-test-coverage/actions/workflows/application-tests.yaml/badge.svg)](https://github.com/Bilbottom/dbt-unit-test-coverage/actions/workflows/application-tests.yaml)\n[![coverage](coverage.svg)](https://github.com/dbrgn/coverage-badge)\n[![GitHub last commit](https://img.shields.io/github/last-commit/Bilbottom/dbt-unit-test-coverage)](https://github.com/Bilbottom/dbt-unit-test-coverage/commits/main)\n\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat\u0026labelColor=ef8336)](https://pycqa.github.io/isort/)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Bilbottom/dbt-unit-test-coverage/main.svg)](https://results.pre-commit.ci/latest/github/Bilbottom/dbt-unit-test-coverage/main)\n\n\u003c/div\u003e\n\n---\n\n# dbt Unit Test Coverage 🧪🔣\n\nThis is a package to generate a code coverage metric for dbt models' unit tests.\n\nThis is not for public consumption (yet!), but rather to show off some Sainsbury's extensions. This also requires a specific fork of the dbt-unit-testing package, namely:\n\n- https://github.com/Bilbottom/dbt-unit-testing\n\n## Installation ⬇️\n\nThis is currently only available on GitHub, so you'll need to supply the GitHub URL to `pip`:\n\n```\npip install git+https://github.com/Bilbottom/dbt-unit-test-coverage@v0.0.2\n```\n\n## Usage 📖\n\nThis package will generate a **very rudimentary** coverage metric for the dbt models' unit tests. We can't measure line hits like we can with Python code, but we can measure the number of models that have unit tests and _which of their CTEs_ have unit tests.\n\nTo run the code coverage report, invoke the `dbt-unit-test-coverage` package after compiling your project:\n\n```bash\n# Compile your project\ndbt clean\ndbt deps\ndbt compile\n\n# Generate the coverage\ndbt-unit-test-coverage\n```\n\nHelp for the `dbt-unit-test-coverage` package can be generated using the `--help` flag thanks to [the `typer` library](https://typer.tiangolo.com/):\n\n```bash\ndbt-unit-test-coverage --help\n```\n\n### Run as a pre-commit hook\n\nRather than running this manually or as part of a CI workflow, you could choose to run this as a [local pre-commit hook](https://pre-commit.com/#repository-local-hooks).\n\nThe exact configuration will depend on the size of your project and your developer workflow, but the following approach is a quick-start that should work for most projects.\n\n```yaml\n# .pre-commit-config.yaml\ndefault_install_hook_types: [pre-commit, post-commit]\ndefault_stages: [commit]\n\nrepos:\n  - repo: local\n    hooks:\n      - id: dbt-unit-test-coverage\n        name: Generate dbt coverage report\n        entry: dbt-unit-test-coverage  --compile-dbt\n        language: system\n        pass_filenames: false\n        stages: [post-commit]\n        always_run: true\n```\n\n## Summary of extensions\n\n### CTE unit testing\n\nThis builds off of another fork, this one from the [EqualExperts/dbt-unit-testing](https://github.com/EqualExperts/dbt-unit-testing) repo:\n\n- https://github.com/Bilbottom/dbt-unit-testing\n\nThis fork adds the ability to unit test particular CTEs in a model, provided you follow [the dbt-labs style guide](https://github.com/dbt-labs/corp/blob/main/dbt_style_guide.md) and include [the `select * from final` line](https://github.com/dbt-labs/corp/blob/725b6e9cf2af208d24a52fc04095c2feaff20b9d/dbt_style_guide.md?plain=1#L157-L158) at the bottom of the model.\n\n### Code coverage\n\nWith the CTE unit tests, we can then measure the number of (logical) CTEs that don't have tests which gives us the coverage metric.\n\n## Future plans\n\nThe v1.5 release of [dbt-core](https://github.com/dbt-labs/dbt-core) introduced programmatic invocations:\n\n- https://docs.getdbt.com/reference/programmatic-invocations\n\nThis would allow us to piggyback off of the dbt functionality even more, rather than hacking together our own solutions.\n\nAdditionally, the coverage metric is easy to manipulate in your favour, so we intend to improve the metric by:\n\n- Validating the columns in the tests to make sure that each of the columns in the CTE are tested\n- Penalising CTEs that are overcomplicated and should be split into multiple CTEs (e.g. a CTE that has large sub-queries)\n\nThis is currently based on the [EqualExperts/dbt-unit-testing](https://github.com/EqualExperts/dbt-unit-testing) framework, but this package will be updated to also be able to use whatever native unit testing framework dbt ends up having -- see:\n\n- https://github.com/dbt-labs/dbt-core/discussions/8275\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilbottom%2Fdbt-unit-test-coverage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbilbottom%2Fdbt-unit-test-coverage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilbottom%2Fdbt-unit-test-coverage/lists"}