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


#1

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?


#2

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"]

#3

tanhevg is correct, see also https://julialang.github.io/Pkg.jl/v1/creating-packages/#Test-specific-dependencies-1.


#4

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!


#5

Yea, that is what I do.


#6

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?


#7

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


#8

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?


#9

Yea.