Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ function test(
coverage = false, test_fn = nothing,
julia_args::Union{Cmd, AbstractVector{<:AbstractString}} = ``,
test_args::Union{Cmd, AbstractVector{<:AbstractString}} = ``,
testsets::Union{Nothing, AbstractVector} = nothing,
force_latest_compatible_version::Bool = false,
allow_earlier_backwards_compatible_versions::Bool = true,
allow_reresolve::Bool = true,
Expand All @@ -542,6 +543,7 @@ function test(
test_fn,
julia_args,
test_args,
testsets,
force_latest_compatible_version,
allow_earlier_backwards_compatible_versions,
allow_reresolve,
Expand Down
25 changes: 22 additions & 3 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2235,12 +2235,30 @@ function free(ctx::Context, pkgs::Vector{PackageSpec}; err_if_free = true)
end
end

function gen_test_code(source_path::String; test_args::Cmd)
function gen_test_code(source_path::String; test_args::Cmd, testsets::Union{Nothing, AbstractVector} = nothing)
test_file = testfile(source_path)

# Generate testset filtering setup if testsets are specified
testset_setup = if testsets !== nothing && !isempty(testsets)
"""
# Set up testset filtering
using Test
testsets_discovered = Test.discover_testsets($(repr(test_file)))
testset_filter = Test.create_testset_filter(testsets_discovered, $(repr(collect(testsets))))
Test.TESTSET_FILTER[] = testset_filter
println("Testset filtering active: \$(length(testset_filter.enabled_testset_ids)) testsets enabled from patterns \$(testset_filter.enabled_patterns)")
println("Enabled testsets:")
Test.show_tree(testsets_discovered, enabled_only=true)
"""
else
""
end

return """
$(Base.load_path_setup_code(false))
cd($(repr(dirname(test_file))))
append!(empty!(ARGS), $(repr(test_args.exec)))
$testset_setup
include($(repr(test_file)))
"""
end
Expand Down Expand Up @@ -2523,6 +2541,7 @@ function test(
ctx::Context, pkgs::Vector{PackageSpec};
coverage = false, julia_args::Cmd = ``, test_args::Cmd = ``,
test_fn = nothing,
testsets::Union{Nothing, AbstractVector} = nothing,
force_latest_compatible_version::Bool = false,
allow_earlier_backwards_compatible_versions::Bool = true,
allow_reresolve::Bool = true
Expand Down Expand Up @@ -2583,7 +2602,7 @@ function test(

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

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

printpkgstyle(ctx.io, :Testing, "Running tests...")
flush(ctx.io)
code = gen_test_code(source_path; test_args)
code = gen_test_code(source_path; test_args, testsets)
cmd = `$(Base.julia_cmd()) --threads=$(get_threads_spec()) $(flags) --eval $code`
p, interrupted = subprocess_handler(cmd, ctx.io, "Tests interrupted. Exiting the test process")
if success(p)
Expand Down
22 changes: 22 additions & 0 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,17 @@ const update = API.up
- `allow_reresolve::Bool=true`: allow Pkg to reresolve the package versions in the test environment
- `julia_args::Union{Cmd, Vector{String}}`: options to be passed the test process.
- `test_args::Union{Cmd, Vector{String}}`: test arguments (`ARGS`) available in the test process.
- `testsets::Union{Nothing, AbstractVector}=nothing`: patterns to match against testset names for selective test execution.

!!! compat "Julia 1.9"
`allow_reresolve` requires at least Julia 1.9.

!!! compat "Julia 1.9"
Passing a string to `coverage` requires at least Julia 1.9.

!!! compat "Julia 1.14"
The `testsets` keyword argument requires at least Julia 1.14.

Run the tests for the given package(s), or for the current project if no positional argument is given to `Pkg.test`
(the current project would need to be a package). The package is tested by running its `test/runtests.jl` file.

Expand Down Expand Up @@ -401,6 +405,24 @@ which could be enabled by testing with
```julia
Pkg.test("foo"; test_args=["--extended"])
```

The `testsets` keyword argument allows selective execution of testsets based on pattern matching:
```julia
# Run testsets with exact name "network" (case-sensitive)
Pkg.test("foo"; testsets=["network"])

# Run testsets matching multiple exact patterns
Pkg.test("foo"; testsets=["network", "database"])

# Use regex for flexible pattern matching
using Test
Pkg.test("foo"; testsets=[r"network.*test"])

# Use case-insensitive regex matching
Pkg.test("foo"; testsets=[r"network"i])
```
When testsets are specified, only the matching testsets and their parent/child testsets will be executed.
String patterns use exact matching (case-sensitive), while regex patterns use flexible substring matching.
"""
const test = API.test

Expand Down
Loading