Skip to content

Commit

Permalink
test: update tests (#1522)
Browse files Browse the repository at this point in the history
* add marker for mf6 tests to pytest.ini
* mark mf6 tests (any importing flopy.mf6)
* un-xfail modflow external path tests
* exclude test_modflow.py::test_single_mflist_entry_load on windows CI
* add fixture to automatically close (or optionally show) plots created by tests
  • Loading branch information
wpbonelli authored Sep 1, 2022
1 parent a54e753 commit 8cf21f4
Show file tree
Hide file tree
Showing 20 changed files with 354 additions and 232 deletions.
25 changes: 25 additions & 0 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from urllib import request
from warnings import warn

import matplotlib.pyplot as plt
import pkg_resources
import pytest

Expand Down Expand Up @@ -337,6 +338,22 @@ def session_tmpdir(tmpdir_factory, request) -> Path:
copytree(temp, Path(keep) / temp.name)


# fixture to automatically close any plots (or optionally show them)


@pytest.fixture(autouse=True)
def close_plot(request):
yield

# plots only shown if requested via CLI flag,
# figures are available, and we're not in CI
show = request.config.getoption("--show-plots")
if len(plt.get_fignums()) > 0 and not is_in_ci() and show:
plt.show()
else:
plt.close("all")


# pytest configuration hooks


Expand Down Expand Up @@ -392,6 +409,14 @@ def pytest_addoption(parser):
help="Run only smoke tests (should complete in <1 minute).",
)

parser.addoption(
"--show-plots",
action="store_true",
default=False,
help="Show any figure windows created by test cases. (Useful to display plots for visual inspection, "
"but automated tests should probably also check patch collections or figure & axis properties.)",
)


def pytest_configure(config):
config.addinivalue_line(
Expand Down
3 changes: 2 additions & 1 deletion autotest/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ markers =
slow: tests that don't complete in a few seconds
example: exercise scripts, tutorials and notebooks
regression: tests that compare multiple results
meta: run by other tests (e.g. testing fixtures)
meta: run by other tests (e.g. testing fixtures)
mf6: tests for the mf6 module
2 changes: 2 additions & 0 deletions autotest/regression/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
from flopy.utils import CellBudgetFile
from flopy.utils.datautil import PyListUtil

pytestmark = pytest.mark.mf6


@requires_exe("mf6")
@requires_pkg("pymake")
Expand Down
18 changes: 9 additions & 9 deletions autotest/regression/test_mf6_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

from flopy.mf6 import MFSimulation

pytestmark = pytest.mark.mf6


@requires_exe("mf6")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
def test_mf6_example_simulations(tmpdir, mf6_example_namfiles):
"""
MF6 examples parametrized by simulation. Arg `mf6_example_namfiles` is a list
of models to run in order provided. Coupled models share the same workspace.
Parameters
----------
tmpdir: function-scoped temporary directory fixture
mf6_example_namfiles: ordered list of namfiles for 1+ coupled models
"""
# MF6 examples parametrized by simulation. `mf6_example_namfiles` is a list
# of models to run in order provided. Coupled models share the same tempdir
#
# Parameters
# ----------
# tmpdir: function-scoped temporary directory fixture
# mf6_example_namfiles: ordered list of namfiles for 1+ coupled models

import pymake

Expand Down
2 changes: 2 additions & 0 deletions autotest/test_binarygrid_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from flopy.discretization import StructuredGrid, UnstructuredGrid, VertexGrid
from flopy.mf6.utils import MfGrdFile

pytestmark = pytest.mark.mf6


@pytest.fixture(scope="module")
def mfgrd_test_path(example_data_path):
Expand Down
2 changes: 2 additions & 0 deletions autotest/test_cbc_full3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def load_mf2005(path, ws_out):
return ml


@pytest.mark.mf6
def load_mf6(path, ws_out):
sim = MFSimulation.load(
sim_name=Path(path).name,
Expand Down Expand Up @@ -118,6 +119,7 @@ def cbc_eval(cbcobj, nnodes, shape3d, modelgrid):
cobj_mg.close()


@pytest.mark.mf6
@pytest.mark.parametrize("path", mf6_paths)
def test_cbc_full3D_mf6(tmpdir, path):
sim = load_mf6(path, str(tmpdir))
Expand Down
2 changes: 2 additions & 0 deletions autotest/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect

import numpy as np
import pytest

from flopy.datbase import DataInterface, DataType
from flopy.mbase import ModelInterface
Expand Down Expand Up @@ -226,6 +227,7 @@ def test_mf2005_copy(example_data_path):
assert not model_is_copy(m, m_c)


@pytest.mark.mf6
def test_mf6_copy(example_data_path):
sim = MFSimulation.load(
"mfsim.nam",
Expand Down
8 changes: 8 additions & 0 deletions autotest/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ def test_export_contourf(tmpdir, example_data_path):
)


@pytest.mark.mf6
@requires_pkg("shapefile", "shapely")
def test_export_contours(tmpdir, example_data_path):
from shapefile import Reader
Expand Down Expand Up @@ -643,6 +644,7 @@ def test_export_contours(tmpdir, example_data_path):
assert len(shapes) == 65


@pytest.mark.mf6
@requires_pkg("shapely")
def test_mf6_grid_shp_export(tmpdir):
nlay = 2
Expand Down Expand Up @@ -1110,6 +1112,7 @@ def test_vtk_export_packages(tmpdir, example_data_path):
assert os.path.exists(filetocheck)


@pytest.mark.mf6
@requires_pkg("vtk")
def test_vtk_mf6(tmpdir, example_data_path):
# test mf6
Expand Down Expand Up @@ -1372,6 +1375,7 @@ def load_iverts(fname):
assert np.allclose(np.ravel(top), top2), "Field data not properly written"


@pytest.mark.mf6
@requires_pkg("vtk")
def test_vtk_vertex(tmpdir, example_data_path):
from vtkmodules.util.numpy_support import vtk_to_numpy
Expand Down Expand Up @@ -1530,6 +1534,7 @@ def load_iverts(fname, closed=False):
return iverts, np.array(xc), np.array(yc)


@pytest.mark.mf6
@requires_pkg("vtk")
def test_vtk_export_model_without_packages_names(tmpdir):
from vtkmodules.util.numpy_support import vtk_to_numpy
Expand Down Expand Up @@ -1587,6 +1592,7 @@ def test_vtk_export_model_without_packages_names(tmpdir):
assert np.allclose(cell_types, cell_types_answer), errmsg


@pytest.mark.mf6
@requires_pkg("vtk")
def test_vtk_export_disv1_model(tmpdir):
from vtkmodules.util.numpy_support import vtk_to_numpy
Expand Down Expand Up @@ -1659,6 +1665,7 @@ def test_vtk_export_disv1_model(tmpdir):
assert np.allclose(cell_types, cell_types_answer), errmsg


@pytest.mark.mf6
@requires_pkg("vtk")
def test_vtk_export_disv2_model(tmpdir):
from vtkmodules.util.numpy_support import vtk_to_numpy
Expand Down Expand Up @@ -1896,6 +1903,7 @@ def test_vtk_export_disu2_grid(tmpdir, example_data_path):
assert np.allclose(cell_types, cell_types_answer), errmsg


@pytest.mark.mf6
@requires_pkg("vtk", "shapefile")
def test_vtk_export_disu_model(tmpdir):
from vtkmodules.util.numpy_support import vtk_to_numpy
Expand Down
1 change: 1 addition & 0 deletions autotest/test_generate_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flopy.mf6.utils import generate_classes


@pytest.mark.mf6
@pytest.mark.skip(
reason="TODO: use external copy of the repo, otherwise files are rewritten"
)
Expand Down
11 changes: 8 additions & 3 deletions autotest/test_lake_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import pytest
from autotest.conftest import requires_pkg
from autotest.conftest import requires_exe, requires_pkg

from flopy.discretization import StructuredGrid
from flopy.mf6 import (
Expand All @@ -23,6 +23,8 @@
from flopy.modflow import Modflow
from flopy.utils import Raster

pytestmark = pytest.mark.mf6


def export_ascii_grid(modelgrid, file_path, v, nodata=0.0):
shape = v.shape
Expand Down Expand Up @@ -136,6 +138,7 @@ def get_lake_connection_data(
return lakeconnectiondata, nlakecon


@requires_exe("mf6")
def test_base_run(tmpdir, example_data_path):
mpath = example_data_path / "mf6-freyberg"
sim = MFSimulation().load(
Expand Down Expand Up @@ -177,6 +180,7 @@ def test_base_run(tmpdir, example_data_path):
)


@requires_exe("mf6")
@requires_pkg("rasterio")
def test_lake(tmpdir, example_data_path):
mpath = example_data_path / "mf6-freyberg"
Expand Down Expand Up @@ -295,9 +299,8 @@ def test_lake(tmpdir, example_data_path):

assert success, f"could not run {sim.name} with lake"

return


@requires_exe("mf6")
def test_embedded_lak_ex01(tmpdir, example_data_path):
nper = 1
nlay, nrow, ncol = 5, 17, 17
Expand Down Expand Up @@ -497,6 +500,7 @@ def test_embedded_lak_ex01(tmpdir, example_data_path):
assert success, f"could not run {sim.name}"


@requires_exe("mf6")
def test_embedded_lak_prudic(example_data_path):
lakebed_leakance = 1.0 # Lakebed leakance ($ft^{-1}$)
nlay = 8 # Number of layers
Expand Down Expand Up @@ -599,6 +603,7 @@ def test_embedded_lak_prudic(example_data_path):
), "idomain not updated correctly with lakibd"


@requires_exe("mf6")
def test_embedded_lak_prudic_mixed(example_data_path):
lakebed_leakance = 1.0 # Lakebed leakance ($ft^{-1}$)
nlay = 8 # Number of layers
Expand Down
Loading

0 comments on commit 8cf21f4

Please sign in to comment.