Skip to content

Commit

Permalink
Merge pull request #120 from timtroendle/feature-test-infrastructure-…
Browse files Browse the repository at this point in the history
…scripts

Add infrastructure to test scripts
  • Loading branch information
timtroendle committed Jul 16, 2021
2 parents 189e9fd + de7bc94 commit 3a0d2c3
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 19 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/pythonpackage.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests of eurocalliopelib
name: Tests of eurocalliopelib and scripts

on: [push, pull_request]

Expand All @@ -16,8 +16,8 @@ jobs:
with:
auto-update-conda: true
python-version: 3.8
activate-environment: test-eurocalliopelib
environment-file: lib/test-requirements.yaml
activate-environment: test-eurocalliope
environment-file: test-requirements.yaml
- name: Test with pytest
run: |
pytest ./lib
pytest
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ As a developer, you may want to run the entire workflow often to spot errors ear

Make sure to run this in a clean working directory. Do not use the working directory in which you are using your normal configuration.

## Run tests of library code and scripts

1. Create a test environment using conda:

$ conda env create -f test-requirements.yaml
$ conda activate test-eurocalliope

2. Run the test suite with py.test:

$ py.test

## License

euro-calliope is developed and maintained within the [Calliope project](https://www.callio.pe). The code in this repository is MIT licensed.
9 changes: 5 additions & 4 deletions Snakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glob
from pathlib import Path

from snakemake.utils import validate

Expand Down Expand Up @@ -35,6 +36,7 @@ __version__ = open(f"{root_dir}VERSION").readlines()[0].strip()
script_dir = f"{root_dir}scripts/"
template_dir = f"{root_dir}templates/"
test_dir = f"{root_dir}tests/"
model_test_dir = f"{test_dir}model"

onstart:
shell("mkdir -p build/logs")
Expand Down Expand Up @@ -349,10 +351,9 @@ rule docs:
rule test:
message: "Run tests"
input:
test_dir + "test_runner.py",
test_dir + "test_model.py",
test_dir + "test_capacityfactors.py",
"build/logs/{resolution}/model.done",
test_dir = model_test_dir,
tests = lambda wildcards: Path(model_test_dir).glob("**/test_*.py"),
model = test_dir + "resources/{resolution}/model.yaml",
example_model = "build/model/{resolution}/example-model.yaml",
capacity_factor_timeseries = expand(
Expand All @@ -363,4 +364,4 @@ rule test:
config = config
output: "build/logs/{resolution}/test-report.html"
conda: "./envs/test.yaml"
script: "./tests/test_runner.py"
script: "./tests/model/test_runner.py"
6 changes: 4 additions & 2 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ The library code contains general-purpose functions and routines that we expect

## Developer Guide

The following assumes you are in the root folder of this repository, i.e. the parent folder of the folder this file is in.

### Installation

Best install `eurocalliopelib` from the conda environment:

$ conda env create -f test-requirements.yaml
$ conda activate test-eurocalliopelib
$ conda activate test-eurocalliope

### Run the test suite

Run the test suite with py.test:

$ py.test
$ py.test tests/lib
1 change: 0 additions & 1 deletion lib/pytest.ini

This file was deleted.

2 changes: 1 addition & 1 deletion lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
description='Library code of the euro-calliope workflow.',
maintainer='calliope-project',
maintainer_email='tim.troendle@usys.ethz.ch',
packages=find_packages(exclude=['tests*']),
packages=find_packages(),
include_package_data=True,
install_requires=[
"numpy",
Expand Down
12 changes: 8 additions & 4 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[pytest]
filterwarnings=
ignore::calliope.exceptions.ModelWarning:calliope
ignore:.*inspect.getargspec\(\) is deprecated.*:DeprecationWarning:pyomo
norecursedirs = .git lib .snakemake
norecursedirs =
.git
.snakemake
tests/model # by default, don't execute tests requiring build results
testpaths =
tests/lib
tests/scripts
python_paths = scripts
5 changes: 3 additions & 2 deletions lib/test-requirements.yaml → test-requirements.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test-eurocalliopelib
name: test-eurocalliope
channels:
- conda-forge
dependencies:
Expand All @@ -20,4 +20,5 @@ dependencies:
- pytest-html=1.20.0
- pip=21.0.1
- pip:
- -e .[geo]
- pytest-pythonpath
- -e ./lib[geo]
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/model/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
filterwarnings=
ignore::calliope.exceptions.ModelWarning:calliope
ignore:.*inspect.getargspec\(\) is deprecated.*:DeprecationWarning:pyomo
norecursedirs = .git lib .snakemake
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion tests/test_runner.py → tests/model/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import pandas as pd


def run_test(path_to_output, path_to_model, path_to_example_model, paths_to_cf_timeseries, config):
def run_test(path_to_test_dir, path_to_output, path_to_model, path_to_example_model, paths_to_cf_timeseries, config):
exit_code = pytest.main(
[
path_to_test_dir,
f"--html={path_to_output}",
f"--self-contained-html",
],
Expand Down Expand Up @@ -85,6 +86,7 @@ def _read_locs(path_to_cf_timeseries):

if __name__ == "__main__":
run_test(
path_to_test_dir=snakemake.input.test_dir,
path_to_model=snakemake.input.model,
path_to_example_model=snakemake.input.example_model,
paths_to_cf_timeseries=snakemake.input.capacity_factor_timeseries,
Expand Down
6 changes: 6 additions & 0 deletions tests/scripts/test_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is a proof of concept. It should be removed once other tests are added.
from scripts.biofuels import utils # This proofs script imports are possible.


def test_country_code_conversion():
assert utils.eu_country_code_to_iso3("uk") == "GBR"

0 comments on commit 3a0d2c3

Please sign in to comment.