Pkg `target` based test specific dependencies just doesn't work?

I’m very new to Julia, and I’ve been trying to get some unit tests up with TestItemRunner.jl. Following the recommended way to specify test dependencies (my account is too new to link, but this is following the Pkg.jl docs):

Project.toml:

name = "mvce"
uuid = "5c5c69df-07ce-4eec-9db9-de18ccd496a9"
authors = ["Peter Hill <peter.hill@york.ac.uk>"]
version = "0.1.0"

[extras]
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

[targets]
test = ["TestItems", "TestItemRunner"]

and test/runtests.jl

using TestItemRunner

and running from the command line:

$ julia --project=. test/runtests.jl
ERROR: LoadError: ArgumentError: Package TestItemRunner not found in current path.
- Run `import Pkg; Pkg.add("TestItemRunner")` to install the TestItemRunner package.

If I instead use the alternative, possibly unstable test/Project.toml method:

Project.toml:

name = "mvce"
uuid = "5c5c69df-07ce-4eec-9db9-de18ccd496a9"
authors = ["Peter Hill <peter.hill@york.ac.uk>"]
version = "0.1.0"

test/Project.toml:

[dep]
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

Then after activating the project and test environments and instantiating inside the REPL, I can run:

$ julia --project=test test/runtests.jl

which works.

What am I missing? Does the target based approach just not work? Have I missed a step? Is the test/Project.toml the recommended way?

The [targets] approach is (as far as I’m aware) only compatible with using ]test in the REPL (or explicitly calling the Pkg.test API). If you want to run your tests as a standalone “script”, using a dedicated test Project is the best approach.

1 Like

Thanks, that does indeed work! That is not at all clear from the docs, so I will make an issue on Pkg.jl

1 Like

One other way is to use TestEnv.jl to create the test environment. That works with the targets approach, and allows rerunning tests in the same Julia session (unlike Pkg.test which starts a separate process).

There’s an open issue for adding support for Pkg to activate these environments without the need for TestEnv.jl here TestEnv.jl functionality/support · Issue #3460 · JuliaLang/Pkg.jl · GitHub

Something like this might be a good design:

pkg> activate Foo --target test

or

pkg> activate Foo --target build

which is what Pkg.build uses, if present.

There is a function that might do most of the work already here

I started implementing it here Add ability to activate test/build target envs by IanButterworth · Pull Request #4218 · JuliaLang/Pkg.jl · GitHub