Optimize corpus tests: mock downloads, separate data validation, suppress CLI output #2127
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project | |
| # SPDX-License-Identifier: Apache-2.0 | |
| name: Unit test | |
| on: | |
| push: | |
| paths: | |
| - ".github/workflows/unittest.yml" | |
| - "bin/**" | |
| - "build_tools/**" | |
| - "pythainlp/**" | |
| - "tests/**" | |
| - "Makefile" | |
| - "MANIFEST.in" | |
| - "pyproject.toml" | |
| pull_request: | |
| branches: | |
| - dev | |
| paths: | |
| - ".github/workflows/unittest.yml" | |
| - "bin/**" | |
| - "build_tools/**" | |
| - "pythainlp/**" | |
| - "tests/**" | |
| - "Makefile" | |
| - "MANIFEST.in" | |
| - "pyproject.toml" | |
| # Avoid duplicate runs for the same source branch and repository. | |
| # For pull_request events, uses the source repo name from | |
| # github.event.pull_request.head.repo.full_name; otherwise uses github.repository. | |
| # For push events, uses the branch name from github.ref_name. | |
| # For pull_request events, uses the source branch name from github.head_ref. | |
| # This ensures events for the same repo and branch share the same group, | |
| # and avoids cross-fork collisions when branch names are reused. | |
| concurrency: | |
| group: >- | |
| ${{ github.workflow }}-${{ | |
| github.event.pull_request.head.repo.full_name || github.repository | |
| }}-${{ github.head_ref || github.ref_name }} | |
| cancel-in-progress: true | |
| jobs: | |
| unittest: | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| # To optimize CI/CD resource utilization and manage dependency overhead, | |
| # tests are categorized into four tiers based on their resource requirements | |
| # and dependency complexity: "core", "compact", "extra", and "noauto". | |
| # Test cases in "noauto" will not be included here. | |
| # See details of test matrix in: | |
| # https://github.com/PyThaiNLP/pythainlp/blob/dev/tests/README.md | |
| os: ["ubuntu-latest", "windows-latest", "macos-latest"] | |
| python-version: ["3.14", "3.9"] # Latest and earliest | |
| include: | |
| # Test the rest of Python versions only on Ubuntu | |
| - os: "ubuntu-latest" | |
| python-version: "3.13" | |
| - os: "ubuntu-latest" | |
| python-version: "3.12" | |
| - os: "ubuntu-latest" | |
| python-version: "3.11" | |
| - os: "ubuntu-latest" | |
| python-version: "3.10" | |
| runs-on: ${{ matrix.os }} | |
| env: | |
| PYICU_WIN_VER: 2.15 # 2.15 is the last version that supports Python 3.9 | |
| INSTALL_FULL_DEPS: false | |
| PYTHON_VERSION_LATEST: "3.14" | |
| PYTHON_VERSION_LATEST_2: "3.13" # Second-latest supported version | |
| PYTHON_VERSION_EARLIEST: "3.9" | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| cache: "pip" | |
| - name: Install build tools | |
| run: | | |
| pip install --upgrade "pip<24.1" "setuptools>=69.0.0,<=73.0.1" | |
| pip install coverage coveralls | |
| # pip<24.1 because https://github.com/omry/omegaconf/pull/1195 | |
| # setuptools>=65.0.2 because https://github.com/pypa/setuptools/commit/d03da04e024ad4289342077eef6de40013630a44#diff-9ea6e1e3dde6d4a7e08c7c88eceed69ca745d0d2c779f8f85219b22266efff7fR1 | |
| # setuptools<=73.0.1 because https://github.com/pypa/setuptools/issues/4620 | |
| - name: Install ICU (macOS) | |
| if: startsWith(matrix.os, 'macos-') | |
| run: | | |
| brew install icu4c | |
| PKG_CONFIG_PATH=$(brew --prefix)/opt/icu4c/lib/pkgconfig | |
| echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}" >> "${GITHUB_ENV}" | |
| ICU_VER=$(pkg-config --modversion icu-i18n) | |
| echo "ICU_VER=${ICU_VER}" | |
| echo "ICU_VER=${ICU_VER}" >> "${GITHUB_ENV}" | |
| - name: Install PyICU (Windows) | |
| if: startsWith(matrix.os, 'windows-') && (matrix.python-version == env.PYTHON_VERSION_LATEST_2 || matrix.python-version == env.PYTHON_VERSION_EARLIEST) | |
| shell: powershell | |
| # Get the wheel URL from https://github.com/cgohlke/pyicu-build/releases | |
| run: | | |
| $PYTHON_WIN_VER = "${{ matrix.python-version }}" | |
| $CP_VER = "cp" + $PYTHON_WIN_VER.Replace(".", "") | |
| $WHEEL_URL = "https://github.com/cgohlke/pyicu-build/releases/download/v${{ env.PYICU_WIN_VER }}/PyICU-${{ env.PYICU_WIN_VER }}-${CP_VER}-${CP_VER}-win_amd64.whl" | |
| pip install "$WHEEL_URL" | |
| - name: Install PyThaiNLP + full testing dependencies | |
| if: env.INSTALL_FULL_DEPS == 'true' | |
| env: | |
| SKLEARN_ALLOW_DEPRECATED_SKLEARN_PACKAGE_INSTALL: True | |
| run: pip install ".[full]" | |
| - name: Test (core) | |
| if: ${{ (matrix.os == 'ubuntu-latest' && matrix.python-version != env.PYTHON_VERSION_LATEST_2 && matrix.python-version != env.PYTHON_VERSION_EARLIEST) || (matrix.os != 'ubuntu-latest' && matrix.python-version == env.PYTHON_VERSION_LATEST) }} | |
| env: | |
| PYTHONIOENCODING: utf-8 | |
| run: | | |
| pip install . | |
| python -m unittest tests.core | |
| # Use 'unittest <test_module>' instead of 'unittest discover' to avoid | |
| # loading tests with dependencies more than expected. | |
| # Test cases loaded is defined in __init__.py in the tests directory. | |
| # See also tests/README.md | |
| - name: Test (compact + core) | |
| if: ${{ ((matrix.python-version == env.PYTHON_VERSION_LATEST_2) || (matrix.python-version == env.PYTHON_VERSION_EARLIEST)) && !(matrix.os == 'ubuntu-latest' && matrix.python-version == env.PYTHON_VERSION_LATEST_2) }} | |
| env: | |
| PYTHONIOENCODING: utf-8 | |
| run: | | |
| pip install ".[compact]" | |
| python -m unittest tests.core tests.compact | |
| - name: Test (extra + compact + core) | |
| if: matrix.os == 'ubuntu-latest' && matrix.python-version == env.PYTHON_VERSION_LATEST_2 | |
| env: | |
| PYTHONIOENCODING: utf-8 | |
| run: | | |
| pip install ".[compact,extra]" | |
| coverage run -m unittest tests.core tests.compact tests.extra | |
| # Only submit a report from the "extra" run, to get maximum coverage | |
| - name: Coverage report | |
| if: matrix.os == 'ubuntu-latest' && matrix.python-version == env.PYTHON_VERSION_LATEST_2 | |
| env: | |
| COVERALLS_SERVICE_NAME: github | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PYTHONIOENCODING: utf-8 | |
| run: coveralls |