Skip to content

Commit 7529523

Browse files
committed
fix failure with uninstalled petsc
1 parent 964f9e4 commit 7529523

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

idaes/core/solvers/tests/test_solvers.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,27 @@
1919
from idaes.core.solvers import get_solver, ipopt_has_linear_solver, petsc
2020

2121

22-
@pytest.mark.unit
23-
def test_petsc_available():
24-
if not pyo.SolverFactory("petsc_snes").available():
25-
raise RuntimeError("Could not find petsc (petsc is an optional extra).")
26-
27-
28-
@pytest.mark.unit
29-
def test_couenne_available():
30-
if not pyo.SolverFactory("couenne").available():
31-
raise RuntimeError("Could not find couenne.")
22+
class OptionalSolverChecks:
23+
"""Encapsulate optional solver checks.
3224
25+
Name of method matches `pyetst.mark.parametrize()` 'solver_name' argument
26+
in the 'test_solver_available()' function.
27+
"""
3328

34-
@pytest.mark.unit
35-
def test_bonmin_available():
36-
if not pyo.SolverFactory("bonmin").available():
37-
raise RuntimeError("Could not find bonmin.")
29+
@staticmethod
30+
def petsc():
31+
return petsc.petsc_available()
3832

3933

4034
@pytest.mark.unit
41-
def test_sipopt_available():
42-
if not pyo.SolverFactory("ipopt_sens").available():
43-
raise RuntimeError("Could not find ipopt_sens.")
35+
@pytest.mark.parametrize(
36+
"solver_name,optional",
37+
[("couenne", False), ("bonmin", False), ("ipopt_sens", False), ("petsc", True)],
38+
)
39+
def test_solver_available(solver_name, optional):
40+
if optional and not getattr(OptionalSolverChecks, solver_name)():
41+
pytest.skip(reason=f"Optional solver '{solver_name}' is not installed.")
42+
assert pyo.SolverFactory(solver_name).available(), f"Could not find {solver_name}."
4443

4544

4645
@pytest.mark.unit
@@ -165,7 +164,9 @@ def test_ipopt_has_mumps():
165164

166165

167166
@pytest.mark.unit
168-
@pytest.mark.skipif(not petsc.petsc_available(), reason="PETSc solver not available")
167+
@pytest.mark.skipif(
168+
not OptionalSolverChecks.petsc(), reason="PETSc solver not available"
169+
)
169170
def test_petsc_idaes_solve():
170171
"""
171172
Make sure there is no issue with the solver class or default settings that
@@ -178,7 +179,9 @@ def test_petsc_idaes_solve():
178179

179180

180181
@pytest.mark.unit
181-
@pytest.mark.skipif(not petsc.petsc_available(), reason="PETSc solver not available")
182+
@pytest.mark.skipif(
183+
not OptionalSolverChecks.petsc(), reason="PETSc solver not available"
184+
)
182185
def test_petsc_dae_idaes_solve():
183186
"""
184187
Check that the PETSc DAE solver works.

0 commit comments

Comments
 (0)