Skip to content

Commit 75b0f73

Browse files
committed
Setup pre-commit and github actions to run tests
1 parent f529055 commit 75b0f73

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
3+
on:
4+
# Triggers the workflow on pull request events but only for the "main" branch
5+
pull_request:
6+
branches: ["main"]
7+
8+
# Allows you to run this workflow manually from the Actions tab
9+
workflow_dispatch:
10+
11+
jobs:
12+
python-style-checks:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Install requirements
18+
run: pip install -r requirements.txt
19+
20+
- name: Run pre-commit
21+
run: pre-commit run --all-files
22+
23+
tests:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- name: Install requirements
29+
run: pip install -r requirements.txt
30+
31+
- name: Run tests
32+
run: pytest --cov --cov-fail-under=90 # Fail tests if coverage is below 90%

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.6.0
4+
hooks:
5+
- id: check-added-large-files
6+
- id: check-json
7+
- id: check-yaml
8+
- id: detect-private-key
9+
- id: end-of-file-fixer
10+
- id: name-tests-test
11+
- id: requirements-txt-fixer
12+
- id: trailing-whitespace
13+
- repo: https://github.com/asottile/pyupgrade
14+
rev: v3.17.0
15+
hooks:
16+
- id: pyupgrade
17+
- repo: https://github.com/psf/black-pre-commit-mirror
18+
rev: 24.8.0
19+
hooks:
20+
- id: black-jupyter
21+
- repo: https://github.com/pycqa/isort
22+
rev: 5.13.2
23+
hooks:
24+
- id: isort

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pre-commit==3.8.0
2+
pytest==8.3.3
3+
pytest-cov==5.0.0

src/example.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Module docstring."""
2+
3+
from typing import Any
4+
5+
6+
class Example:
7+
"""Example class docstring."""
8+
9+
def __init__(self, variable: Any = None) -> None:
10+
self.variable = variable
11+
12+
def function(self, *args: Any, **kwargs: Any) -> int:
13+
"""Function docstring.
14+
15+
Returns:
16+
int: Number of arguments passed
17+
"""
18+
return len(args) + len(kwargs)

src/example_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Tests for example module."""
2+
3+
from typing import Any
4+
5+
import pytest
6+
7+
from src import example
8+
9+
10+
class TestExample:
11+
"""Testcases for Example class"""
12+
13+
def test_init(self) -> None:
14+
"""Test init."""
15+
16+
obj = example.Example("test_variable")
17+
assert obj.variable == "test_variable"
18+
19+
@pytest.mark.parametrize(
20+
"args,kwargs,expected_num_args",
21+
[
22+
((1, 2, "A"), {}, 3),
23+
((), {"A": "A"}, 1),
24+
(("A", "B"), {"a": "a"}, 3),
25+
(("A", "B"), {}, 2),
26+
],
27+
)
28+
def test_function(self, args: Any, kwargs: Any, expected_num_args: int) -> None:
29+
"""Test function."""
30+
31+
obj = example.Example()
32+
num_args = obj.function(*args, **kwargs)
33+
assert num_args == expected_num_args

0 commit comments

Comments
 (0)