Skip to content

Commit b950c57

Browse files
mjohanse-emrMichael Johansen
andauthored
Add unit test workflows to ni-apis-python. (#8)
* Add unit test workflows to ni-apis-python. Signed-off-by: Michael Johansen <[email protected]> * Move test python files into a 'unit' folder. Signed-off-by: Michael Johansen <[email protected]> * Make artifact names unique for uploading test results. Signed-off-by: Michael Johansen <[email protected]> * Pass the submodules input to the checkout command in some workflows. Signed-off-by: Michael Johansen <[email protected]> * Create a composite action for running and uploading unit test results. Signed-off-by: Michael Johansen <[email protected]> * Remove outputs section since there aren't any outputs. Signed-off-by: Michael Johansen <[email protected]> * Add shell attribute to run calls in composite action. Signed-off-by: Michael Johansen <[email protected]> * Re-word step title to be more readable. Signed-off-by: Michael Johansen <[email protected]> * Address review feedback about descriptions and casing. Signed-off-by: Michael Johansen <[email protected]> * Fix cache key generation to use valid python version string. Signed-off-by: Michael Johansen <[email protected]> * Update the setup-python and setup-poetry actions to 0.4.0 to get the pythonVersion environment variable. Signed-off-by: Michael Johansen <[email protected]> * Use format() to properly reference the package name within the hashFiles call. Signed-off-by: Michael Johansen <[email protected]> --------- Signed-off-by: Michael Johansen <[email protected]> Co-authored-by: Michael Johansen <[email protected]>
1 parent 119ce90 commit b950c57

File tree

9 files changed

+122
-10
lines changed

9 files changed

+122
-10
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: 'Run and upload unit tests'
2+
description: 'Run unit tests using pytest and upload the results as an artifact.'
3+
inputs:
4+
package-name:
5+
description: 'The name of the package to run tests for.'
6+
required: true
7+
runs:
8+
using: "composite"
9+
steps:
10+
- name: Cache ${{ inputs.package-name }} virtualenv
11+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
12+
with:
13+
path: ./packages/${{ inputs.package-name }}/.venv
14+
key: ${{ inputs.package-name }}-${{ runner.os }}-py${{ env.pythonVersion }}-${{ hashFiles(format('packages/{0}/poetry.lock', inputs.package-name)) }}
15+
- name: Install ${{ inputs.package-name }}
16+
run: poetry install -v
17+
working-directory: ./packages/${{ inputs.package-name }}
18+
shell: bash
19+
- name: Run ${{ inputs.package-name }} unit tests and code coverage
20+
run: poetry run pytest ./tests/unit -v --cov=${{ inputs.package-name }} --junitxml=test_results/${{ inputs.package-name }}-${{ matrix.os }}-py${{ matrix.python-version }}.xml
21+
working-directory: ./packages/${{ inputs.package-name }}
22+
shell: bash
23+
- name: Upload ${{ inputs.package-name }} test results
24+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
25+
with:
26+
name: test_results_unit_${{ inputs.package-name }}_${{ matrix.os }}_py${{ matrix.python-version }}
27+
path: ./packages/${{ inputs.package-name }}/test_results/*.xml
28+
if: always()

.github/workflows/CI.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@ jobs:
1313
name: Check ni.protobuf.types
1414
uses: ./.github/workflows/check_package.yml
1515
with:
16-
package_name: ni.protobuf.types
16+
package-name: ni.protobuf.types
1717
check_ni_pythonpanel_v1_proto:
1818
name: Check ni.pythonpanel.v1.proto
1919
uses: ./.github/workflows/check_package.yml
2020
with:
21-
package_name: ni.pythonpanel.v1.proto
21+
package-name: ni.pythonpanel.v1.proto
22+
run_unit_tests:
23+
name: Run unit tests
24+
uses: ./.github/workflows/run_unit_tests.yml
25+
needs: [check_ni_protobuf_types, check_ni_pythonpanel_v1_proto]
26+
report_test_results:
27+
name: Report test results
28+
uses: ./.github/workflows/report_test_results.yml
29+
needs: [run_unit_tests]
30+
if: always()
31+
permissions:
32+
contents: read
33+
checks: write
34+
pull-requests: write

.github/workflows/check_package.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Check Package
33
on:
44
workflow_call:
55
inputs:
6-
package_name:
6+
package-name:
77
description: 'The name of the package to check.'
88
default: ''
99
required: true
@@ -12,15 +12,17 @@ on:
1212

1313
jobs:
1414
check_package:
15-
name: Check ${{ inputs.package_name }}
15+
name: Check ${{ inputs.package-name }}
1616
runs-on: ubuntu-latest
1717
defaults:
1818
run:
1919
# Set the working-directory for all steps in this job.
20-
working-directory: ./packages/${{ inputs.package_name }}
20+
working-directory: ./packages/${{ inputs.package-name }}
2121
steps:
2222
- name: Check out repo
2323
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
24+
with:
25+
submodules: true
2426
- name: Set up Python
2527
uses: ni/python-actions/setup-python@a3bfe1baa6062fd6157683651d653d527967d4d4 # v0.3.1
2628
id: setup-python
@@ -31,9 +33,9 @@ jobs:
3133
- name: Cache virtualenv
3234
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
3335
with:
34-
path: packages/${{ inputs.package_name }}/.venv
35-
key: ${{ inputs.package_name }}-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('packages/${{ inputs.package_name }}/poetry.lock') }}
36-
- name: Install ${{ inputs.package_name }}
36+
path: packages/${{ inputs.package-name }}/.venv
37+
key: ${{ inputs.package-name }}-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles(format('packages/{0}/poetry.lock', inputs.package-name)) }}
38+
- name: Install ${{ inputs.package-name }}
3739
run: poetry install -v
3840
- name: Lint
3941
run: poetry run ni-python-styleguide lint
@@ -50,10 +52,10 @@ jobs:
5052
with:
5153
python-platform: Linux
5254
version: PATH
53-
working-directory: ./packages/${{ inputs.package_name }}
55+
working-directory: ./packages/${{ inputs.package-name }}
5456
- name: Pyright static analysis (Windows)
5557
uses: jakebailey/pyright-action@b5d50e5cde6547546a5c4ac92e416a8c2c1a1dfe # v2.3.2
5658
with:
5759
python-platform: Windows
5860
version: PATH
59-
working-directory: ./packages/${{ inputs.package_name }}
61+
working-directory: ./packages/${{ inputs.package-name }}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Report test results
2+
3+
on:
4+
workflow_call:
5+
workflow_dispatch:
6+
7+
jobs:
8+
report_test_results:
9+
name: Report test results
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
checks: write
14+
pull-requests: write
15+
steps:
16+
- name: Check out repo
17+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
18+
- name: Download test results
19+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
20+
with:
21+
path: test_results
22+
pattern: test_results_*
23+
merge-multiple: true
24+
- name: List downloaded files
25+
run: ls -lR
26+
- name: Publish test results
27+
uses: EnricoMi/publish-unit-test-result-action@3a74b2957438d0b6e2e61d67b05318aa25c9e6c6 # v2.20.0
28+
with:
29+
files: "test_results/**/*.xml"
30+
if: always()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Run unit tests
2+
3+
on:
4+
workflow_call:
5+
workflow_dispatch:
6+
7+
jobs:
8+
run_unit_tests:
9+
name: Run unit tests
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
matrix:
13+
os: [windows-latest, ubuntu-latest]
14+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
15+
# Fail-fast skews the pass/fail ratio and seems to make pytest produce
16+
# incomplete JUnit XML results.
17+
fail-fast: false
18+
steps:
19+
- name: Check out repo
20+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
21+
with:
22+
submodules: true
23+
- name: Set up Python
24+
uses: ni/python-actions/setup-python@49cd7a9a4098c459b90ba97e8e56bf5cd80997bb # v0.4.0
25+
id: setup-python
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
- name: Set up Poetry
29+
uses: ni/python-actions/setup-poetry@49cd7a9a4098c459b90ba97e8e56bf5cd80997bb # v0.4.0
30+
- name: Run and upload unit tests - ni.protobuf.types
31+
uses: ./.github/actions/run_and_upload_unit_tests
32+
with:
33+
package-name: ni.protobuf.types
34+
- name: Run and upload unit tests - ni.pythonpanel.v1.proto
35+
uses: ./.github/actions/run_and_upload_unit_tests
36+
with:
37+
package-name: ni.pythonpanel.v1.proto
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Unit tests for the package."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Unit tests for the package."""

0 commit comments

Comments
 (0)