Organizing Julia code

I understand that Julia projects conventionally have src and test directories. How does one connect the latter to the former? This works:

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

But perhaps there is a better way, involving some configuration file within the project?

Is there a convention for naming unit tests and integration tests? Can someone recommend a small package that I could use as a model?

Thanks

Do you want a package or just a project? Check out https://julialang.github.io/Pkg.jl/v1/environments/#Creating-your-own-projects-1 and if you’re after a package, then PkgTemplates.jl (https://github.com/invenia/PkgTemplates.jl) are the way to go.

1 Like

Usually one would be

using ThatModule

in the unit tests.

A tiny package that I like is

and may be useful as an example.

You may want to use a package template generator too, eg

2 Likes

As a starting point,
I typically mirror the folder structure of src in test.
So if i have files: src/foo.jl and src/bar.jl I will also have test/foo.jl and test/bar.jl.

then i might also add seperate files on top for integration testing,
or i might not.
Depending on the project

2 Likes

I’ve had a look at UnPack.jl and am still confused. Under UnPack.jl, the test directory has a file called runtests.jl, which begins with

using UnPack

When I execute that file (julia runtests.jl), I get

[Alans-MacBook-Air test]$ julia runtests.jl
ERROR: LoadError: ArgumentError: Package UnPack not found in current path:
- Run `import Pkg; Pkg.add("UnPack")` to install the UnPack package.

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:892
 [2] include(::Module, ::String) at ./Base.jl:377
 [3] exec_options(::Base.JLOptions) at ./client.jl:288
 [4] _start() at ./client.jl:484
 in expression starting at /Users/rogers/distrib/UnPack/test/runtests.jl:2

Clearly, the Project.toml file is not enough to tell Julia how to find the source code. Do I need to install or activate this package using the package manager? If I do that, how can I be sure I’m testing the local code (in …/src) rather than a version installed elsewhere by Pkg?

Thanks

Workflow is like this:

  1. Run ]activate .; instantiate in the package folder.
  2. Afterwards run ]test.
3 Likes

This works from within the REPL, but from the Unix shell,

julia runtest.jl

still bombs. Is this as expected?

Thanks for the help.

Yes. You can make a script to run the tests from what I posted.

using Pkg
Pkg.activate(".");
Pkg.instantiate()
Pkg.test()

Call it “mytest.jl” and run as julia mytest.jl.

1 Like

Yes.

Also, most people use an interactive REPL for Julia, usually from some editor/IDE. If you want to run things from the command line, you will face the cost of compilation each time, so it will be frustrating way of using Julia.

1 Like