Skip to content

Commit 5c4755d

Browse files
committed
✏️ Extract the step runs something before really running test as a reusable workflow parameter.
1 parent 0fa4ac2 commit 5c4755d

File tree

2 files changed

+84
-38
lines changed

2 files changed

+84
-38
lines changed

.github/workflows/rw_uv_run_test.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# * test_type: The testing type. In generally, it only has 2 options: 'unit-test' and 'integration-test'.
99
# * all_test_items_paths: The target paths of test items under test.
1010
# * install_dependency_with_group: Install the dependency by UV configuration with dependency group setting. This parameter receive the dependency group naming.
11+
# * pre_test_script: Shell script or bash command to run before executing tests (optional).
1112
# * python-versions: JSON array of Python versions to test against. Default: '["3.13"]'
1213
# * operating-systems: JSON array of operating systems to test on. Default: '["ubuntu-latest", "ubuntu-22.04", "macos-latest", "macos-14"]'
1314
#
@@ -54,6 +55,11 @@ on:
5455
type: string
5556
required: false
5657
default: ''
58+
pre_test_script:
59+
description: "Shell script or bash command to run before executing tests (e.g., 'curl -s https://slack.com/api/auth.test -H \"Authorization: Bearer $SLACK_BOT_TOKEN\"')."
60+
type: string
61+
required: false
62+
default: ''
5763
max-parallel:
5864
description: "Set the max-parallel jobs."
5965
type: number
@@ -108,9 +114,11 @@ jobs:
108114
run: |
109115
uv pip install --group=${{ inputs.install_dependency_with_group }}
110116
111-
- name: Verify the Slack bot token
117+
- name: Run pre-test script
118+
if: ${{ inputs.pre_test_script != '' }}
119+
working-directory: ${{ inputs.test_working_directory }}
112120
run: |
113-
curl -s https://slack.com/api/auth.test -H "Authorization: Bearer $SLACK_BOT_TOKEN"
121+
${{ inputs.pre_test_script }}
114122
115123
- name: Run the specific tests with pytest
116124
if: ${{ inputs.test_type == '' }}

docs/contents/document/workflows/test/rw_uv_run_test.mdx

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,49 @@ graph TD
3333
E --> F{Dependency Group?}
3434
F -->|No Group| G[Install All Dependencies]
3535
F -->|With Group| H[Install Group Dependencies]
36-
G --> I[Run Tests with PyTest]
36+
G --> I{Pre-Test Script?}
3737
H --> I
38-
I --> J[Generate Coverage Report]
39-
J --> K[Upload Artifacts]
38+
I -->|Yes| J[Run Pre-Test Script]
39+
I -->|No| K[Run Tests with PyTest]
40+
J --> K
41+
K --> L[Generate Coverage Report]
42+
L --> M[Upload Artifacts]
4043
```
4144

4245
## Inputs
4346

4447
### Required Inputs
4548

46-
| Input | Type | Description |
47-
|-------|------|-------------|
49+
| Input | Type | Description |
50+
|-------------|--------|--------------------------------------------------------------|
4851
| `test_type` | string | Type of tests to run (e.g., 'unit-test', 'integration-test') |
4952

5053
### Optional Inputs
5154

52-
| Input | Type | Default | Description |
53-
|-------|------|---------|-------------|
54-
| `test_working_directory` | string | `'./'` | Working directory for test execution |
55-
| `test_folder` | string | `'./test'` | Folder path for test code |
56-
| `all_test_items_paths` | string | `'["./test"]'` | JSON array of test paths to execute |
57-
| `install_dependency_with_group` | string | `''` | UV dependency group to install (e.g., 'test', 'dev') |
58-
| `with-environment-variables` | string | `''` | Additional environment variables to set |
59-
| `max-parallel` | number | `0` | Maximum parallel jobs (0 = unlimited) |
60-
| `python-versions` | string | `'["3.13"]'` | JSON array of Python versions to test |
61-
| `operating-systems` | string | `'["ubuntu-latest", "ubuntu-22.04", "macos-latest", "macos-14"]'` | JSON array of OS to test on |
55+
| Input | Type | Default | Description |
56+
|---------------------------------|--------|-------------------------------------------------------------------|------------------------------------------------------|
57+
| `test_working_directory` | string | `'./'` | Working directory for test execution |
58+
| `test_folder` | string | `'./test'` | Folder path for test code |
59+
| `all_test_items_paths` | string | `'["./test"]'` | JSON array of test paths to execute |
60+
| `install_dependency_with_group` | string | `''` | UV dependency group to install (e.g., 'test', 'dev') |
61+
| `with-environment-variables` | string | `''` | Additional environment variables to set |
62+
| `pre_test_script` | string | `''` | Shell script or bash command to run before tests |
63+
| `max-parallel` | number | `0` | Maximum parallel jobs (0 = unlimited) |
64+
| `python-versions` | string | `'["3.13"]'` | JSON array of Python versions to test |
65+
| `operating-systems` | string | `'["ubuntu-latest", "ubuntu-22.04", "macos-latest", "macos-14"]'` | JSON array of OS to test on |
6266

6367
### Secrets
6468

65-
| Secret | Required | Description |
66-
|--------|----------|-------------|
67-
| `e2e_test_api_token` | No | API token for end-to-end tests if needed |
69+
| Secret | Required | Description |
70+
|----------------------|----------|------------------------------------------|
71+
| `e2e_test_api_token` | No | API token for end-to-end tests if needed |
6872

6973
## Outputs
7074

7175
This workflow uploads coverage reports as artifacts:
7276

73-
| Artifact Name | Description |
74-
|---------------|-------------|
77+
| Artifact Name | Description |
78+
|----------------------------------------------|----------------------------------|
7579
| `coverage_<test_type>_<os>_<python_version>` | Coverage report file (.coverage) |
7680

7781
## Usage Examples
@@ -129,6 +133,20 @@ jobs:
129133
all_test_items_paths: '["./test/"]'
130134
```
131135
136+
### With Pre-Test Script
137+
138+
```yaml
139+
jobs:
140+
test:
141+
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_uv_run_test.yaml@master
142+
with:
143+
test_type: unit-test
144+
pre_test_script: |
145+
echo "Running pre-test validation..."
146+
curl -s https://slack.com/api/auth.test -H "Authorization: Bearer $SLACK_BOT_TOKEN"
147+
all_test_items_paths: '["./test/unit_test/"]'
148+
```
149+
132150
### End-to-End Testing with Secrets
133151
134152
```yaml
@@ -141,6 +159,7 @@ jobs:
141159
test_type: e2e-test
142160
all_test_items_paths: '["./test/e2e/"]'
143161
python-versions: '["3.13"]'
162+
pre_test_script: 'echo "Starting E2E tests..." && ./scripts/setup-test-env.sh'
144163
```
145164
146165
### Complete CI Pipeline
@@ -202,15 +221,34 @@ uv sync --locked --all-extras --dev
202221
uv pip install --group=<group-name>
203222
```
204223

205-
### Step 4: Test Execution
224+
### Step 4: Pre-Test Script (Optional)
225+
226+
If `pre_test_script` is provided, runs custom commands before tests:
227+
228+
```bash
229+
# Example: Verify API connectivity
230+
curl -s https://slack.com/api/auth.test -H "Authorization: Bearer $SLACK_BOT_TOKEN"
231+
232+
# Example: Setup test environment
233+
./scripts/setup-test-env.sh
234+
```
235+
236+
**Common use cases:**
237+
- Verify external service connectivity
238+
- Setup test databases or mock servers
239+
- Validate environment variables
240+
- Run database migrations
241+
- Initialize test fixtures
242+
243+
### Step 5: Test Execution
206244

207245
Runs tests using UV's pytest integration:
208246

209247
```bash
210248
E2E_TEST_API_TOKEN=${{ secrets.e2e_test_api_token }} uv run pytest <test-path>
211249
```
212250

213-
### Step 5: Coverage Collection
251+
### Step 6: Coverage Collection
214252

215253
Renames and uploads coverage reports:
216254

@@ -343,11 +381,11 @@ with:
343381
344382
### Speed Comparison
345383
346-
| Tool | Dependency Installation | Virtual Environment |
347-
|------|------------------------|---------------------|
348-
| **UV** | ~1-2 seconds | ~0.5 seconds |
349-
| Poetry | ~30-60 seconds | ~5-10 seconds |
350-
| pip | ~20-40 seconds | ~3-5 seconds |
384+
| Tool | Dependency Installation | Virtual Environment |
385+
|--------|-------------------------|---------------------|
386+
| **UV** | ~1-2 seconds | ~0.5 seconds |
387+
| Poetry | ~30-60 seconds | ~5-10 seconds |
388+
| pip | ~20-40 seconds | ~3-5 seconds |
351389
352390
### Key Benefits
353391
@@ -431,15 +469,15 @@ with:
431469

432470
## Comparison with Other Test Workflows
433471

434-
| Feature | rw_uv_run_test | rw_run_test | rw_poetry_run_test |
435-
|---------|----------------|-------------|-------------------|
436-
| Dependency Manager | UV | pip/uv | Poetry |
437-
| Speed | ⚡ Fastest | Fast | Moderate |
438-
| Python Versions | 3.11+ | 3.8+ | 3.8+ |
439-
| Lock File | uv.lock | requirements.txt | poetry.lock |
440-
| Virtual Env | UV managed | Actions managed | Poetry managed |
441-
| Matrix Testing | ✅ Built-in | ✅ Built-in | ✅ Built-in |
442-
| Best For | Modern projects | Simple projects | Poetry projects |
472+
| Feature | rw_uv_run_test | rw_run_test | rw_poetry_run_test |
473+
|--------------------|-----------------|------------------|--------------------|
474+
| Dependency Manager | UV | pip/uv | Poetry |
475+
| Speed | ⚡ Fastest | Fast | Moderate |
476+
| Python Versions | 3.11+ | 3.8+ | 3.8+ |
477+
| Lock File | uv.lock | requirements.txt | poetry.lock |
478+
| Virtual Env | UV managed | Actions managed | Poetry managed |
479+
| Matrix Testing | ✅ Built-in | ✅ Built-in | ✅ Built-in |
480+
| Best For | Modern projects | Simple projects | Poetry projects |
443481

444482
## Migration Guide
445483

0 commit comments

Comments
 (0)