Skip to content

Commit ed5f6ac

Browse files
authored
refactor test scripts (#152)
1 parent 5bfd47f commit ed5f6ac

3 files changed

Lines changed: 70 additions & 49 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ docs/_build
88

99
# Ignore output when run the test
1010
tests/**/output
11+
tests/test_summary.txt
1112

1213
# Ignore output when run the examples
1314
examples/*/density_*

tests/run_tests.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/usr/bin/env bash
22

3-
# exit when any command fails
4-
set -e
5-
63
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
74

85
# scenarios to test
@@ -15,10 +12,13 @@ declare -a scenarios=(
1512
"two_layers_with_variable_conductivity"
1613
)
1714

15+
# Delete old test summary
16+
rm -rf "${SCRIPT_DIR}/test_summary.txt"
17+
1818
# Run scenarios
1919
for scenario in "${scenarios[@]}"
2020
do
21-
echo -e "\n==> Running scenario: ${scenario} <===\n"
21+
echo -e "\n==> Running simulation: ${scenario} <==\n"
2222

2323
# Remove old output
2424
rm -rf "${SCRIPT_DIR}/data/${scenario}/output"
@@ -31,6 +31,7 @@ do
3131
MANDYOC=${MANDYOC} bash run.sh
3232
done
3333

34-
# Run tests
34+
echo -e "\n==> Verifying results <==\n"
35+
3536
cd "${SCRIPT_DIR}" && \
36-
pytest -v testing_results.py
37+
pytest -v testing_results.py --tb=line -rf | tee test_summary.txt

tests/testing_results.py

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
"""
22
Compare the Mandyoc output data with the expected data
33
"""
4-
import os
54
import pytest
65
import numpy as np
76
import numpy.testing as npt
87
from pathlib import Path
98

10-
# Test path
11-
base_path = Path(os.path.realpath(os.path.abspath(__file__))).parent
129

13-
scenarios = [
14-
"vanKeken1997_case1a",
15-
"Crameri2012_case2",
16-
"continental_rift",
17-
"punch",
18-
"Schmalholz_2011",
19-
"two_layers_with_variable_conductivity",
20-
]
10+
BASE_PATH = Path(__file__).parent.absolute()
2111

22-
# Name of the files to compare
23-
fields = [
24-
"time",
12+
MAIN_FIELDS = [
2513
"density",
2614
"heat",
2715
"pressure",
16+
"step",
2817
"strain",
2918
"strain_rate",
30-
"surface",
3119
"temperature",
20+
"time",
3221
"velocity",
3322
"viscosity",
34-
"step",
3523
]
3624

37-
# steps to compare
38-
steps = [0, 1, 10000]
25+
26+
# Definition of each scenario
27+
SCENARIOS = {
28+
"vanKeken1997_case1a": {
29+
"steps": [0, 1],
30+
"fields": MAIN_FIELDS,
31+
},
32+
"Crameri2012_case2": {
33+
"steps": [0],
34+
"fields": MAIN_FIELDS,
35+
"num_procs": 1,
36+
},
37+
"continental_rift": {
38+
"steps": [0, 1],
39+
"fields": MAIN_FIELDS,
40+
},
41+
"punch": {
42+
"steps": [0],
43+
"fields": MAIN_FIELDS,
44+
},
45+
"Schmalholz_2011": {
46+
"steps": [0, 1],
47+
"fields": MAIN_FIELDS,
48+
},
49+
"two_layers_with_variable_conductivity": {
50+
"steps": [0, 10000],
51+
"fields": MAIN_FIELDS,
52+
},
53+
}
54+
3955

4056
def read(filename):
4157
"""
@@ -49,37 +65,40 @@ def read(filename):
4965
return data
5066

5167

52-
@pytest.mark.parametrize("step", steps)
53-
@pytest.mark.parametrize("field", fields)
54-
@pytest.mark.parametrize("scenario", scenarios)
55-
def test_result(scenario, field, step):
56-
"""Run tests"""
68+
def get_test_cases():
69+
"""Generates valid test combinations."""
70+
cases = []
71+
for name, config in SCENARIOS.items():
72+
for step in config["steps"]:
73+
for field in config["fields"]:
74+
cases.append((name, field, step))
75+
return cases
76+
5777

58-
if scenario != 'Crameri2012_case2' and field == 'surface':
59-
pytest.skip('No surface for this scenario')
78+
@pytest.mark.parametrize("scenario, field, step", get_test_cases())
79+
def test_result(scenario, field, step):
80+
config = SCENARIOS[scenario]
6081

61-
if scenario == 'Crameri2012_case2' and step == 1:
62-
pytest.skip('Tested with only one processor')
82+
test_path = BASE_PATH / "data" / scenario / "output"
83+
expected_path = BASE_PATH / "data" / scenario / "expected"
6384

64-
if (scenario == 'punch' and step == 1) or (scenario == 'punch' and field == 'step_1'):
65-
pytest.skip('Test with only one step')
85+
if not test_path.exists():
86+
pytest.fail(f"Simulation directory missing. Scenario {scenario} likely failed to run.")
6687

67-
if (scenario != 'two_layers_with_variable_conductivity' and step == 10000) or (scenario == 'two_layers_with_variable_conductivity' and step == 1):
68-
pytest.skip('Test step/file not available for this scenario')
88+
# Handle the 'step' field
89+
if field == 'step':
90+
num_procs = config.get("num_procs", 2)
91+
filenames = [f"step_{step}_{i}.txt" for i in range(num_procs)]
92+
else:
93+
filenames = [f"{field}_{step}.txt"]
6994

70-
test_path = base_path / "data" / scenario/ "output"
71-
expected_path = base_path / "data" / scenario/ "expected"
95+
for fname in filenames:
96+
out_file = test_path / fname
97+
exp_file = expected_path / fname
7298

73-
filenames = [f"{field}_{step}.txt"]
99+
assert out_file.exists(), f"Missing output file: {out_file}"
74100

75-
if field == 'step':
76-
if scenario in ['Crameri2012_case2']:
77-
filenames = [f"step_{step}_0.txt"]
78-
else:
79-
filenames = [f"step_{step}_0.txt", f"step_{step}_1.txt"]
80-
81-
for filename in filenames:
82-
output = read(test_path / filename)
83-
expected = read(expected_path / filename)
101+
output = read(out_file)
102+
expected = read(exp_file)
84103

85104
npt.assert_allclose(output, expected, rtol=2e-4, atol=1.0e-18)

0 commit comments

Comments
 (0)