Skip to content

Commit df498f5

Browse files
authored
Allow using Test.runtests in other packages (#2710)
1 parent e28c612 commit df498f5

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Test/Test.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ version_added(::F) where {F} = v"0.10.5" # The default for any unlabeled tests.
173173
warn_unsupported::Bool = false,
174174
exclude_tests_after::VersionNumber = v"999.0.0",
175175
verbose::Bool = false,
176+
test_module = MathOptInterface.Test,
176177
)
177178
178-
Run all tests in `MathOptInterface.Test` on `model`.
179+
Run all tests in `test_module`, which defaults to `MathOptInterface.Test`, on `model`.
179180
180181
## Configuration arguments
181182
@@ -201,6 +202,8 @@ Run all tests in `MathOptInterface.Test` on `model`.
201202
is released with a new test.
202203
* `verbose` is a `Bool` that controls whether the name of the test is printed
203204
before executing it. This can be helpful when debugging.
205+
* `test_module` is a `Module` where all the functions starting with `test_`
206+
are considered as tests.
204207
205208
See also: [`setup_test`](@ref).
206209
@@ -227,13 +230,14 @@ function runtests(
227230
warn_unsupported::Bool = false,
228231
verbose::Bool = false,
229232
exclude_tests_after::VersionNumber = v"999.0.0",
233+
test_module = @__MODULE__,
230234
)
231-
tests = filter(names(@__MODULE__; all = true)) do name
235+
tests = filter(names(test_module; all = true)) do name
232236
return startswith("$name", "test_")
233237
end
234-
tests = string.(tests)
238+
test_names = string.(tests)
235239
for ex in exclude
236-
if ex in tests && any(t -> ex != t && occursin(ex, t), tests)
240+
if ex in test_names && any(t -> ex != t && occursin(ex, t), test_names)
237241
@warn(
238242
"The exclude string \"$ex\" is ambiguous because it exactly " *
239243
"matches a test, but it also partially matches another. Use " *
@@ -242,19 +246,17 @@ function runtests(
242246
)
243247
end
244248
end
245-
for name_sym in names(@__MODULE__; all = true)
249+
for name_sym in tests
246250
name = string(name_sym)
247-
if !startswith(name, "test_")
248-
continue # All test functions start with test_
249-
elseif !isempty(include) && !any(s -> occursin(s, name), include)
251+
if !isempty(include) && !any(s -> occursin(s, name), include)
250252
continue
251253
elseif !isempty(exclude) && any(s -> occursin(s, name), exclude)
252254
continue
253255
end
254256
if verbose
255257
@info "Running $name"
256258
end
257-
test_function = getfield(@__MODULE__, name_sym)
259+
test_function = getfield(test_module, name_sym)
258260
if version_added(test_function) > exclude_tests_after
259261
if verbose
260262
println(" Skipping test because of `exclude_tests_after`")

0 commit comments

Comments
 (0)