Skip to content

Commit d211307

Browse files
authored
tests: refactor unit test nox sessions (#873)
This change is needed as part of b/463296248. Instead of having 4 different presubmits that run 4 different nox unit tests, we now have all 4 patterns tested under a single nox session. This follows the pattern that we have in google-cloud-python where there is a single unit test nox session.
1 parent 2196e2a commit d211307

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
lines changed

.github/workflows/unittest.yml

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,38 @@ on:
66
- main
77

88
jobs:
9-
run-unittests:
9+
unit-prerelease:
10+
name: prerelease_deps
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python: ["3.14"]
15+
option: ["prerelease"]
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
- name: Setup Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python }}
23+
allow-prereleases: true
24+
- name: Install nox
25+
run: |
26+
python -m pip install --upgrade setuptools pip wheel
27+
python -m pip install nox
28+
- name: Run ${{ matrix.option }} tests
29+
env:
30+
COVERAGE_FILE: .coverage${{ matrix.option }}-${{matrix.python }}
31+
run: |
32+
nox -s prerelease_deps
33+
unit:
1034
name: unit${{ matrix.option }}-${{ matrix.python }}
1135
# TODO(https://github.com/googleapis/gapic-generator-python/issues/2303): use `ubuntu-latest` once this bug is fixed.
1236
# Use ubuntu-22.04 until Python 3.7 is removed from the test matrix
1337
# https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories
1438
runs-on: ubuntu-22.04
1539
strategy:
1640
matrix:
17-
option: ["", "_grpc_gcp", "_wo_grpc", "_w_prerelease_deps", "_w_async_rest_extra"]
1841
python:
1942
- "3.7"
2043
- "3.8"
@@ -24,13 +47,6 @@ jobs:
2447
- "3.12"
2548
- "3.13"
2649
- "3.14"
27-
exclude:
28-
- option: "_wo_grpc"
29-
python: 3.7
30-
- option: "_wo_grpc"
31-
python: 3.8
32-
- option: "_wo_grpc"
33-
python: 3.9
3450
steps:
3551
- name: Checkout
3652
uses: actions/checkout@v4
@@ -45,21 +61,21 @@ jobs:
4561
python -m pip install nox
4662
- name: Run unit tests
4763
env:
48-
COVERAGE_FILE: .coverage${{ matrix.option }}-${{matrix.python }}
64+
COVERAGE_FILE: .coverage-${{matrix.python }}
4965
run: |
50-
nox -s unit${{ matrix.option }}-${{ matrix.python }}
66+
nox -s unit-${{ matrix.python }}
5167
- name: Upload coverage results
5268
uses: actions/upload-artifact@v4
5369
with:
54-
name: coverage-artifact-${{ matrix.option }}-${{ matrix.python }}
55-
path: .coverage${{ matrix.option }}-${{ matrix.python }}
70+
name: coverage-artifact-${{ matrix.python }}
71+
path: .coverage-${{ matrix.python }}
5672
include-hidden-files: true
5773

5874
report-coverage:
5975
name: cover
6076
runs-on: ubuntu-latest
6177
needs:
62-
- run-unittests
78+
- unit
6379
steps:
6480
- name: Checkout
6581
uses: actions/checkout@v4

noxfile.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -215,46 +215,41 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal
215215

216216

217217
@nox.session(python=PYTHON_VERSIONS)
218-
def unit(session):
218+
@nox.parametrize(
219+
["install_grpc_gcp", "install_grpc", "install_async_rest"],
220+
[
221+
(False, True, False), # Run unit tests with grpcio installed
222+
(True, True, False), # Run unit tests with grpcio/grpcio-gcp installed
223+
(False, False, False), # Run unit tests without grpcio installed
224+
(False, True, True), # Run unit tests with grpcio and async rest installed
225+
],
226+
)
227+
def unit(session, install_grpc_gcp, install_grpc, install_async_rest):
219228
"""Run the unit test suite."""
220-
default(session)
221229

222-
223-
@nox.session(python=PYTHON_VERSIONS)
224-
def unit_w_prerelease_deps(session):
225-
"""Run the unit test suite."""
226-
default(session, prerelease=True)
227-
228-
229-
@nox.session(python=PYTHON_VERSIONS)
230-
def unit_grpc_gcp(session):
231-
"""
232-
Run the unit test suite with grpcio-gcp installed.
233-
`grpcio-gcp` doesn't support protobuf 4+.
234-
Remove extra `grpcgcp` when protobuf 3.x is dropped.
235-
https://github.com/googleapis/python-api-core/issues/594
236-
"""
237-
constraints_path = str(
238-
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
230+
# `grpcio-gcp` doesn't support protobuf 4+.
231+
# Remove extra `grpcgcp` when protobuf 3.x is dropped.
232+
# https://github.com/googleapis/python-api-core/issues/594
233+
if install_grpc_gcp:
234+
constraints_path = str(
235+
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
236+
)
237+
# Install grpcio-gcp
238+
session.install("-e", ".[grpcgcp]", "-c", constraints_path)
239+
# Install protobuf < 4.0.0
240+
session.install("protobuf<4.0.0")
241+
242+
default(
243+
session=session,
244+
install_grpc=install_grpc,
245+
install_async_rest=install_async_rest,
239246
)
240-
# Install grpcio-gcp
241-
session.install("-e", ".[grpcgcp]", "-c", constraints_path)
242-
# Install protobuf < 4.0.0
243-
session.install("protobuf<4.0.0")
244-
245-
default(session)
246-
247-
248-
@nox.session(python=PYTHON_VERSIONS)
249-
def unit_wo_grpc(session):
250-
"""Run the unit test suite w/o grpcio installed"""
251-
default(session, install_grpc=False)
252247

253248

254-
@nox.session(python=PYTHON_VERSIONS)
255-
def unit_w_async_rest_extra(session):
256-
"""Run the unit test suite with the `async_rest` extra"""
257-
default(session, install_async_rest=True)
249+
@nox.session(python=DEFAULT_PYTHON_VERSION)
250+
def prerelease_deps(session):
251+
"""Run the unit test suite."""
252+
default(session, prerelease=True)
258253

259254

260255
@nox.session(python=DEFAULT_PYTHON_VERSION)

0 commit comments

Comments
 (0)