LoadError: UndefVarError: include not defined when using Pkg.test

Hello, I want to create a package A with different modules, A and Aux, and test them using pkg.test. The project structure is as follows:

#src/A.jl
include("aux.jl")
module A
export f
f(x)=x^2
end
#src/Aux.jl
module Aux
export g
g(x) = x+1
end
#test/runtests.jl
using Test, A, Aux
@test f(0)==0
@test g(0)==1

Now, if I activate my environment and test using

]> test

I get a

ERROR: LoadError: UndefVarError: include not defined

I don’t understant this behaviour. On the other hand if I put the include statement inside module A, I get a

LoadError: ArgumentError: Package aux not found in current path

Put the include inside module A. Basically, for a package, there shouldn’t really be anything outside the module of the package.

Regarding the error on using Aux, that is not surprising since the Aux module is inside A. So you can do:

using A
using A.Aux
1 Like

Thanks kristoffer, it makes sense.
Now, if I want to define and test two different modules A, B in the same package, how do I do ? For instance, I would like something like

#src/A.jl
module A
export f
f(x)=x
end

module B
export g
g(x)=x+1
end

and

#test/runtests.jl
using Test
using A,B
@test f(0)==0
@test g(0)==1

I get a

LoadError: ArgumentError: Package aux not found in current path:

  • Run import Pkg; Pkg.add("B") to install the aux package.

Your package is A here. And note:

You can define a module B inside A as:

module A

module B
end

end

You can only test a full package, you cannot test a submodule of a package. Then you would need to make B its own package.

Thanks. So a single package, say A, can only define a single module of the same name A ?

Yes. Everything else has to be inside A.

1 Like