Skip to content

Commit 0b42e5f

Browse files
authored
Refactor to use ParallelTestRunner in the tests (#4119)
1 parent 301e229 commit 0b42e5f

File tree

10 files changed

+195
-316
lines changed

10 files changed

+195
-316
lines changed

Project.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
name = "JuMP"
22
uuid = "4076af6c-e467-56ae-b986-b466b2749572"
3-
repo = "https://github.com/jump-dev/JuMP.jl.git"
43
version = "1.29.4"
4+
repo = "https://github.com/jump-dev/JuMP.jl.git"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
99
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
1010
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1111
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
12-
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1312
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
13+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1414
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1515

1616
[weakdeps]
@@ -26,6 +26,7 @@ MacroTools = "0.5"
2626
MathOptInterface = "1.38.0"
2727
MutableArithmetics = "1.6"
2828
OrderedCollections = "1"
29+
ParallelTestRunner = "2.4.0"
2930
PrecompileTools = "1"
3031
Printf = "1"
3132
SparseArrays = "1"
@@ -34,7 +35,8 @@ julia = "1.10"
3435

3536
[extras]
3637
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
38+
ParallelTestRunner = "d3525ed8-44d0-4b2c-a655-542cee43accc"
3739
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3840

3941
[targets]
40-
test = ["DimensionalData", "Test"]
42+
test = ["DimensionalData", "ParallelTestRunner", "Test"]

docs/src/developers/extensions.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,30 @@ implements an [`AbstractModel`](@ref) is [InfiniteOpt.jl](https://github.com/inf
405405
### Testing JuMP extensions
406406

407407
The JuMP test suite contains a large number of tests for JuMP extensions. You
408-
can run these tests by copying the MIT-licensed [`Kokako.jl`](https://github.com/jump-dev/JuMP.jl/blob/master/test/Kokako.jl)
409-
file in the JuMP tests into your `/test` folder, and then adding this snippet to
410-
your `/test/runtests.jl` file:
408+
can run these tests by copying the following snippet to your `/test/runtests.jl`
409+
file:
411410

412411
```julia
413412
using MyJuMPExtension
414413
import JuMP
415-
include("Kokako.jl")
416-
const MODULES_TO_TEST = Kokako.include_modules_to_test(JuMP)
417-
Kokako.run_tests(
418-
MODULES_TO_TEST,
419-
MyJuMPExtension.MyModel,
420-
MyJuMPExtension.MyVariableRef;
421-
test_prefix = "test_extension_",
422-
)
414+
import Test
415+
function runtests(mod::Module)
416+
is_test_extension(f) = startswith("$f", "test_extension_")
417+
for f in filter(is_test_extension, names(mod; all = true))
418+
Test.@testset "$f" begin
419+
getfield(mod, f)(
420+
MyJuMPExtension.MyModel,
421+
MyJuMPExtension.MyVariableRef,
422+
)
423+
end
424+
end
425+
end
426+
is_test_file(f) = startswith(f, "test_") && endswith(f, ".jl")
427+
for (root, dirs, files) in walkdir(joinpath(pkgdir(JuMP), "test"))
428+
Test.@testset "$file" for file in filter(is_test_file, files)
429+
runtests(include(joinpath(root, file)))
430+
end
431+
end
423432
```
424433

425434
## Set an `optimize!` hook

src/nlp_expr.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,15 @@ function check_belongs_to_model(
553553
expr::GenericNonlinearExpr,
554554
model::AbstractModel,
555555
)
556-
for arg in expr.args
557-
if arg isa AbstractJuMPScalar
558-
check_belongs_to_model(arg, model)
556+
stack = Any[expr]
557+
while !isempty(stack)
558+
child = pop!(stack)
559+
if child isa GenericNonlinearExpr
560+
for arg in child.args
561+
push!(stack, arg)
562+
end
563+
elseif child isa AbstractJuMPScalar
564+
check_belongs_to_model(child, model)
559565
end
560566
end
561567
return

test/JuMPExtension.jl

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -605,82 +605,4 @@ function JuMP.constraints_string(print_mode, model::MyModel)
605605
]
606606
end
607607

608-
###
609-
### Tests
610-
###
611-
612-
# Test printing of models of type `ModelType` for which the model is stored in
613-
# its JuMP form, for example, as `AbstractVariable`s and `AbstractConstraint`s.
614-
# This is used by `JuMPExtension` but can also be used by external packages such
615-
# as `StructJuMP`, see https://github.com/jump-dev/JuMP.jl/issues/1711
616-
617-
function test_model_extension_printing()
618-
repl(s) = JuMP._math_symbol(MIME("text/plain"), s)
619-
620-
model_1 = MyModel()
621-
JuMP.@variable(model_1, a >= 1)
622-
JuMP.@variable(model_1, b <= 1)
623-
JuMP.@variable(model_1, -1 <= c <= 1)
624-
JuMP.@variable(model_1, a1 >= 1, Int)
625-
JuMP.@variable(model_1, b1 <= 1, Int)
626-
JuMP.@variable(model_1, -1 <= c1 <= 1, Int)
627-
JuMP.@variable(model_1, x, Bin)
628-
JuMP.@variable(model_1, y)
629-
JuMP.@variable(model_1, z, Int)
630-
JuMP.@variable(model_1, u[1:3], Bin)
631-
JuMP.@variable(model_1, fi == 9)
632-
JuMP.@objective(model_1, Max, a - b + 2a1 - 10x)
633-
JuMP.@constraint(model_1, a + b - 10c - 2x + c1 <= 1)
634-
JuMP.@constraint(model_1, a * b <= 2)
635-
JuMP.@constraint(model_1, [1 - a; u] in JuMP.SecondOrderCone())
636-
637-
model_2 = MyModel()
638-
JuMP.@variable(model_2, x, Bin)
639-
JuMP.@variable(model_2, y, Int)
640-
JuMP.@constraint(model_2, x * y <= 1)
641-
642-
model_3 = MyModel()
643-
JuMP.@variable(model_3, x)
644-
JuMP.@constraint(model_3, x <= 3)
645-
646-
Test.@test JuMP.model_string(MIME("text/plain"), model_1) == """
647-
Max a - b + 2 a1 - 10 x
648-
Subject to
649-
a + b - 10 c - 2 x + c1 $(repl(:leq)) 1
650-
a*b $(repl(:leq)) 2
651-
[-a + 1, u[1], u[2], u[3]] $(repl(:in)) MathOptInterface.SecondOrderCone(4)
652-
"""
653-
654-
Test.@test JuMP.model_string(MIME("text/latex"), model_1) == """
655-
\$\$ \\begin{aligned}
656-
\\max\\quad & a - b + 2 a1 - 10 x\\\\
657-
\\text{Subject to} \\quad & a + b - 10 c - 2 x + c1 \\leq 1\\\\
658-
& a\\times b \\leq 2\\\\
659-
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in \\text{MathOptInterface.SecondOrderCone(4)}\\\\
660-
\\end{aligned} \$\$"""
661-
662-
Test.@test sprint(show, model_1) == """
663-
An Abstract JuMP Model
664-
Maximization problem with:
665-
Variables: 13
666-
Objective function type: $(JuMP.GenericAffExpr{Float64,MyVariableRef})
667-
Constraints: 3
668-
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z"""
669-
670-
Test.@test sprint(show, model_2) == """
671-
An Abstract JuMP Model
672-
Feasibility problem with:
673-
Variables: 2
674-
Constraint: 1
675-
Names registered in the model: x, y"""
676-
677-
Test.@test sprint(show, model_3) == """
678-
An Abstract JuMP Model
679-
Feasibility problem with:
680-
Variable: 1
681-
Constraint: 1
682-
Names registered in the model: x"""
683-
return
684-
end
685-
686608
end

test/Kokako.jl

Lines changed: 0 additions & 157 deletions
This file was deleted.

test/parallel_test_setup.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
#############################################################################
6+
# JuMP
7+
# An algebraic modeling language for Julia
8+
# See https://github.com/jump-dev/JuMP.jl
9+
#############################################################################
10+
11+
import JuMP
12+
import Test
13+
14+
include(joinpath(@__DIR__, "JuMPExtension.jl"))
15+
16+
function run_tests(m::Module)
17+
_startswith(k) = f -> startswith("$f", k)
18+
# Default tests
19+
for f in filter(_startswith("test_"), names(m; all = true))
20+
Test.@testset "$f" begin
21+
getfield(m, f)()
22+
end
23+
end
24+
# Test with {Float32}
25+
for f in filter(_startswith("test_extension_"), names(m; all = true))
26+
Test.@testset "$f::Float32" begin
27+
getfield(m, f)(
28+
JuMP.GenericModel{Float32},
29+
JuMP.GenericVariableRef{Float32},
30+
)
31+
end
32+
end
33+
# Test JuMP extension
34+
for f in filter(_startswith("test_extension_"), names(m; all = true))
35+
Test.@testset "$f::JuMPExtension" begin
36+
getfield(m, f)(JuMPExtension.MyModel, JuMPExtension.MyVariableRef)
37+
end
38+
end
39+
return
40+
end

0 commit comments

Comments
 (0)