Skip to content

Commit

Permalink
ci: initial implementation of top-level pytest tests (#433)
Browse files Browse the repository at this point in the history
```
$ poetry init
$ poetry add --dev pytest
$ poetry add --dev pytest-subtests
```
  • Loading branch information
jiridanek authored Jul 17, 2024
1 parent 821a004 commit fecd10c
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ jobs:
exit 1
fi
pytest-tests:
runs-on: ubuntu-latest
env:
poetry_version: '1.8.3'
steps:
- uses: actions/checkout@v4

- name: Cache poetry in ~/.local
uses: actions/cache/restore@v4
id: cache-poetry-restore
with:
path: ~/.local
key: "${{ runner.os }}-local-${{ env.poetry_version }}"

- name: Install poetry
if: steps.cache-poetry-restore.outputs.cache-hit != 'true'
run: pip install poetry==${{ env.poetry_version }}

- name: Save cache
if: steps.cache-poetry-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.local
key: ${{ steps.cache-poetry-restore.outputs.cache-primary-key }}

- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'poetry'

- name: Configure poetry
run: poetry env use "${{ steps.setup-python.outputs.python-path }}"

- name: Install deps
run: poetry install --sync

- run: poetry run pytest

code-static-analysis:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ podman run -it -p 8888:8888 quay.io/opendatahub/workbench-images:jupyter-mini

### Deploy & Test

#### Running Python selftests in Pytest

```shell
pip install poetry
poetry env use /usr/bin/python3.12
poetry config virtualenvs.in-project true
poetry install --sync

poetry run pytest
```

#### Notebooks

Deploy the notebook images in your Kubernetes environment using:
Expand Down
108 changes: 108 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "notebooks"
version = "2024.1"
authors = []
description = "Open Data Hub / OpenShift AI Notebook / Workbench images, and tests for the same in Python."
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "~3.12"


[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"
pytest-subtests = "^0.12.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
17 changes: 17 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://docs.pytest.org/en/7.1.x/reference/reference.html#configuration-options

[pytest]
addopts = --strict-markers --capture=no --tb=short

required_plugins = pytest-subtests

junit_logging = all
junit_log_passing_tests = False

log_cli = True
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = INFO

log_file = logs/pytest-logs.txt
log_file_level = DEBUG
Empty file added tests/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import pathlib
import tomllib
from typing import TYPE_CHECKING

if TYPE_CHECKING:
import pytest_subtests

PROJECT_ROOT = pathlib.Path(__file__).parent.parent


def test_image_pipfiles(subtests: pytest_subtests.plugin.SubTests):
for file in PROJECT_ROOT.glob("**/Pipfile"):
with subtests.test(msg="checking Pipfile", pipfile=file):
directory = file.parent # "ubi9-python-3.9"
ubi, lang, python = directory.name.split("-")

with open(file, "rb") as fp:
pipfile = tomllib.load(fp)
assert "requires" in pipfile, "Pipfile is missing a [[requires]] section"
assert pipfile["requires"]["python_version"] == python, "Pipfile does not declare the expected Python version"

0 comments on commit fecd10c

Please sign in to comment.