Skip to content

Commit 0fa4ac2

Browse files
committed
βž• Add a new reusable workflow about running test via *uv*.
1 parent 627e6c7 commit 0fa4ac2

File tree

4 files changed

+662
-0
lines changed

4 files changed

+662
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#################################################################################################################################
2+
#
3+
# Workflow Description:
4+
# Use UV to run testing by specific type with all test items via PyTest and generate its testing
5+
# coverage report (it would save reports by 'actions/upload-artifact').
6+
#
7+
# Workflow input parameters:
8+
# * test_type: The testing type. In generally, it only has 2 options: 'unit-test' and 'integration-test'.
9+
# * all_test_items_paths: The target paths of test items under test.
10+
# * install_dependency_with_group: Install the dependency by UV configuration with dependency group setting. This parameter receive the dependency group naming.
11+
# * python-versions: JSON array of Python versions to test against. Default: '["3.13"]'
12+
# * operating-systems: JSON array of operating systems to test on. Default: '["ubuntu-latest", "ubuntu-22.04", "macos-latest", "macos-14"]'
13+
#
14+
# Workflow running output:
15+
# No, but it would save the testing coverage reports to provide after-process to organize and record.
16+
#
17+
# * Upload-Artifact:
18+
# * coverage: The test coverage report which be generated by PyTest, and it's recorded after run test done.
19+
# The file name format would be .coverage.<test type>.<runtime os>-<python-version>
20+
#
21+
#################################################################################################################################
22+
23+
name: Run test items via PyTest
24+
25+
on:
26+
workflow_call:
27+
inputs:
28+
test_type:
29+
description: "The testing type. In generally, it only has 2 options: 'unit-test' and 'integration-test'."
30+
required: true
31+
type: string
32+
test_working_directory:
33+
description: "The working directory for test running."
34+
required: false
35+
type: string
36+
default: './'
37+
test_folder:
38+
description: "The folder path for test code."
39+
required: false
40+
type: string
41+
default: './test'
42+
all_test_items_paths:
43+
description: "The target paths of test items under test."
44+
required: false
45+
type: string
46+
default: '["./test"]'
47+
install_dependency_with_group:
48+
description: "Install the dependency by UV configuration with dependency group setting. This parameter receive the dependency group naming. (multiple values allowed)"
49+
type: string
50+
required: false
51+
default: ''
52+
with-environment-variables:
53+
description: "Set the specific environment for the environment variables."
54+
type: string
55+
required: false
56+
default: ''
57+
max-parallel:
58+
description: "Set the max-parallel jobs."
59+
type: number
60+
required: false
61+
default: 0
62+
python-versions:
63+
description: "JSON array of Python versions to test against."
64+
type: string
65+
required: false
66+
default: '["3.13"]'
67+
operating-systems:
68+
description: "JSON array of operating systems to test on."
69+
type: string
70+
required: false
71+
default: '["ubuntu-latest", "ubuntu-22.04", "macos-latest", "macos-14"]'
72+
secrets:
73+
e2e_test_api_token:
74+
description: "Set the API token for end-to-end test if it needs."
75+
required: false
76+
77+
jobs:
78+
run_test_items:
79+
strategy:
80+
max-parallel: ${{ inputs.max-parallel }}
81+
matrix:
82+
python-version: ${{fromJson(inputs.python-versions)}}
83+
os: ${{fromJson(inputs.operating-systems)}}
84+
test-path: ${{fromJson(inputs.all_test_items_paths)}}
85+
fail-fast: false # Fix issue in GitHub Action: FailFast: cancelling since parallel instance has failed
86+
87+
runs-on: ${{ matrix.os }}
88+
steps:
89+
- uses: actions/checkout@v5
90+
91+
- name: Install uv
92+
uses: astral-sh/setup-uv@v7
93+
with:
94+
python-version: ${{ matrix.python-version }}
95+
96+
- name: Activate the uv virtual environment
97+
run: |
98+
uv venv
99+
. .venv/bin/activate
100+
101+
- name: Install all the project dependencies
102+
if: ${{ inputs.install_dependency_with_group == '' }}
103+
run: uv sync --locked --all-extras --dev
104+
105+
- name: Build Python runtime environment by UV with dependency group *${{ inputs.install_dependency_with_group }}*
106+
if: ${{ inputs.install_dependency_with_group != '' }}
107+
working-directory: ${{ inputs.test_working_directory }}
108+
run: |
109+
uv pip install --group=${{ inputs.install_dependency_with_group }}
110+
111+
- name: Verify the Slack bot token
112+
run: |
113+
curl -s https://slack.com/api/auth.test -H "Authorization: Bearer $SLACK_BOT_TOKEN"
114+
115+
- name: Run the specific tests with pytest
116+
if: ${{ inputs.test_type == '' }}
117+
working-directory: ${{ inputs.test_working_directory }}
118+
run: |
119+
# Export additional environment variables if provided
120+
if [ -n "${{ inputs.with-environment-variables }}" ]; then
121+
export ${{ inputs.with-environment-variables }}
122+
fi
123+
E2E_TEST_API_TOKEN=${{ secrets.e2e_test_api_token }} uv run pytest ${{ matrix.test-path }}
124+
continue-on-error: ${{ inputs.keep_run_if_test_fail }}
125+
env:
126+
E2E_TEST_API_TOKEN: ${{ secrets.e2e_test_api_token }}
127+
128+
- name: Run ${{ inputs.test_type }} tests with pytest
129+
if: ${{ inputs.test_type != '' }}
130+
working-directory: ${{ inputs.test_working_directory }}
131+
run: |
132+
# Export additional environment variables if provided
133+
if [ -n "${{ inputs.with-environment-variables }}" ]; then
134+
export ${{ inputs.with-environment-variables }}
135+
fi
136+
E2E_TEST_API_TOKEN=${{ secrets.e2e_test_api_token }} uv run pytest ${{ inputs.test_folder }}
137+
continue-on-error: ${{ inputs.keep_run_if_test_fail }}
138+
env:
139+
E2E_TEST_API_TOKEN: ${{ secrets.e2e_test_api_token }}
140+
141+
- name: Rename the code coverage result file
142+
working-directory: ${{ inputs.test_working_directory }}
143+
run: |
144+
mv ./.coverage ./.coverage.${{ inputs.test_type }}.${{ matrix.os }}-${{ matrix.python-version }}
145+
146+
- name: Upload code coverage result file
147+
uses: actions/upload-artifact@v4
148+
with:
149+
name: coverage_${{ inputs.test_type }}_${{ matrix.os }}_${{ matrix.python-version }}
150+
path: ${{ inputs.test_working_directory }}.coverage.${{ inputs.test_type }}.${{ matrix.os }}-${{ matrix.python-version }}
151+
if-no-files-found: error
152+
include-hidden-files: true

β€Ždocs/contents/document/sidebars.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const sidebars: SidebarsConfig = {
7878
id: 'workflows/test/rw_run_test',
7979
label: 'Run Test',
8080
},
81+
{
82+
type: 'doc',
83+
id: 'workflows/test/rw_uv_run_test',
84+
label: 'UV Run Test',
85+
},
8186
{
8287
type: 'doc',
8388
id: 'workflows/test/rw_poetry_run_test',

β€Ždocs/contents/document/workflows/index.mdxβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ All workflow source code is available in the [GitHub repository](https://github.
1919
[![View Source](https://img.shields.io/badge/View-Source-blue?logo=github)](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_get_tests.yaml)
2020
- **[rw_run_test](./test/rw_run_test.mdx)** - Run tests with single Python version<br/>
2121
[![View Source](https://img.shields.io/badge/View-Source-blue?logo=github)](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_run_test.yaml)
22+
- **[rw_uv_run_test](./test/rw_uv_run_test.mdx)** - Run tests using UV (ultra-fast Python package manager)<br/>
23+
[![View Source](https://img.shields.io/badge/View-Source-blue?logo=github)](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_uv_run_test.yaml)
2224
- **[rw_poetry_run_test](./test/rw_poetry_run_test.mdx)** - Run tests using Poetry<br/>
2325
[![View Source](https://img.shields.io/badge/View-Source-blue?logo=github)](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_poetry_run_test.yaml)
2426
- **[rw_run_test_with_multi_py_versions](./test/rw_run_test_with_multi_py_versions.mdx)** - Run tests with multiple Python versions<br/>
@@ -181,6 +183,7 @@ Below is a complete list of all available reusable workflows with direct links t
181183
|------------------------------------------------------|---------------|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
182184
| `rw_get_tests.yaml` | Testing | Discover test items | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_get_tests.yaml) |
183185
| `rw_run_test.yaml` | Testing | Run tests (single Python version) | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_run_test.yaml) |
186+
| `rw_uv_run_test.yaml` | Testing | Run tests with UV (ultra-fast) | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_uv_run_test.yaml) |
184187
| `rw_poetry_run_test.yaml` | Testing | Run tests with Poetry | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_poetry_run_test.yaml) |
185188
| `rw_run_test_with_multi_py_versions.yaml` | Testing | Run tests (multiple Python versions) | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_run_test_with_multi_py_versions.yaml) |
186189
| `rw_poetry_run_test_with_multi_py_versions.yaml` | Testing | Run tests with Poetry (multi-version) | [View](https://github.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/blob/master/.github/workflows/rw_poetry_run_test_with_multi_py_versions.yaml) |

0 commit comments

Comments
Β (0)