Skip to content

Commit 2014dd8

Browse files
authored
ci: convert addition autotests to use flopyTest testing framework (#1278)
1 parent 797ca8a commit 2014dd8

27 files changed

+683
-576
lines changed

autotest/Autotesting.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ The space between `-n` and the number of processors can be replaced with a
4545

4646
pytest -v -n=auto t001_test.py
4747

48+
### Debugging failed tests
49+
50+
To debug a failed autotest rerun the failed test by running the following command from the autotest directory
51+
52+
python mffailedtest.py --keep
53+
54+
The `--keep` will retain the test directories created by the test, which will allow the input or output files to be evaluated for errors.
4855

4956
## Creating an autotest
5057

autotest/t016_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import flopy
33
import numpy as np
4-
54
from ci_framework import baseTestDir, flopyTest
65

76
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)

autotest/t017_test.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import os
44
import numpy as np
55
import flopy
6+
from ci_framework import baseTestDir, flopyTest
67

7-
cpth = os.path.join("temp", "t017")
8-
# make the directory if it does not exists
9-
if not os.path.isdir(cpth):
10-
os.makedirs(cpth, exist_ok=True)
8+
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)
119

1210

1311
def test_formattedfile_read():
12+
model_ws = f"{baseDir}_test_formattedfile_read"
13+
testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True)
1414

1515
h = flopy.utils.FormattedHeadFile(
1616
os.path.join("..", "examples", "data", "mf2005_test", "test1tr.githds")
@@ -41,7 +41,7 @@ def test_formattedfile_read():
4141
h.close()
4242

4343
# Check error when reading empty file
44-
fname = os.path.join(cpth, "empty.githds")
44+
fname = os.path.join(model_ws, "empty.githds")
4545
with open(fname, "w"):
4646
pass
4747
with pytest.raises(ValueError):
@@ -51,6 +51,8 @@ def test_formattedfile_read():
5151

5252

5353
def test_binaryfile_read():
54+
model_ws = f"{baseDir}_test_binaryfile_read"
55+
testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True)
5456

5557
h = flopy.utils.HeadFile(
5658
os.path.join("..", "examples", "data", "freyberg", "freyberg.githds")
@@ -81,7 +83,7 @@ def test_binaryfile_read():
8183
h.close()
8284

8385
# Check error when reading empty file
84-
fname = os.path.join(cpth, "empty.githds")
86+
fname = os.path.join(model_ws, "empty.githds")
8587
with open(fname, "w"):
8688
pass
8789
with pytest.raises(ValueError):
@@ -148,6 +150,8 @@ def test_cellbudgetfile_read():
148150

149151

150152
def test_cellbudgetfile_position():
153+
model_ws = f"{baseDir}_test_cellbudgetfile_position"
154+
testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True)
151155

152156
fpth = os.path.join(
153157
"..", "examples", "data", "zonbud_examples", "freyberg.gitcbc"
@@ -176,7 +180,7 @@ def test_cellbudgetfile_position():
176180
length = os.path.getsize(fpth) - ipos
177181

178182
buffsize = 32
179-
opth = os.path.join(cpth, "end.cbc")
183+
opth = os.path.join(model_ws, "end.cbc")
180184
with open(opth, "wb") as fout:
181185
while length:
182186
chunk = min(buffsize, length)
@@ -204,7 +208,7 @@ def test_cellbudgetfile_position():
204208
assert np.array_equal(d1, d2), msg
205209

206210
# Check error when reading empty file
207-
fname = os.path.join(cpth, "empty.gitcbc")
211+
fname = os.path.join(model_ws, "empty.gitcbc")
208212
with open(fname, "w"):
209213
pass
210214
with pytest.raises(ValueError):
@@ -324,12 +328,14 @@ def test_cellbudgetfile_readrecord_waux():
324328

325329

326330
def test_binaryfile_writeread():
331+
model_ws = f"{baseDir}_test_binaryfile_writeread"
332+
testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True)
327333

328334
pth = os.path.join("..", "examples", "data", "nwt_test")
329335
model = "Pr3_MFNWT_lower.nam"
330336
ml = flopy.modflow.Modflow.load(model, version="mfnwt", model_ws=pth)
331337
# change the model work space
332-
ml.change_model_ws(os.path.join("temp", "t017"))
338+
ml.change_model_ws(model_ws)
333339
#
334340
ncol = ml.dis.ncol
335341
nrow = ml.dis.nrow
@@ -350,7 +356,7 @@ def test_binaryfile_writeread():
350356
kper=1,
351357
)
352358
b = ml.dis.botm.array[0, :, :].astype(np.float64)
353-
pth = os.path.join("temp", "t017", "bottom.hds")
359+
pth = os.path.join(model_ws, "bottom.hds")
354360
flopy.utils.Util2d.write_bin(b.shape, pth, b, header_data=header)
355361

356362
bo = flopy.utils.HeadFile(pth, precision=precision)
@@ -380,7 +386,7 @@ def test_binaryfile_writeread():
380386
kper=1,
381387
)
382388
b = ml.dis.botm.array[0, :, :].astype(np.float32)
383-
pth = os.path.join("temp", "t017", "bottom_single.hds")
389+
pth = os.path.join(model_ws, "bottom_single.hds")
384390
flopy.utils.Util2d.write_bin(b.shape, pth, b, header_data=header)
385391

386392
bo = flopy.utils.HeadFile(pth, precision=precision)

autotest/t018_test.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import os
22
import numpy as np
33
import flopy
4+
from ci_framework import baseTestDir, flopyTest
45

5-
# import flopy.pest.templatewriter
6-
# import flopy.pest.templatewriter as flopy.pest.templatewriter
7-
# import flopy.pest.flopy.pest.params as flopy.pest.params
8-
9-
mpth = os.path.join("temp", "t018")
10-
# make the directory if it does not exist
11-
if not os.path.isdir(mpth):
12-
os.makedirs(mpth, exist_ok=True)
6+
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)
137

148

159
def test_tpl_constant():
10+
model_ws = f"{baseDir}_test_tpl_constant"
11+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
12+
1613
# Define the model dimensions
1714
nlay = 3
1815
nrow = 20
1916
ncol = 20
2017

2118
# Create the flopy model object and add the dis and lpf packages
22-
m = flopy.modflow.Modflow(modelname="tpl1", model_ws=mpth)
19+
m = flopy.modflow.Modflow(modelname="tpl1", model_ws=model_ws)
2320
dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol)
2421
lpf = flopy.modflow.ModflowLpf(m, hk=10.0)
2522

@@ -46,19 +43,22 @@ def test_tpl_constant():
4643
tw = flopy.pest.templatewriter.TemplateWriter(m, [p])
4744
tw.write_template()
4845

49-
tplfile = os.path.join(mpth, "tpl1.lpf.tpl")
46+
tplfile = os.path.join(model_ws, "tpl1.lpf.tpl")
5047
assert os.path.isfile(tplfile)
5148

5249
return
5350

5451

5552
def test_tpl_layered():
53+
model_ws = f"{baseDir}_test_tpl_layered"
54+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
55+
5656
nlay = 3
5757
nrow = 20
5858
ncol = 20
5959

6060
# Create the flopy model object and add the dis and lpf packages
61-
m = flopy.modflow.Modflow(modelname="tpl2", model_ws=mpth)
61+
m = flopy.modflow.Modflow(modelname="tpl2", model_ws=model_ws)
6262
dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol)
6363
lpf = flopy.modflow.ModflowLpf(m, hk=10.0)
6464

@@ -81,19 +81,22 @@ def test_tpl_layered():
8181
tw = flopy.pest.templatewriter.TemplateWriter(m, [p])
8282
tw.write_template()
8383

84-
tplfile = os.path.join(mpth, "tpl2.lpf.tpl")
84+
tplfile = os.path.join(model_ws, "tpl2.lpf.tpl")
8585
assert os.path.isfile(tplfile)
8686

8787
return
8888

8989

9090
def test_tpl_zoned():
91+
model_ws = f"{baseDir}_test_tpl_zoned"
92+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
93+
9194
nlay = 3
9295
nrow = 20
9396
ncol = 20
9497

9598
# Create the flopy model object and add the dis and lpf packages
96-
m = flopy.modflow.Modflow(modelname="tpl3", model_ws=mpth)
99+
m = flopy.modflow.Modflow(modelname="tpl3", model_ws=model_ws)
97100
dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol)
98101
lpf = flopy.modflow.ModflowLpf(m, hk=10.0)
99102

@@ -144,7 +147,7 @@ def test_tpl_zoned():
144147
tw = flopy.pest.templatewriter.TemplateWriter(m, plist)
145148
tw.write_template()
146149

147-
tplfile = os.path.join(mpth, "tpl3.lpf.tpl")
150+
tplfile = os.path.join(model_ws, "tpl3.lpf.tpl")
148151
assert os.path.isfile(tplfile)
149152

150153
return

autotest/t019_test.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import os
22
import numpy as np
33
import flopy
4+
from ci_framework import baseTestDir, flopyTest
45

5-
mpth = os.path.join("temp", "t019")
6-
# make the directory if it does not exist
7-
if not os.path.isdir(mpth):
8-
os.makedirs(mpth, exist_ok=True)
6+
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)
97

108

119
# Test hydmod data readers
1210
def test_hydmodfile_create():
13-
model_ws = os.path.join(mpth)
14-
if not os.path.isdir(model_ws):
15-
os.makedirs(model_ws, exist_ok=True)
11+
model_ws = f"{baseDir}_test_hydmodfile_create"
12+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
13+
1614
m = flopy.modflow.Modflow("test", model_ws=model_ws)
1715
hyd = flopy.modflow.ModflowHyd(m)
1816
m.hyd.write_file()
@@ -48,6 +46,9 @@ def test_hydmodfile_create():
4846

4947

5048
def test_hydmodfile_load():
49+
model_ws = f"{baseDir}_test_hydmodfile_load"
50+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
51+
5152
model = "test1tr.nam"
5253
pth = os.path.join("..", "examples", "data", "hydmod_test")
5354
m = flopy.modflow.Modflow.load(
@@ -58,10 +59,6 @@ def test_hydmodfile_load():
5859
hydref, flopy.modflow.ModflowHyd
5960
), "Did not load hydmod package...test1tr.hyd"
6061

61-
model_ws = os.path.join(mpth)
62-
if not os.path.isdir(model_ws):
63-
os.makedirs(model_ws, exist_ok=True)
64-
6562
m.change_model_ws(model_ws)
6663
m.hyd.write_file()
6764

@@ -75,8 +72,6 @@ def test_hydmodfile_load():
7572

7673

7774
def test_hydmodfile_read():
78-
import os
79-
import flopy
8075

8176
pth = os.path.join(
8277
"..", "examples", "data", "hydmod_test", "test1tr.hyd.gitbin"
@@ -149,8 +144,6 @@ def test_hydmodfile_read():
149144

150145

151146
def test_mf6obsfile_read():
152-
import os
153-
import flopy
154147

155148
try:
156149
import pandas as pd

autotest/t020_test.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
# Test modflow write adn run
1+
# Test modflow write and run
22
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from ci_framework import baseTestDir, flopyTest
35

4-
try:
5-
import matplotlib.pyplot as plt
6-
7-
# if os.getenv('TRAVIS'): # are we running https://travis-ci.org/ automated tests ?
8-
# matplotlib.use('Agg') # Force matplotlib not to use any Xwindows backend
9-
except:
10-
plt = None
6+
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)
117

128

139
def analyticalWaterTableSolution(h1, h2, z, R, K, L, x):
@@ -34,10 +30,10 @@ def test_mfnwt_run():
3430
print(f"Specified executable {exe_name} does not exist in path")
3531
return
3632

33+
model_ws = f"{baseDir}_test_mfnwt_run"
34+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
35+
3736
modelname = "watertable"
38-
model_ws = os.path.join("temp", "t020")
39-
if not os.path.isdir(model_ws):
40-
os.makedirs(model_ws, exist_ok=True)
4137

4238
# model dimensions
4339
nlay, nrow, ncol = 1, 1, 100
@@ -172,6 +168,9 @@ def test_irch():
172168
import os
173169
import flopy
174170

171+
model_ws = f"{baseDir}_test_tpl_constant"
172+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
173+
175174
org_model_ws = os.path.join(
176175
"..", "examples", "data", "freyberg_multilayer_transient"
177176
)
@@ -197,11 +196,11 @@ def test_irch():
197196
d = arr - aarr
198197
assert np.abs(d).sum() == 0
199198

200-
new_model_ws = "temp"
201-
m.change_model_ws(new_model_ws)
199+
m.change_model_ws(model_ws)
202200
m.write_input()
201+
203202
mm = flopy.modflow.Modflow.load(
204-
nam_file, model_ws="temp", forgive=False, verbose=True, check=False
203+
nam_file, model_ws=model_ws, forgive=False, verbose=True, check=False
205204
)
206205
for kper in range(m.nper):
207206
arr = irch[kper]

autotest/t021_test.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# Test modflow write adn run
21
import os
32
import numpy as np
43
import flopy
4+
from ci_framework import baseTestDir, flopyTest
55

6-
mpth = os.path.join("temp", "t021")
7-
# make the directory if it does not exist
8-
if not os.path.isdir(mpth):
9-
os.makedirs(mpth, exist_ok=True)
6+
baseDir = baseTestDir(__file__, relPath="temp", verbose=True)
107

118

129
def test_mflist_external():
10+
model_ws = f"{baseDir}_test_mflist_external"
11+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
12+
1313
ml = flopy.modflow.Modflow(
14-
"mflist_test", model_ws=mpth, external_path=os.path.join(mpth, "ref")
14+
"mflist_test",
15+
model_ws=model_ws,
16+
external_path=os.path.join(model_ws, "ref"),
1517
)
1618
dis = flopy.modflow.ModflowDis(ml, 1, 10, 10, nper=3, perlen=1.0)
1719
wel_data = {
@@ -36,24 +38,24 @@ def test_mflist_external():
3638

3739

3840
def test_single_mflist_entry_load():
41+
model_ws = f"{baseDir}_test_single_mflist_entry_load"
42+
testFramework = flopyTest(verbose=True, testDirs=model_ws)
3943

40-
import os
41-
import numpy as np
42-
import flopy
43-
44-
model_ws = os.path.join("..", "examples", "data", "freyberg")
44+
pth = os.path.join("..", "examples", "data", "freyberg")
4545
m = flopy.modflow.Modflow.load(
46-
"freyberg.nam", model_ws=model_ws, load_only=["WEL"], check=False
46+
"freyberg.nam", model_ws=pth, load_only=["WEL"], check=False
4747
)
4848
w = m.wel
4949
spd = w.stress_period_data
5050
flopy.modflow.ModflowWel(m, stress_period_data={0: [0, 0, 0, 0.0]})
5151
m.external_path = "."
52-
m.change_model_ws("temp", reset_external=True)
52+
m.change_model_ws(model_ws, reset_external=True)
5353
m.write_input()
5454

5555
mm = flopy.modflow.Modflow.load(
56-
"freyberg.nam", model_ws="temp", forgive=False
56+
"freyberg.nam",
57+
model_ws=model_ws,
58+
forgive=False,
5759
)
5860
assert mm.wel.stress_period_data
5961
mm.write_input()

0 commit comments

Comments
 (0)