Skip to content

Commit 1c6c1c1

Browse files
Pkg.test: add testset filtering
1 parent 482399a commit 1c6c1c1

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/API.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ function test(
517517
coverage = false, test_fn = nothing,
518518
julia_args::Union{Cmd, AbstractVector{<:AbstractString}} = ``,
519519
test_args::Union{Cmd, AbstractVector{<:AbstractString}} = ``,
520+
testsets::Union{Nothing, AbstractVector{<:AbstractString}} = nothing,
520521
force_latest_compatible_version::Bool = false,
521522
allow_earlier_backwards_compatible_versions::Bool = true,
522523
allow_reresolve::Bool = true,
@@ -542,6 +543,7 @@ function test(
542543
test_fn,
543544
julia_args,
544545
test_args,
546+
testsets,
545547
force_latest_compatible_version,
546548
allow_earlier_backwards_compatible_versions,
547549
allow_reresolve,

src/Operations.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,12 +2235,30 @@ function free(ctx::Context, pkgs::Vector{PackageSpec}; err_if_free = true)
22352235
end
22362236
end
22372237

2238-
function gen_test_code(source_path::String; test_args::Cmd)
2238+
function gen_test_code(source_path::String; test_args::Cmd, testsets::Union{Nothing, AbstractVector{<:AbstractString}} = nothing)
22392239
test_file = testfile(source_path)
2240+
2241+
# Generate testset filtering setup if testsets are specified
2242+
testset_setup = if testsets !== nothing && !isempty(testsets)
2243+
"""
2244+
# Set up testset filtering
2245+
using Test
2246+
testsets_discovered = Test.discover_testsets($(repr(test_file)))
2247+
testset_filter = Test.create_testset_filter(testsets_discovered, $(repr(collect(testsets))))
2248+
Test.TESTSET_FILTER[] = testset_filter
2249+
println("Testset filtering active: \$(length(testset_filter.enabled_testset_ids)) testsets enabled from patterns \$(testset_filter.enabled_patterns)")
2250+
println("Enabled testsets:")
2251+
Test.show_tree(testsets_discovered, enabled_only=true)
2252+
"""
2253+
else
2254+
""
2255+
end
2256+
22402257
return """
22412258
$(Base.load_path_setup_code(false))
22422259
cd($(repr(dirname(test_file))))
22432260
append!(empty!(ARGS), $(repr(test_args.exec)))
2261+
$testset_setup
22442262
include($(repr(test_file)))
22452263
"""
22462264
end
@@ -2523,6 +2541,7 @@ function test(
25232541
ctx::Context, pkgs::Vector{PackageSpec};
25242542
coverage = false, julia_args::Cmd = ``, test_args::Cmd = ``,
25252543
test_fn = nothing,
2544+
testsets::Union{Nothing, AbstractVector{<:AbstractString}} = nothing,
25262545
force_latest_compatible_version::Bool = false,
25272546
allow_earlier_backwards_compatible_versions::Bool = true,
25282547
allow_reresolve::Bool = true
@@ -2583,7 +2602,7 @@ function test(
25832602

25842603
printpkgstyle(ctx.io, :Testing, "Running tests...")
25852604
flush(ctx.io)
2586-
code = gen_test_code(source_path; test_args)
2605+
code = gen_test_code(source_path; test_args, testsets)
25872606
cmd = `$(Base.julia_cmd()) $(flags) --eval $code`
25882607

25892608
path_sep = Sys.iswindows() ? ';' : ':'
@@ -2626,7 +2645,7 @@ function test(
26262645

26272646
printpkgstyle(ctx.io, :Testing, "Running tests...")
26282647
flush(ctx.io)
2629-
code = gen_test_code(source_path; test_args)
2648+
code = gen_test_code(source_path; test_args, testsets)
26302649
cmd = `$(Base.julia_cmd()) --threads=$(get_threads_spec()) $(flags) --eval $code`
26312650
p, interrupted = subprocess_handler(cmd, ctx.io, "Tests interrupted. Exiting the test process")
26322651
if success(p)

src/Pkg.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,17 @@ const update = API.up
352352
- `allow_reresolve::Bool=true`: allow Pkg to reresolve the package versions in the test environment
353353
- `julia_args::Union{Cmd, Vector{String}}`: options to be passed the test process.
354354
- `test_args::Union{Cmd, Vector{String}}`: test arguments (`ARGS`) available in the test process.
355+
- `testsets::Union{Nothing, Vector{Union{String,Regex}}}=nothing`: patterns to match against testset names for selective test execution.
355356
356357
!!! compat "Julia 1.9"
357358
`allow_reresolve` requires at least Julia 1.9.
358359
359360
!!! compat "Julia 1.9"
360361
Passing a string to `coverage` requires at least Julia 1.9.
361362
363+
!!! compat "Julia 1.14"
364+
The `testsets` keyword argument requires at least Julia 1.14.
365+
362366
Run the tests for the given package(s), or for the current project if no positional argument is given to `Pkg.test`
363367
(the current project would need to be a package). The package is tested by running its `test/runtests.jl` file.
364368
@@ -401,6 +405,24 @@ which could be enabled by testing with
401405
```julia
402406
Pkg.test("foo"; test_args=["--extended"])
403407
```
408+
409+
The `testsets` keyword argument allows selective execution of testsets based on pattern matching:
410+
```julia
411+
# Run testsets with exact name "network" (case-sensitive)
412+
Pkg.test("foo"; testsets=["network"])
413+
414+
# Run testsets matching multiple exact patterns
415+
Pkg.test("foo"; testsets=["network", "database"])
416+
417+
# Use regex for flexible pattern matching
418+
using Test
419+
Pkg.test("foo"; testsets=[r"network.*test"])
420+
421+
# Use case-insensitive regex matching
422+
Pkg.test("foo"; testsets=[r"network"i])
423+
```
424+
When testsets are specified, only the matching testsets and their parent/child testsets will be executed.
425+
String patterns use exact matching (case-sensitive), while regex patterns use flexible substring matching.
404426
"""
405427
const test = API.test
406428

0 commit comments

Comments
 (0)