# Setup for Module/Package Testing

**URL:** <https://discourse.julialang.org/t/setup-for-module-package-testing/49236>\
**Category:** Performance\
**Tags:** pkg, revise\
**Created:** [October 29, 2020, 2:07pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236 "2020-10-29T14:07:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [October 29, 2020, 2:07pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/1 "2020-10-29T14:07:15Z")

</div>

I’m developing a private package and I’m curious what the recommend process is. I started with the idea of using **Pkg.develop(path=“path to my package”)** but I’ve noticed that with this method I often need to restart the REPL because Revise is unable to revise the package in memory. With changes to the package and restarting the REPL I then get a package pre-compilation which takes some time. Therefore in development, specially in early stages, I’m wasting a lot of time in pre-compilation.

Another idea then is to not use **Pkg.develop()** but instead just use **include(“path to my main module”)**. Then in my test script I use the following (name of main module being AM2App)

```julia
if !haskey(Pkg.installed(),"AM2App")
    include("../src/AM2App.jl")
    using .AM2App
else
    using AM2App
end
using Test

```

In development mode using the include(“module path”) seems to be a much faster because I can skip the pre-compilation step. Also the Module is easily replaced each time I run which means I don’t need to restart the REPL.

Am I going down the right path here?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 29, 2020, 2:25pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/2 "2020-10-29T14:25:04Z")

</div>

Rather than trying to `develop` the package in your global package environment, you can instead `activate` the package’s own environment. That has a few advantages:

- No need to modify your global package environment at all
- Less potential confusion about what dependencies you are using (whatever is in your package’s `Project.toml` will be used)
- Any packages you install during development will automatically be added to your package’s `Project.toml` and `Manifest.toml`, making it easy to reproduce your state later.

Activating your package’s environment just requires that you `Pkg.activate("/path/to/your/package/folder")` or `cd` into that folder and do `julia --project`. I’ve also found `Revise` to be pretty reliable in this setup. The only thing it can’t do is manage changing type definitions, which is indeed unfortunate. I usually just restart Julia in that case (or restart my Jupyter kernel) which I agree is rather annoyingly slow sometimes.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [October 29, 2020, 4:36pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/3 "2020-10-29T16:36:56Z")

</div>

Thanks, that’s a good point about the dependencies.

This method still requires package pre-compilation though, right?

Also, is it possible to somehow check which active environment Julia is using? My **if block** using **!haskey(Pkg.installed(),“AM2App”)** no longer works when I run in project mode. It would be cool if I could write my test scripts so they can work in _Project mode_ or in _include() mode_.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [November 2, 2020, 1:37am UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/4 "2020-11-02T01:37:35Z")

</div>

> [@Brad\_Carman](#):
>
> Also, is it possible to somehow check which active environment Julia is using?

`Base.active_project()` will give you the path to the `Project.toml` `Pkg` is currently using.

> [@Brad\_Carman](#):
>
> My **if block** using **!haskey(Pkg.installed(),“AM2App”)** no longer works when I run in project mode

`Pkg.installed()`, which I think is now being deprecated, do not returns the project package as installed.

> [@Brad\_Carman](#):
>
> It would be cool if I could write my test scripts so they can work in _Project mode_ or in _include() mode_ .

Well, if your tests are located in the default `test/runtests.jl` file you should be able to just do:

```julia
# test/runtests.jl
using MyPackage
using Test

# My tests ...

```

If you run the tests using `Pkg.test("MyPackage")` it will run the test in “project mode”, always using the version of `MyPackage` that the current active project refers to.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [November 4, 2020, 6:18pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/5 "2020-11-04T18:18:08Z")

</div>

What I’ve found is working best for me is to have a **run.jl** and **runtests.jl** files in my test folder.

```julia
TestApp
   src
      TestApp.jl
   test
       run.jl
       runtests.jl
       runtests_test1.jl
       runtests_test2.jl

```

Then I break up my actual testing into files like **runtests\_test1.jl** , **runtests\_test2.jl** , etc.

For **runtests.jl** I have

```julia
using TestApp

include("runtests_test1.jl")
include("runtests_test2.jl")

```

And for **run.jl** I have

```julia
include("../src/TestApp.jl")
using .TestApp

# include("runtests_test1.jl") #skip this test for now...
include("runtests_test2.jl") 

```

The advantages are:

1. I can quickly execute **run.jl** and I don’t need to wait for pre-compilation. The module is simply replaced in Main and I can quickly develop new functions and sort out simple bugs

2. I can toggle which tests I want to run by simply commenting out include statements

3. When I’m ready to test the full package or generate coverage info I can run `Pkg.test()` to run the same test scripts properly

4. I can generate trace-compile statements for PackageCompiler using:

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [December 24, 2021, 8:01pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/6 "2021-12-24T20:01:58Z")

</div>

> [@rdeits](#):
>
> I’ve also found `Revise` to be pretty reliable in this setup

This puts Revise into `Project.toml`, but my package doesn’t _really_ depend on it. Is it possible to _not_ make my package depend on Revise, but still use it during development?

EDIT: one way seems to be to use [test-specific dependencies](https://pkgdocs.julialang.org/v1/creating-packages/#Test-specific-dependencies-in-Julia-1.2-and-above)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 26, 2021, 12:46pm UTC](https://discourse.julialang.org/t/setup-for-module-package-testing/49236/7 "2021-12-26T12:46:14Z")

</div>

> [@ForceBru](#):
>
> Is it possible to _not_ make my package depend on Revise, but still use it during development?

Yeah, you just need to install Revise in your global package environment. You do that by starting Julia (without a `--project` argument) and then doing `]add Revise`.
