# Inheriting package dependencies in test environment

**URL:** https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505
**Category:** General Usage
**Tags:** question, package
**Created:** [September 21, 2021, 6:20am UTC](https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505 "2021-09-21T06:20:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![madhavkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/madhavkrishnan/32/29368_2.png) [@madhavkrishnan](https://discourse.julialang.org/u/madhavkrishnan)
#### Post date: [September 21, 2021, 6:20am UTC](https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505/1 "2021-09-21T06:20:31Z")

</div>

I am trying to write a test module for a package I am developing. However it looks like I don’t understand exactly how to set it up. My understanding was that the test environment would automatically inherit the packages used by the parent package but when I run test from the Pkg prompt I get an error that the required package is not available in this path. I have written a MWE below,

My package has the following files

Project.toml

```julia
name = "MyPkg"
uuid = "a9a052b8-e4c5-4edf-a254-c280e2630dd3"
authors = [" ****** ######"]
version = "0.1.0"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

```

src\MyPkg.jl

```julia
module MyPkg

using Random

function random()
    x = rand(1:5)
    return x
end

export random

end # module

```

test\Project.toml

```julia
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

```

test\runtests.jl

```julia
using Test

include("../src/MyPkg.jl")

@test MyPkg.random() <= 5

```

Now when I run,

```julia
pkg> activate .

(MyPkg) pkg> test

```

I get the error,

```julia
    Testing MyPkg
Status `C:\Users\ **** \AppData\Local\Temp\jl_T5Nx7K\Project.toml`
  [a9a052b8] MyPkg v0.1.0 `C:\Users\ ****** \MyPkg`
  [8dfed614] Test
Status `C:\Users\ ***** \AppData\Local\Temp\jl_T5Nx7K\Manifest.toml`
  [a9a052b8] MyPkg v0.1.0 `C:\Users\ **** \MyPkg`
  [2a0f44e3] Base64
  [8ba89e20] Distributed
  [b77e0a4c] InteractiveUtils
  [56ddb016] Logging
  [d6f4376e] Markdown
  [9a3f8284] Random
  [9e88b42a] Serialization
  [6462fe0b] Sockets
  [8dfed614] Test
ERROR: LoadError: LoadError: ArgumentError: Package Random not found in current path:
- Run `import Pkg; Pkg.add("Random")` to install the Random package.

```

I don’t want to add the packages again to the test environment because, there is a build step that modifies one of the imported package’s behaviour which would break if it is imported again. Could someone tell me what I am missing here?

---

<div class="post-metadata">

### Author: ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)
#### Post date: [September 21, 2021, 6:45am UTC](https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505/2 "2021-09-21T06:45:43Z")

</div>

If you replace this:

> [@madhavkrishnan](#):
>
> ```julia
> include("../src/MyPkg.jl")
> 
> ```

with

```julia
using MyPkg

```

The dependencies of `MyPkg` should be loaded correctly when running tests.

---

<div class="post-metadata">

### Author: ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)
#### Post date: [September 21, 2021, 7:07am UTC](https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505/3 "2021-09-21T07:07:53Z")

</div>

To explain a bit more what is going on, when you do

```julia
(MyPkg) pkg> test

```

the code in `runtests.jl` is run in an environment which has `MyPkg` and anything in your test dependencies (`test/Project.toml` in this case). The dependencies of `MyPkg` (and indeed the dependencies of the packages listed in `test/Project.toml`) are installed, but not available for use by `using PackageName`.

This

```julia
include("../src/MyPkg.jl")

```

defined `MyPkg` as a new module rather than importing it and tried to do `using Random`. But `Random` was not a top level dependency in the test environment and therefore this failed.

---

<div class="post-metadata">

### Author: ![madhavkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/madhavkrishnan/32/29368_2.png) [@madhavkrishnan](https://discourse.julialang.org/u/madhavkrishnan)
#### Post date: [September 21, 2021, 7:12am UTC](https://discourse.julialang.org/t/inheriting-package-dependencies-in-test-environment/68505/4 "2021-09-21T07:12:46Z")

</div>

Thanks @mmiller that is exactly what I needed to understand. I tried using _mainmodule_ in my actual code but it failed due to other reasons which I could track down and fix now. Thanks a lot.
