Inheriting package dependencies in test environment

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


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

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


module MyPkg

using Random

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

export random

end # module


Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"


using Test


@test MyPkg.random() <= 5

Now when I run,

pkg> activate .

(MyPkg) pkg> test

I get the error,

    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?

If you replace this:


using MyPkg

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


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

(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.



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.


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.

1 Like