From 79bf48444dfd62515b321a32df18ae21eeaf7b54 Mon Sep 17 00:00:00 2001 From: turnerm Date: Fri, 13 Oct 2023 17:18:48 +0200 Subject: [PATCH] HAPI-225 add testing infrastructure --- .config/coveragerc | 17 +++++++++ .config/pytest.ini | 4 +++ .github/workflows/run-python-tests.yaml | 48 +++++++++++++++++++++++++ pyproject.toml | 30 ++++++++++++++++ requirements.txt | 35 ++++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 .config/coveragerc create mode 100644 .config/pytest.ini create mode 100644 .github/workflows/run-python-tests.yaml diff --git a/.config/coveragerc b/.config/coveragerc new file mode 100644 index 0000000..ca9d302 --- /dev/null +++ b/.config/coveragerc @@ -0,0 +1,17 @@ +[run] +source = src + +omit = */_version.py + +[report] +exclude_also = + from ._version + def __repr__ + if self.debug: + if settings.DEBUG + raise AssertionError + raise NotImplementedError + if 0: + if __name__ == .__main__.: + if TYPE_CHECKING: + @(abc\.)?abstractmethod diff --git a/.config/pytest.ini b/.config/pytest.ini new file mode 100644 index 0000000..bb28fd3 --- /dev/null +++ b/.config/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +pythonpath = ../src +addopts = "--color=yes" +log_cli = 1 diff --git a/.github/workflows/run-python-tests.yaml b/.github/workflows/run-python-tests.yaml new file mode 100644 index 0000000..7f67dab --- /dev/null +++ b/.github/workflows/run-python-tests.yaml @@ -0,0 +1,48 @@ +# This workflow will install Python dependencies, lint and run tests +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Run tests + +on: + workflow_dispatch: # add run button in github + push: + branches-ignore: + - gh-pages + - 'dependabot/**' + pull_request: + branches-ignore: + - gh-pages + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.11' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install --upgrade hatch + - name: Test with hatch/pytest + run: | + hatch run test:test + - name: Check styling + if: always() + run: | + hatch run lint:style + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + junit_files: test-results.xml + - name: Publish in Coveralls + uses: coverallsapp/github-action@v2 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + flag-name: tests + format: lcov diff --git a/pyproject.toml b/pyproject.toml index 8b4698d..8c7692f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,8 @@ Homepage = "https://github.com/OCHA-DAP/hapi-schemas" [project.optional-dependencies] database = ["hdx-python-database>=1.2.8"] +dev = ["pre-commit"] +test = ["pytest", "pytest-cov"] ######### # Hatch # @@ -64,3 +66,31 @@ source = "vcs" [tool.hatch.version.raw-options] local_scheme = "no-local-version" version_scheme = "python-simplified-semver" + +# Tests + +[tool.hatch.envs.test] +features = ["test"] + +[tool.hatch.envs.test.scripts] +test = """ + pytest -c .config/pytest.ini --rootdir=. --junitxml=test-results.xml \ + --cov --cov-config=.config/coveragerc --no-cov-on-fail \ + --cov-report=lcov --cov-report=term-missing + """ + +[[tool.hatch.envs.test.matrix]] +python = ["3.11"] + +[tool.hatch.envs.lint] +detached = true +dependencies = [ + "black", + "ruff", +] + +[tool.hatch.envs.lint.scripts] +style = [ + "ruff --config .config/ruff.toml {args:.}", + "black --config .config/black.toml --check --diff {args:.}", +] diff --git a/requirements.txt b/requirements.txt index e5c4fdb..ebb2659 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,13 +4,48 @@ # # pip-compile --all-extras --output-file=requirements.txt pyproject.toml # +cfgv==3.4.0 + # via pre-commit +coverage[toml]==7.3.2 + # via pytest-cov +distlib==0.3.7 + # via virtualenv +filelock==3.12.4 + # via virtualenv greenlet==3.0.0 # via sqlalchemy hdx-python-database==1.2.8 # via hapi-schema (pyproject.toml) +identify==2.5.30 + # via pre-commit +iniconfig==2.0.0 + # via pytest +nodeenv==1.8.0 + # via pre-commit +packaging==23.2 + # via pytest +platformdirs==3.11.0 + # via virtualenv +pluggy==1.3.0 + # via pytest +pre-commit==3.4.0 + # via hapi-schema (pyproject.toml) +pytest==7.4.2 + # via + # hapi-schema (pyproject.toml) + # pytest-cov +pytest-cov==4.1.0 + # via hapi-schema (pyproject.toml) +pyyaml==6.0.1 + # via pre-commit sqlalchemy==2.0.22 # via # hapi-schema (pyproject.toml) # hdx-python-database typing-extensions==4.8.0 # via sqlalchemy +virtualenv==20.24.5 + # via pre-commit + +# The following packages are considered to be unsafe in a requirements file: +# setuptools