Add package to `project.toml` when needed only for testing?

Hi there,

I am currently writing a Julia package. All dependencies are stored in the project.toml file. Now I am writing a test file to be called by runtests.jl. Only for this testing I now need another package—let’s call it PackageForTesting.jl—which will provide the ground truth to which I can compare my package to. Do I add PackageForTesting.jl now to project.toml?

To be crystal-clear: the exported functions of my package do not require PackageForTesting.jl. It is only the testing file that needs PackageForTesting.jl to have some values to compare against.

So, add PackageForTesting.jl to project.toml or not?

3 Likes

You can do this, as long as you only test your package via Pkg.test("MyPackage"), i.e. not run runtest.jl directly. To accomplish this, Project.toml needs to be modified as follows:

name = "MyPackage"
uuid = "..."
authors = [...]
version = "0.1.0"

[deps]
SourceDependency1 = "..."
...

[extras]
Test = "..."
TestDependency1 = "..."
TestDependency2 = "..."

[targets]
test = ["Test", "TestDependency1", "TestDependency2"]
6 Likes

tanhevg is correct, see also 5. Creating Packages · Pkg.jl.

Thanks, that helps! I was looking exactly for this piece in the documentation of Pkg.jl. Browsing through the documentation I noticed that there is no command to add these test-specific dependencies to project.toml, i.e. to auto-generate the entries [extras]and [targets]. To get the uuids right, would this be a suitable workflow:

  1. add Dependency1 Dependency2 \rightarrow this adds to the [deps] section.
  2. Open project.toml and copy entries from [deps] for Dependency1 and Dependency2 to [extras].
  3. Still in project.toml create the entry [targets].

Thanks!

Yea, that is what I do.

The testing is fine with the workflow now, but here’s something I noticed when using Travis-CI:

If my .travis.yml file reads

language: julia

jobs:
  include:
    - stage: "Documentation"
      julia: 1.1
      os: linux
      script:
      - julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
      - julia --project=docs/ docs/make.jl
      after_success: skip

everything runs smoothly.

If I now add a test job to the .travis.yml file so that it reads like this,

language: julia
julia:
    - 1.1
jobs:
  include:
    - stage: "Documentation"
      julia: 1.1
      os: linux
      script:
      - julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
      - julia --project=docs/ docs/make.jl
      after_success: skip

then the test job will fail because the package Test was not found, i.e.

 Resolving package versions...
ERROR: LoadError: LoadError: ArgumentError: Package Test not found in current path:
- Run `import Pkg; Pkg.add("Test")` to install the Test package.

Inspecting the project.toml file afterwards, I see that the [targets] entry is missing!

Any ideas?

Maybe you ran into https://github.com/JuliaLang/Pkg.jl/issues/876.

I guess I did. I see the issue is closed now. Does that mean that with some new version of Pkg.jl it will be fixed?

Yea.

Are the newer versions of Pkg shipped only with new Julia releases? is it possible to update Pkg?

Yes.

Not really.

1 Like

How does one specify min/max versions for test dependencies using the “1.0-1.1 method”?

If one uses the “1.2 method” involving a test/Project.toml, can that file contain a [compat] section?

Thanks.

You kinda can’t.

Yes, but that method is pretty beta so just be aware of that.

1 Like