17
17
from cpmpy .exceptions import NotSupportedError , TransformationNotImplementedError
18
18
import itertools
19
19
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" ))
29
31
30
32
# SOLVERS = SolverLookup.supported()
31
33
SOLVERS = [
32
- "ortools" ,
33
- "gurobi" ,
34
- "minizinc" ,
35
- ]
34
+ "ortools" ,
35
+ "gurobi" ,
36
+ "minizinc" ,
37
+ ]
36
38
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.
41
43
42
44
Args:
43
45
solver ([string]): Loaded with parametrized solver name
44
46
example ([string]): Loaded with parametrized example filename
45
47
"""
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" )
48
50
51
+ base_solvers = SolverLookup .base_solvers
49
52
try :
50
- base_solvers = SolverLookup .base_solvers
51
53
solver_class = SolverLookup .lookup (solver )
52
54
if not solver_class .supported ():
53
55
# check this here, as unsupported solvers can fail the example for various reasons
@@ -59,8 +61,10 @@ def test_examples(solver, example):
59
61
runpy .run_path (example , run_name = "__main__" ) # many examples won't do anything `__name__ != "__main__"`
60
62
except (NotSupportedError , TransformationNotImplementedError ) as e :
61
63
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 } " )
64
68
except ValueError as e :
65
69
if "Unknown solver" in str (e ):
66
70
pytest .skip (reason = f"Skipped, example uses specific solver, raised: { e } " )
@@ -71,3 +75,10 @@ def test_examples(solver, example):
71
75
finally :
72
76
SolverLookup .base_solvers = base_solvers
73
77
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