Skip to content

Commit b5b15ac

Browse files
committed
Separate tests for examples & advanced_examples
1 parent 70b00b9 commit b5b15ac

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

tests/test_examples.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,39 @@
1717
from cpmpy.exceptions import NotSupportedError, TransformationNotImplementedError
1818
import itertools
1919

20-
cwd = getcwd()
21-
if 'y' in cwd[-2:]:
22-
EXAMPLES = glob(join(".", "examples", "*.py")) + \
23-
glob(join(".", "examples", "advanced", "*.py")) + \
24-
glob(join(".", "examples", "csplib", "*.py"))
25-
else:
26-
EXAMPLES = glob(join("..", "examples", "*.py")) + \
27-
glob(join("..", "examples", "advanced", "*.py")) + \
28-
glob(join("..", "examples", "csplib", "*.py"))
20+
prefix = '.' if 'y' in getcwd()[-2:] else '..'
21+
22+
TO_SKIP = [
23+
"prob001_convert_data.py"
24+
]
25+
26+
EXAMPLES = glob(join(prefix, "examples", "*.py")) + \
27+
glob(join(prefix, "examples", "csplib", "*.py"))
28+
EXAMPLES = [e for e in EXAMPLES if not any(x in e for x in TO_SKIP)]
29+
30+
ADVANCED_EXAMPLES = glob(join(prefix, "examples", "advanced", "*.py"))
2931

3032
# SOLVERS = SolverLookup.supported()
3133
SOLVERS = [
32-
"ortools",
33-
"gurobi",
34-
"minizinc",
35-
]
34+
"ortools",
35+
"gurobi",
36+
"minizinc",
37+
]
3638

37-
@pytest.mark.parametrize(("solver", "example"), itertools.product(SOLVERS, EXAMPLES))
38-
@pytest.mark.timeout(60) # some examples take long, use 10 second timeout
39-
def test_examples(solver, example):
40-
"""Loads example files and executes with default solver
39+
@pytest.mark.parametrize(("solver", "example"), itertools.product(SOLVERS, EXAMPLES)) # run the test for each combination of solver and example
40+
@pytest.mark.timeout(60) # 60-second timeout for each test
41+
def test_example(solver, example):
42+
"""Loads the example file and executes its __main__ block with the given solver being set as default.
4143
4244
Args:
4345
solver ([string]): Loaded with parametrized solver name
4446
example ([string]): Loaded with parametrized example filename
4547
"""
46-
if solver in ('gurobi', 'minizinc') and any(x in example for x in ["npuzzle", "tst_likevrp", "ortools_presolve_propagate", 'sudoku_ratrun1.py']):
47-
return pytest.skip(reason=f"exclude {example} for gurobi, too slow or solver specific")
48+
if solver in ('gurobi', 'minizinc') and any(x in example for x in ["npuzzle.py", "tst_likevrp.py", 'sudoku_', 'pareto_optimal.py', 'prob009_perfect_squares.py', 'blocks_world.py', 'flexible_jobshop.py']):
49+
return pytest.skip(reason=f"exclude {example} for {solver}, too slow or solver-specific")
4850

51+
base_solvers = SolverLookup.base_solvers
4952
try:
50-
base_solvers = SolverLookup.base_solvers
5153
solver_class = SolverLookup.lookup(solver)
5254
if not solver_class.supported():
5355
# check this here, as unsupported solvers can fail the example for various reasons
@@ -59,8 +61,10 @@ def test_examples(solver, example):
5961
runpy.run_path(example, run_name="__main__") # many examples won't do anything `__name__ != "__main__"`
6062
except (NotSupportedError, TransformationNotImplementedError) as e:
6163
if solver == 'ortools': # `from` augments exception trace
62-
raise Exception("Example not supported by ortools, which is currently able to run all models, but raised") from e
63-
pytest.skip(reason=f"Skipped, solver or its transformation does not support model, raised {type(e).__name__}: {e}")
64+
raise Exception(
65+
"Example not supported by ortools, which is currently able to run all models, but raised") from e
66+
pytest.skip(
67+
reason=f"Skipped, solver or its transformation does not support model, raised {type(e).__name__}: {e}")
6468
except ValueError as e:
6569
if "Unknown solver" in str(e):
6670
pytest.skip(reason=f"Skipped, example uses specific solver, raised: {e}")
@@ -71,3 +75,10 @@ def test_examples(solver, example):
7175
finally:
7276
SolverLookup.base_solvers = base_solvers
7377

78+
79+
@pytest.mark.parametrize("example", ADVANCED_EXAMPLES)
80+
@pytest.mark.timeout(10)
81+
def test_advanced_example(example):
82+
"""Loads the advanced example file and executes its __main__ block with no default solver set."""
83+
sys.argv = [example]
84+
runpy.run_path(example, run_name="__main__")

0 commit comments

Comments
 (0)