Skip to content

Commit dd5b6a4

Browse files
authored
Merge pull request #29 from DEMENT-Model/iss11-test-infrastructure
Test infrastructure
2 parents d9b6d18 + ffd6071 commit dd5b6a4

File tree

7 files changed

+132
-1
lines changed

7 files changed

+132
-1
lines changed

.github/workflows/testing.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
7+
# Triggers the workflow on pushes to open pull requests with code changes
8+
pull_request:
9+
branches: [ main ]
10+
paths:
11+
- '**.py'
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
python-version: ['3.10', '3.11', '3.12']
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Set up Python ${{ matrix.python-version }}
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install uv
33+
uv sync --dev
34+
35+
- name: Run tests
36+
run: uv run pytest

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# vscode workspace
22
.vscode/
33

4+
# Python module bytecode
5+
__pycache__
46

57
# Jupyter Notebook
68
*/.ipynb_checkpoints
@@ -16,4 +18,4 @@ output/
1618
documentation/DEMENTpy_Documentation.*
1719

1820
# DEMENTpy_notebooks
19-
DEMENTpy_notebooks/
21+
DEMENTpy_notebooks/

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ dependencies = [
1313
dev = [
1414
"pre-commit>=4.2.0",
1515
"ruff>=0.11.4",
16+
"pytest>=7.0",
1617
]
18+
19+
[tool.pytest.ini_options]
20+
testpaths = ["tests"]
21+
python_files = "test_*.py"
22+
addopts = "-v"

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
import os
3+
4+
# Add the src/ directory to the pythonpath
5+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))

tests/test_grid.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Tests for the Grid class."""
2+
3+
import pandas as pd
4+
import pytest
5+
6+
from initialization import initialize_data
7+
from grid import Grid
8+
9+
10+
@pytest.fixture
11+
def grid():
12+
"""Initialize a Grid object using the initialize_data function."""
13+
input_dir = 'grassland'
14+
runtime = pd.read_csv(input_dir+'/runtime.txt', header=None, index_col=0, sep='\t')
15+
data = initialize_data(runtime, input_dir)
16+
return Grid(runtime, data)
17+
18+
19+
def test_initialization_runs(grid):
20+
"""Test that the initialization of Grid works without an error."""
21+
pass
22+
23+
24+
def test_degradation_runs(grid):
25+
"""Test that Grid.degradation works without an error."""
26+
grid.degradation(0)
27+
28+
29+
def test_uptake_runs(grid):
30+
"""Test that Grid.uptake works without an error."""
31+
grid.degradation(0) # degradation needs to run to initialize some DataFrames
32+
grid.uptake(0)
33+
34+
35+
def test_metabolism_runs(grid):
36+
"""Test that Grid.metabolism works without an error."""
37+
grid.metabolism(0)
38+
39+
40+
def test_mortality_runs(grid):
41+
"""Test that Grid.mortality works without an error."""
42+
grid.mortality(0)
43+
44+
45+
def test_reproduction_runs(grid):
46+
"""Test that Grid.reproduction works without an error."""
47+
grid.reproduction(0)

tests/test_initialization.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Tests for the initialization of the data."""
2+
3+
import pandas as pd
4+
import pytest
5+
6+
from initialization import initialize_data
7+
8+
9+
def test_initialize_data():
10+
"""Test that initialize_data works without an error."""
11+
input_dir = 'grassland'
12+
runtime = pd.read_csv(input_dir+'/runtime.txt', header=None, index_col=0, sep='\t')
13+
data = initialize_data(runtime, input_dir)
14+
assert isinstance(data, dict)

tests/test_placeholder.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Tests to show how to use pytest."""
2+
3+
import numpy as np
4+
import pytest
5+
6+
7+
def test_basic():
8+
"""Illustrates how to write a simple test."""
9+
assert 1 + 1 == 2
10+
11+
12+
def test_raises_exception():
13+
"""Illustrates how to write a test that checks for an exception."""
14+
with pytest.raises(ZeroDivisionError):
15+
1 / 0
16+
17+
18+
@pytest.mark.parametrize("x,y,r", [(3, 4, 5), (-5, -12, 13)])
19+
def test_multiple_parameters(x, y, r):
20+
"""Illustrates how to write a test to run with multiple input arguments."""
21+
assert np.sqrt(x**2 + y**2) == r

0 commit comments

Comments
 (0)