Running a single test script

I want to be able to run a single test just by including it. For example, imagine the following test file:

using Test, BenchmarkTools

@test 1+1 == 2

If I include it directly, it fails:

include("test/test_example.jl"
ERROR: LoadError: ArgumentError: Package BenchmarkTools not found in current path.
- Run 'import Pkg; Pkg.add("BenchmarkTools")' to install the BenchmarkTools package.
Stacktrace:

Now I modified it:

using Pkg

Pkg.activate(@__DIR__)

using Test, BenchmarkTools

@test 1+1 == 2

And it works:

julia> include("test/test_example.jl")
  Activating project at '~/repos/SymbolicAWEModels.jl/test'
Test Passed

But Github Copilot tells me this would be a problematic approach:

`Pkg.activate(@__DIR__)` at top-level will override the temporary test environment that `Pkg.test()` sets up (used in CI via `julia-actions/julia-runtest`). That can change dependency resolution/version selection mid-test run and make CI behavior non-reproducible. Consider activating only when this file is executed directly (e.g., based on `PROGRAM_FILE`), or handle `--project=test` selection in the runner script instead and remove per-file activation during `Pkg.test()` runs.

Is this approach problematic, or can I ignore this warning?

And why is the temporary test environment different from the environment in the test folder?

I use the workspace feature:

[workspace]

projects = ["docs", "examples", "scripts", "test"]

and have a separate Project.toml file in the test folder, which - for example - contains BenchmarkTools.jl.

With the workspace setup you can just start Julia with

julia --project=test

and then include your test file.

I know that, but I want to stay in the REPL as long as possible and want to be able to run single tests by just including the test file. I do the same with the examples in the example folder. I also want to be able to run them without having to activate the examples project explicitly.

Furthermore, I am using a custom system image, and then the command you shared would not be sufficient.

In that case you can proceed like you started with Pkg.activate(@__DIR__) in every file. You can safely ignore CoPilot’s advice, it’s only relevant if you’re using the old approach with

[targets]
test = [...]

in Project.toml.

TestEnv.activate() is the universal way to activate test environemnt – always works.

But that requires the package TestEnv to be installed. And it is usually broken for Julia versions that are not yet released.

I use now:

using Pkg
if abspath(PROGRAM_FILE) == abspath(@__FILE__)
    Pkg.activate(@__DIR__)
end

to make Copilot happy.

And my CI tests pass now. Without this guard, one test failed on Julia 1.11.

Indeed, I assumed released Julia versions, not some preliminary ones – didn’t know TestEnv doesn’t work on pre-versions.

Hmm, does this mean there is no universal way to properly activate test environment in Julia, something that would work all the way from LTS to pre-releases?

I used to use TestEnv a lot, but the sub-projects available since Julia 1.12 are better. So I am switching away from TestEnv. I need multiple separate sub-projects, for testing, examples, docs etc.

And the approach above works good enough for me.