Skip to content

Commit 9441e11

Browse files
authored
Add unit test coverage report to pre-merge (#1064)
2 parents c832dc5 + 0e7fa88 commit 9441e11

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# .github/workflows/coverage.yml
2+
name: Post coverage comment
3+
4+
on:
5+
workflow_run:
6+
workflows: ["Tests and Code Checks"]
7+
types:
8+
- completed
9+
10+
jobs:
11+
test:
12+
name: Run tests & display coverage
13+
runs-on: linux-ubuntu-latest
14+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
15+
permissions:
16+
# Gives the action the necessary permissions for publishing new
17+
# comments in pull requests.
18+
pull-requests: write
19+
# Gives the action the necessary permissions for editing existing
20+
# comments (to avoid publishing multiple comments in the same PR)
21+
contents: write
22+
# Gives the action the necessary permissions for looking up the
23+
# workflow that launched this workflow, and download the related
24+
# artifact that contains the comment to be published
25+
actions: read
26+
steps:
27+
# DO NOT run actions/checkout here, for security reasons
28+
# For details, refer to https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
29+
- name: Post comment
30+
uses: py-cov-action/python-coverage-comment-action@v3
31+
with:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}

.github/workflows/main.yml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
code-quality:
4545
name: Code Quality
4646

47-
runs-on: ubuntu-latest
47+
runs-on: linux-ubuntu-latest
4848
timeout-minutes: 10
4949

5050
strategy:
@@ -68,8 +68,17 @@ jobs:
6868
unit:
6969
name: unit test / python ${{ matrix.python-version }}
7070

71-
runs-on: ubuntu-latest
72-
timeout-minutes: 10
71+
runs-on: linux-ubuntu-latest
72+
timeout-minutes: 15
73+
74+
permissions:
75+
# Gives the action the necessary permissions for publishing new
76+
# comments in pull requests.
77+
pull-requests: write
78+
# Gives the action the necessary permissions for pushing data to the
79+
# python-coverage-comment-action branch, and for editing existing
80+
# comments (to avoid publishing multiple comments in the same PR)
81+
contents: write
7382

7483
strategy:
7584
fail-fast: false
@@ -88,12 +97,26 @@ jobs:
8897
- name: Install Hatch
8998
uses: pypa/hatch@install
9099

91-
- name: Run Unit Tests
92-
run: hatch run -v +py=${{ matrix.python-version }} test:unit
100+
- name: Run Unit Tests and Generate Coverage
101+
run: hatch run -v +py=${{ matrix.python-version }} test:unit-with-cov
102+
103+
# Only run coverage comment once (not for all python versions)
104+
- name: Coverage Comment
105+
if: matrix.python-version == '3.12' && github.event_name == 'pull_request'
106+
uses: py-cov-action/python-coverage-comment-action@v3
107+
with:
108+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
109+
110+
- name: Store Pull Request comment to be posted
111+
uses: actions/upload-artifact@v4
112+
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
113+
with:
114+
name: python-coverage-comment-action
115+
path: python-coverage-comment-action.txt
93116

94117
build:
95118
name: Build and Verify Packages
96-
runs-on: ubuntu-latest
119+
runs-on: linux-ubuntu-latest
97120

98121
steps:
99122
- name: Check out the repository

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ logs/
2020
*.sublime*
2121
.python-version
2222
.hatch
23+
.coverage*

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ dependencies = [
8181
"types-requests",
8282
"debugpy",
8383
"pydantic>=1.10.0, <2",
84+
"pytest-cov",
8485
]
8586
path = ".hatch"
8687
python = "3.9"
@@ -95,6 +96,7 @@ sqlw-e2e = "pytest --color=yes -v --profile databricks_uc_sql_endpoint -n auto -
9596

9697
[tool.hatch.envs.test.scripts]
9798
unit = "pytest --color=yes -v --profile databricks_cluster -n auto --dist=loadscope tests/unit"
99+
unit-with-cov = "pytest --color=yes -v --profile databricks_cluster -n auto --dist=loadscope tests/unit --cov=dbt"
98100

99101
[[tool.hatch.envs.test.matrix]]
100102
python = ["3.9", "3.10", "3.11", "3.12"]
@@ -132,3 +134,8 @@ disallow_untyped_defs = false
132134
[[tool.mypy.overrides]]
133135
module = ["databricks.*", "agate.*", "jinja2.*", "yaml.*"]
134136
ignore_missing_imports = true
137+
138+
[tool.coverage.run]
139+
relative_files = true
140+
branch = true
141+
source = ["dbt"]

0 commit comments

Comments
 (0)