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

**URL:** https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552
**Category:** New to Julia
**Created:** [February 7, 2019, 3:11pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552 "2019-02-07T15:11:00Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [February 7, 2019, 3:11pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/1 "2019-02-07T15:11:00Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![tanhevg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tanhevg/32/12025_2.png) [@tanhevg](https://discourse.julialang.org/u/tanhevg)
#### Post date: [February 7, 2019, 3:29pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/2 "2019-02-07T15:29:49Z")

</div>

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:

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

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

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

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

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 7, 2019, 3:40pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/3 "2019-02-07T15:40:31Z")

</div>

tanhevg is correct, see also [5. Creating Packages · Pkg.jl](https://julialang.github.io/Pkg.jl/v1/creating-packages/#Test-specific-dependencies-1).

---

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [February 7, 2019, 3:49pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/4 "2019-02-07T15:49:57Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 7, 2019, 3:51pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/5 "2019-02-07T15:51:00Z")

</div>

Yea, that is what I do.

---

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [February 7, 2019, 9:24pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/6 "2019-02-07T21:24:14Z")

</div>

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

If my `.travis.yml` file reads

```yaml
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,

```julia
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.

```julia
 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?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 7, 2019, 9:27pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/7 "2019-02-07T21:27:15Z")

</div>

Maybe you ran into [https://github.com/JuliaLang/Pkg.jl/issues/876](https://github.com/JuliaLang/Pkg.jl/issues/876).

---

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [February 8, 2019, 11:54am UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/8 "2019-02-08T11:54:07Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 8, 2019, 12:02pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/9 "2019-02-08T12:02:43Z")

</div>

> [@timueh](#):
>
> Does that mean that with some new version of `Pkg.jl` it will be fixed?

Yea.

---

<div class="post-metadata">

### Author: ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)
#### Post date: [April 18, 2019, 4:47pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/10 "2019-04-18T16:47:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [April 18, 2019, 4:48pm UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/11 "2019-04-18T16:48:27Z")

</div>

> [@jdlara-berkeley](#):
>
> Are the newer versions of Pkg shipped only with new Julia releases?

Yes.

> [@jdlara-berkeley](#):
>
> Is it possible to update Pkg?

Not really.

---

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [November 18, 2019, 1:38am UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/12 "2019-11-18T01:38:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 18, 2019, 6:24am UTC](https://discourse.julialang.org/t/add-package-to-project-toml-when-needed-only-for-testing/20552/13 "2019-11-18T06:24:19Z")

</div>

> [@dpo](#):
>
> How does one specify min/max versions for test dependencies using the “1.0-1.1 method”?

You kinda can’t.

> [@dpo](#):
>
> If one uses the “1.2 method” involving a `test/Project.toml` , can that file contain a `[compat]` section?

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