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