Skip to content

Commit 0f904d2

Browse files
Use Pytest for running tests (#726)
* Add pytest and required plugins to dev requirements * Add settings to .coveragerc * Update Makefile to use pytest where possible * Update Jenkins scripts to use Makefile * Update Jenkinsfile to install new requirements NOTE: Revert this change once the Jenkins environment is updated * Remove test run scripts for unit, intregration, and install tests * Update installation instructions and reference them in CI guide * Update CHANGELOG.md * Update doc/guide/install.rst --------- Co-authored-by: emanuel-schmid <[email protected]> Co-authored-by: Emanuel Schmid <[email protected]>
1 parent 09a9ecd commit 0f904d2

File tree

10 files changed

+100
-115
lines changed

10 files changed

+100
-115
lines changed

.coveragerc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# .coveragerc to control coverage.py
2+
3+
[run]
4+
# Also report branch coverage
5+
branch = True
6+
# Set concurrency type for correct coverage of multi-processing code
7+
concurrency = multiprocessing
8+
9+
[paths]
10+
source = climada/
11+
12+
[report]
13+
# Regexes for lines to exclude from consideration
14+
exclude_also =
15+
# Main code is not run
16+
if __name__ == .__main__.:
17+
18+
# Abtract methods are not run
19+
@(abc\.)?abstractmethod
20+
21+
# Never fail when reporting
22+
ignore_errors = True
23+
24+
[html]
25+
directory = coverage

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Code freeze date: YYYY-MM-DD
99
### Dependency Updates
1010

1111
Added:
12+
- `pytest` [#726](https://github.com/CLIMADA-project/climada_python/pull/726)
13+
- `pytest-cov` [#726](https://github.com/CLIMADA-project/climada_python/pull/726)
14+
- `pytest-subtests` [#726](https://github.com/CLIMADA-project/climada_python/pull/726)
1215

1316
Changed:
1417

@@ -42,6 +45,7 @@ Removed:
4245
- `Exposures.affected_total_value` now takes a hazard intensity threshold as argument. Affected values are only those for which at least one event exceeds the threshold. (previously, all exposures points with an assigned centroid were considered affected). By default the centroids are reassigned. [#702](https://github.com/CLIMADA-project/climada_python/pull/702) [#730](https://github.com/CLIMADA-project/climada_python/pull/730)
4346
- Add option to pass region ID to `LitPop.from_shape` [#720](https://github.com/CLIMADA-project/climada_python/pull/720)
4447
- Slightly improved performance on `LitPop`-internal computations [#720](https://github.com/CLIMADA-project/climada_python/pull/720)
48+
- Use `pytest` for executing tests [#726](https://github.com/CLIMADA-project/climada_python/pull/726)
4549
- Users can opt-out of the climada specific logging definitions and freely configure logging to their will, by setting the config value `logging.managed` to `false`. [#724](https://github.com/CLIMADA-project/climada_python/pull/724)
4650
- Add option to read additional variables from IBTrACS when using `TCTracks.from_ibtracs_netcdf` [#728](https://github.com/CLIMADA-project/climada_python/pull/728)
4751

Makefile

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
# test, coverage and lint
33
###
44

5+
PYTEST_JUNIT_ARGS = --junitxml=tests_xml/tests.xml
6+
7+
PYTEST_COV_ARGS = \
8+
--cov --cov-config=.coveragerc --cov-report html --cov-report xml \
9+
--cov-report term:skip-covered
10+
11+
PYTEST_ARGS = $(PYTEST_JUNIT_ARGS) $(PYTEST_COV_ARGS)
12+
513
.PHONY : help
614
help: ## Use one of the following instructions:
715
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
@@ -12,13 +20,12 @@ lint : ## Static code analysis with Pylint
1220

1321
.PHONY : unit_test
1422
unit_test : ## Unit tests execution with coverage and xml reports
15-
python -m coverage run tests_runner.py unit
16-
python -m coverage xml -o coverage.xml
17-
python -m coverage html -d coverage
23+
pytest $(PYTEST_ARGS) --ignore=climada/test climada/
1824

1925
.PHONY : install_test
2026
install_test : ## Test installation was successful
21-
python tests_install.py report
27+
pytest $(PYTEST_JUNIT_ARGS) climada/engine/test/test_cost_benefit.py \
28+
climada/engine/test/test_impact.py
2229

2330
.PHONY : data_test
2431
data_test : ## Test data APIs
@@ -30,21 +37,14 @@ notebook_test : ## Test notebooks in doc/tutorial
3037

3138
.PHONY : integ_test
3239
integ_test : ## Integration tests execution with xml reports
33-
python -m coverage run --parallel-mode --concurrency=multiprocessing tests_runner.py integ
34-
python -m coverage combine
35-
python -m coverage xml -o coverage.xml
36-
python -m coverage html -d coverage
40+
pytest $(PYTEST_ARGS) climada/test/
3741

3842
.PHONY : test
3943
test : ## Unit and integration tests execution with coverage and xml reports
40-
python -m coverage run --parallel-mode --concurrency=multiprocessing tests_runner.py unit
41-
python -m coverage run --parallel-mode --concurrency=multiprocessing tests_runner.py integ
42-
python -m coverage combine
43-
python -m coverage xml -o coverage.xml
44-
python -m coverage html -d coverage
44+
pytest $(PYTEST_ARGS) climada/
4545

4646
.PHONY : ci-clean
4747
ci-clean :
4848
rm -rf tests_xml
49-
rm pylint.log
50-
49+
rm pylint.log coverage.xml
50+
rm -r coverage

doc/guide/Guide_Continuous_Integration_and_Testing.ipynb

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"cells": [
33
{
4+
"attachments": {},
45
"cell_type": "markdown",
56
"metadata": {},
67
"source": [
78
"# Testing and Continuous Integration"
89
]
910
},
1011
{
12+
"attachments": {},
1113
"cell_type": "markdown",
1214
"metadata": {},
1315
"source": [
@@ -20,31 +22,43 @@
2022
]
2123
},
2224
{
25+
"attachments": {},
2326
"cell_type": "markdown",
2427
"metadata": {},
2528
"source": [
2629
"## Testing CLIMADA\n",
2730
"\n",
31+
"Executing the entire test suite requires you to install the additional requirements for testing.\n",
32+
"See the [installation instructions](install.rst) for [developer dependencies](install-dev) for further information.\n",
33+
"\n",
34+
"\n",
2835
"### Installation Test\n",
2936
"\n",
30-
"From the installation directory run\\\n",
31-
"`make install_test`\\\n",
37+
"From the installation directory run\n",
38+
"```\n",
39+
"make install_test\n",
40+
"```\n",
3241
"It lasts about 45 seconds. If it succeeds, CLIMADA is properly installed and ready to use.\n",
3342
"\n",
3443
"### Unit Tests\n",
3544
"\n",
36-
"From the installation directory run\\\n",
37-
"`make unit_test`\\\n",
45+
"From the installation directory run\n",
46+
"```\n",
47+
"make unit_test\n",
48+
"```\n",
3849
"It lasts about 5 minutes and runs unit tests for all modules.\n",
3950
"\n",
4051
"### Integration Tests\n",
4152
"\n",
42-
"From the installation directory run\\\n",
43-
"`make integ_test`\\\n",
44-
"It lasts about 45 minutes and runs extensive integration tests, during which also data from external resources is read. An open internet connection is required for a successful test run."
53+
"From the installation directory run\n",
54+
"```\n",
55+
"make integ_test\n",
56+
"```\n",
57+
"It lasts about 15 minutes and runs extensive integration tests, during which also data from external resources is read. An open internet connection is required for a successful test run."
4558
]
4659
},
4760
{
61+
"attachments": {},
4862
"cell_type": "markdown",
4963
"metadata": {},
5064
"source": [
@@ -59,6 +73,7 @@
5973
]
6074
},
6175
{
76+
"attachments": {},
6277
"cell_type": "markdown",
6378
"metadata": {},
6479
"source": [
@@ -69,6 +84,7 @@
6984
]
7085
},
7186
{
87+
"attachments": {},
7288
"cell_type": "markdown",
7389
"metadata": {},
7490
"source": [
@@ -85,6 +101,7 @@
85101
]
86102
},
87103
{
104+
"attachments": {},
88105
"cell_type": "markdown",
89106
"metadata": {},
90107
"source": [
@@ -104,6 +121,7 @@
104121
]
105122
},
106123
{
124+
"attachments": {},
107125
"cell_type": "markdown",
108126
"metadata": {},
109127
"source": [
@@ -117,6 +135,7 @@
117135
]
118136
},
119137
{
138+
"attachments": {},
120139
"cell_type": "markdown",
121140
"metadata": {},
122141
"source": [
@@ -149,6 +168,7 @@
149168
]
150169
},
151170
{
171+
"attachments": {},
152172
"cell_type": "markdown",
153173
"metadata": {},
154174
"source": [
@@ -172,6 +192,7 @@
172192
]
173193
},
174194
{
195+
"attachments": {},
175196
"cell_type": "markdown",
176197
"metadata": {},
177198
"source": [
@@ -183,6 +204,7 @@
183204
]
184205
},
185206
{
207+
"attachments": {},
186208
"cell_type": "markdown",
187209
"metadata": {},
188210
"source": [
@@ -198,6 +220,7 @@
198220
]
199221
},
200222
{
223+
"attachments": {},
201224
"cell_type": "markdown",
202225
"metadata": {},
203226
"source": [
@@ -231,6 +254,7 @@
231254
]
232255
},
233256
{
257+
"attachments": {},
234258
"cell_type": "markdown",
235259
"metadata": {},
236260
"source": [
@@ -240,6 +264,7 @@
240264
]
241265
},
242266
{
267+
"attachments": {},
243268
"cell_type": "markdown",
244269
"metadata": {},
245270
"source": [
@@ -249,6 +274,7 @@
249274
]
250275
},
251276
{
277+
"attachments": {},
252278
"cell_type": "markdown",
253279
"metadata": {},
254280
"source": [
@@ -259,6 +285,7 @@
259285
]
260286
},
261287
{
288+
"attachments": {},
262289
"cell_type": "markdown",
263290
"metadata": {},
264291
"source": [
@@ -276,6 +303,7 @@
276303
]
277304
},
278305
{
306+
"attachments": {},
279307
"cell_type": "markdown",
280308
"metadata": {},
281309
"source": [
@@ -355,6 +383,7 @@
355383
]
356384
},
357385
{
386+
"attachments": {},
358387
"cell_type": "markdown",
359388
"metadata": {},
360389
"source": [
@@ -372,6 +401,7 @@
372401
]
373402
},
374403
{
404+
"attachments": {},
375405
"cell_type": "markdown",
376406
"metadata": {},
377407
"source": [

doc/guide/install.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ For advanced Python users or developers of CLIMADA, we recommed cloning the CLIM
148148
If this test passes, great!
149149
You are good to go.
150150

151+
.. _install-dev:
152+
151153
Install Developer Dependencies (Optional)
152154
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
153155

@@ -177,6 +179,15 @@ The CLIMADA Python package defines the following `extras <https://peps.python.or
177179
* - ``dev``
178180
- combination of ``doc`` and ``test``
179181

182+
For executing the pre-defined test scripts in exactly the same way as they are executed by the automated CI pipeline, you will need ``make`` to be installed.
183+
On macOS and on Linux it is pre-installed. On Windows, it can easily be installed with conda:
184+
185+
.. code-block:: shell
186+
187+
conda install -n climada_env make
188+
189+
Instructions for running the test scripts can be found in the :doc:`Testing and CI Guide <Guide_Continuous_Integration_and_Testing>`.
190+
180191
Install CLIMADA Petals (Optional)
181192
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182193

script/jenkins/branches/Jenkinsfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ pipeline {
2323
sh '''#!/bin/bash
2424
export PATH=$PATH:$CONDAPATH
2525
source activate climada_env
26+
python -m pip install pytest pytest-cov pytest-subtests
2627
rm -rf tests_xml/
2728
rm -rf coverage/
28-
python -m coverage run tests_runner.py unit
29-
python -m coverage xml -o coverage.xml
30-
python -m coverage html -d coverage'''
29+
make unit_test'''
3130
}
3231
}
3332

@@ -41,4 +40,4 @@ pipeline {
4140
cobertura coberturaReportFile: 'coverage.xml'
4241
}
4342
}
44-
}
43+
}

script/jenkins/ci_night/Jenkinsfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ pipeline {
2121
source activate climada_env
2222
rm -rf tests_xml/
2323
rm -rf coverage/
24-
python -m coverage run --parallel-mode --concurrency=multiprocessing tests_runner.py unit
25-
python -m coverage run --parallel-mode --concurrency=multiprocessing tests_runner.py integ
26-
python -m coverage combine
27-
python -m coverage xml -o coverage.xml
28-
python -m coverage html -d coverage'''
24+
make test'''
2925
}
3026
}
3127
}
@@ -36,4 +32,4 @@ pipeline {
3632
cobertura coberturaReportFile: 'coverage.xml', enableNewApi: false
3733
}
3834
}
39-
}
35+
}

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
# Requirements for testing
2424
DEPS_TEST = [
25-
"coverage>=7.2",
2625
"ipython",
2726
"mccabe>=0.6",
2827
"pylint==2.7.1",
28+
"pytest",
29+
"pytest-cov",
30+
"pytest-subtests",
2931
]
3032

3133
setup(

0 commit comments

Comments
 (0)