# How to avoid \`using MyPackage\` in every doctest?

**URL:** https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675
**Category:** General Usage
**Tags:** documenter
**Created:** [August 19, 2021, 1:37pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675 "2021-08-19T13:37:53Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 1:37pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/1 "2021-08-19T13:37:53Z")

</div>

I have a jldoctest entry which is:

```nohighlight
julia> using CellListMap

julia> box = Box([100,70,130],12)
Box{OrthorhombicCell, 3, Float64, 9}
  unit cell matrix: [100.0 0.0 0.0; 0.0 70.0 0.0; 0.0 0.0 130.0]
  cutoff: 12.0
  number of computing cells on each dimension: [10, 7, 12]
  computing cell sizes: [12.5, 14.0, 13.0] (lcell: 1)
  Total number of cells: 840

```

Do I really need `using CellListMap` there? I am getting `Box undefined` if I do not put it there, but that is a function of the package and exported by the package. I am using:

```julia
using Test, Documenter, CellListMap
doctest(CellListMap)

```

It seems that I need to use this command:

```julia
DocMeta.setdocmeta!(MyPackage, :DocTestSetup, :(using MyPackage); recursive=true)

```

Is that the only solution? Where can I put that such that I don’t have to type it every time? I tried adding it to docs/make.jl but that didn’t work. That help entry, in particular, is within the comment block above the corresponding function.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 2:18pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/2 "2021-08-19T14:18:06Z")

</div>

Actually the whole `jldoctest` workflow is not working for me. My `runtests.jl` starts with:

```julia
using CellListMap
using StaticArrays
using Test
using Documenter

DocMeta.setdocmeta!(
    CellListMap,
    :DocTestSetup,
    :(using CellListMap);
    recursive=true
)
doctest(CellListMap)

```

But I wtill get `Box undefined` in a the `jldoctest` that is:

```nohighlight
julia> box = Box([100,70,130],12)
Box{OrthorhombicCell, 3, Float64, 9}
  unit cell matrix: [100.0 0.0 0.0; 0.0 70.0 0.0; 0.0 0.0 130.0]
  cutoff: 12.0
  number of computing cells on each dimension: [10, 7, 12]
  computing cell sizes: [12.5, 14.0, 13.0] (lcell: 1)
  Total number of cells: 840

```

and this one is in a regular `.md` file of the docs.

Also, I get loads of errors of the sort:

````julia
┌ Error: doctest failure in ~/.julia/dev/CellListMap/src/testing.jl
│ 
│ ```jldoctest
│ julia> box = Box([ 50. 0. 00. 
│ 0. 30. 30.          
│ 0. 00. 50. ], 2.)
│ 
│ julia> x = 100*rand(SVector{3,Float64},10000);
│ 
│ julia> p = [CellListMap.wrap_to_first(x,box) for x in x];
│ 
│ julia> scatter(Tuple.(p),aspect_ratio=1,framestyle=:box,label=:none)
│ ```
│ 
│ Subexpression:
│ 
│ x = 100*rand(SVector{3,Float64},10000);
│ 
│ Evaluated output:
│ 
│ ERROR: UndefVarError: SVector not defined
│ Stacktrace:
│ [1] top-level scope
│ @ none:1
│ 
│ Expected output:
│ 
│ 
│ 
│ diff =
│ ERROR: UndefVarError: SVector not defined
│ Stacktrace:
│ [1] top-level scope
│ @ none:1
└ @ Documenter.DocTests ~/.julia/packages/Documenter/R2HVS/src/DocTests.jl:385

````

But none of these fields are `jldoctest`s, so they should not be evaluated. What even stranger is that the file where the error is indicated, `testing.jl` has nothing to do with that code block, which is another file (`pbc.md`), and is a standard julia block:

````julia
#```julia
julia> box = Box([ 50. 0. 00. 
                    0. 30. 30.          
                    0. 00. 50. ], 2.)

julia> x = 100*rand(SVector{3,Float64},10000);

julia> p = [CellListMap.wrap_to_first(x,box) for x in x];

julia> scatter(Tuple.(p),aspect_ratio=1,framestyle=:box,label=:none)
#```

````

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 2:25pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/3 "2021-08-19T14:25:46Z")

</div>

I would also appreciate if some points to a package where `jldoctests` are being used both in the documentation and on function comments, such that I can copy them. I looked at some examples suggested in the Documenter docs page, but at least the ones I have checked didn’t seem to be using these features.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 19, 2021, 2:45pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/4 "2021-08-19T14:45:52Z")

</div>

See  
[https://github.com/Nosferican/WebDriver.jl/blob/master/docs/make.jl](https://github.com/Nosferican/WebDriver.jl/blob/master/docs/make.jl)  
or  
[https://github.com/uva-bi-sdad/GHOST.jl/blob/main/test/runtests.jl](https://github.com/uva-bi-sdad/GHOST.jl/blob/main/test/runtests.jl)  
[https://github.com/uva-bi-sdad/GHOST.jl/blob/main/src/01\_BaseUtils.jl](https://github.com/uva-bi-sdad/GHOST.jl/blob/main/src/01_BaseUtils.jl)

Those are a few examples of `DocMeta.setdocmeta!`

---

<div class="post-metadata">

### Author: ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)
#### Post date: [August 19, 2021, 3:54pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/5 "2021-08-19T15:54:26Z")

</div>

you know, `include` is just like pasting code

```julia
using foo
include("do1.jl")

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 4:00pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/6 "2021-08-19T16:00:59Z")

</div>

@Rratic What you mean by that exactly in this context?

@Nosferican, thanks for the examples. At a first sight I do not see any difference relative to what I’m doing…

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [August 19, 2021, 4:44pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/7 "2021-08-19T16:44:55Z")

</div>

> [@lmiq](#):
>
> Where can I put that such that I don’t have to type it every time?

If you put that part in the `make.jl` file, it automatically runs before the tests. Why is that a problem still?

> [@lmiq](#):
>
> But I wtill get `Box undefined` in a the `jldoctest` that is:

Is `Box` exported? Otherwise, use `MyModule.Box`. The idea is that doctests look like it would look for an user, so the code is evaluated _outside_ `MyModule`.

> [@lmiq](#):
>
> `ERROR: UndefVarError: SVector not defined`

Also add `using SVector` to `setdocmeta!`.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/8 "2021-08-19T17:01:12Z")

</div>

> [@rikh](#):
>
> If you put that part in the `make.jl` file, it automatically runs before the tests. Why is that a problem still?

Because it does not work ☹ (see below)

> [@rikh](#):
>
> Is `Box` exported?

Yes.

> [@rikh](#):
>
> Also add `using SVector` to `setdocmeta!` .

That block is was not a `jldoctest` block, I was not expecting it to be tested. And the doctests are trying to run many blocks that are not `jldoctest` blocks and throwing a lot of errors. The errors also, strangely, keep referencing my `testing.jl` file, which not only does not contain any `jdoctest`, but neither the blocks it is complaining about. Is a file named `testing.jl` a problem in principle for any reason? (edit: now I changed all `julia` blocks to `julia-repl` blocks, and it seems that this errors disappeared. Are `julia` blocks ran by `doctests`?

This is my current `make.jl`:

```julia
import Pkg
Pkg.add("Documenter")
using Documenter
using CellListMap
DocMeta.setdocmeta!(CellListMap, :DocTestSetup, :(using CellListMap); recursive=true)
push!(LOAD_PATH,"../src/")
makedocs(
    modules=[CellListMap],
    sitename="CellListMap.jl",
    pages = [
        "Overview" => "index.md",
        "Examples" => "examples.md",
        "Periodic conditions" => "pbc.md",
        "Parallelization" => "parallelization.md",
        "Performance" => "performance.md",
        "Reference" => "reference.md",
        "Help entries" => "help.md",
    ]
)
deploydocs(
    repo = "github.com/m3g/CellListMap.jl.git",
    target = "build",
    branch = "gh-pages",
    versions = ["stable" => "v^", "v#.#"],
)

```

This is what I’m getting if I try to run the `doctests` (this is the same error I get if I run `include("make.jl")`, with the `make.jl` file above:

````julia
julia> using Documenter, CellListMap
[Info: Precompiling CellListMap [69e1c6dd-3888-40e6-b3c8-31ac5f578864]

julia> using Test

julia> DocMeta.setdocmeta!(CellListMap, :DocTestSetup, :(using CellListMap); recursive=true)

julia> doctest(CellListMap)
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
┌ Error: doctest failure in ~/.julia/dev/CellListMap/docs/src/pbc.md:7-15
│ 
│ ```jldoctest
│ julia> box = Box([100,70,130],12)
│ Box{OrthorhombicCell, 3, Float64, 9}
│ unit cell matrix: [100.0 0.0 0.0; 0.0 70.0 0.0; 0.0 0.0 130.0]
│ cutoff: 12.0
│ number of computing cells on each dimension: [10, 7, 12]
│ computing cell sizes: [12.5, 14.0, 13.0] (lcell: 1)
│ Total number of cells: 840
│ ```
│ 
│ Subexpression:
│ 
│ box = Box([100,70,130],12)
│ 
│ Evaluated output:
│ 
│ ERROR: UndefVarError: Box not defined
│ Stacktrace:
│ [1] top-level scope
│ @ none:1
│ 
│ Expected output:
│ 
│ Box{OrthorhombicCell, 3, Float64, 9}
│ unit cell matrix: [100.0 0.0 0.0; 0.0 70.0 0.0; 0.0 0.0 130.0]
│ cutoff: 12.0
│ number of computing cells on each dimension: [10, 7, 12]
│ computing cell sizes: [12.5, 14.0, 13.0] (lcell: 1)
│ Total number of cells: 840
│ 
│ diff =
│ Box{OrthorhombicCell, 3, Float64, 9}
│ unit cell matrix: [100.0 0.0 0.0; 0.0 70.0 0.0; 0.0 0.0 130.0]
│ cutoff: 12.0
│ number of computing cells on each dimension: [10, 7, 12]
│ computing cell sizes: [12.5, 14.0, 13.0] (lcell: 1)
│ Total number of cells: 840ERROR: UndefVarError: Box not defined
│ Stacktrace:
│ [1] top-level scope
│ @ none:1
└ @ Documenter.DocTests ~/.julia/packages/Documenter/R2HVS/src/DocTests.jl:385
┌ Error: Doctesting failed
│ exception =
│ `makedocs` encountered a doctest error. Terminating build
│ Stacktrace:
│ [1] error(s::String)
│ @ Base ./error.jl:33
│ [2] runner(#unused#::Type{Documenter.Builder.Doctest}, doc::Documenter.Documents.Document)
│ @ Documenter.Builder ~/.julia/packages/Documenter/R2HVS/src/Builder.jl:217
│ [3] dispatch(#unused#::Type{Documenter.Builder.DocumentPipeline}, x::Documenter.Documents.Document)
│ @ Documenter.Utilities.Selectors ~/.julia/packages/Documenter/R2HVS/src/Utilities/Selectors.jl:170
│ [4] #2
│ @ ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:257 [inlined]
│ [5] cd(f::Documenter.var"#2#3"{Documenter.Documents.Document}, dir::String)
│ @ Base.Filesystem ./file.jl:106
│ [6] makedocs(; debug::Bool, format::Documenter.Writers.HTMLWriter.HTML, kwargs::Base.Iterators.Pairs{Symbol, Any, NTuple{6, Symbol}, NamedTuple{(:root, :source, :sitename, :doctest, :modules, :doctestfilters), Tuple{String, String, String, Symbol, Vector{Module}, Vector{Regex}}}})
│ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:256
│ [7] (::Documenter.var"#all_doctests#32"{Bool, Vector{Regex}, Vector{Module}})()
│ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:849
│ [8] macro expansion
│ @ ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:870 [inlined]
│ [9] macro expansion
│ @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1151 [inlined]
│ [10] doctest(source::String, modules::Vector{Module}; fix::Bool, testset::String, doctestfilters::Vector{Regex})
│ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:870
│ [11] doctest(package::Module; manual::Bool, testset::Nothing, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
│ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:805
│ [12] doctest(package::Module)
│ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:792
│ [13] top-level scope
│ @ REPL[4]:1
│ [14] eval
│ @ ./boot.jl:360 [inlined]
│ [15] eval
│ @ ./Base.jl:39 [inlined]
│ [16] repleval(m::Module, code::Expr, #unused#::String)
│ @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.3.30/scripts/packages/VSCodeServer/src/repl.jl:157
│ [17] (::VSCodeServer.var"#69#71"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
│ @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.3.30/scripts/packages/VSCodeServer/src/repl.jl:123
│ [18] with_logstate(f::Function, logstate::Any)
│ @ Base.CoreLogging ./logging.jl:491
│ [19] with_logger
│ @ ./logging.jl:603 [inlined]
│ [20] (::VSCodeServer.var"#68#70"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
│ @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.3.30/scripts/packages/VSCodeServer/src/repl.jl:124
│ [21] #invokelatest#2
│ @ ./essentials.jl:708 [inlined]
│ [22] invokelatest(::Any)
│ @ Base ./essentials.jl:706
│ [23] macro expansion
│ @ ~/.vscode/extensions/julialang.language-julia-1.3.30/scripts/packages/VSCodeServer/src/eval.jl:34 [inlined]
│ [24] (::VSCodeServer.var"#53#54")()
│ @ VSCodeServer ./task.jl:411
└ @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:859
Doctests: CellListMap: Test Failed at /home/leandro/.julia/packages/Documenter/R2HVS/src/Documenter.jl:870
  Expression: all_doctests()
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:870 [inlined]
 [2] macro expansion
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1151 [inlined]
 [3] doctest(source::String, modules::Vector{Module}; fix::Bool, testset::String, doctestfilters::Vector{Regex})
   @ Documenter ~/.julia/packages/Documenter/R2HVS/src/Documenter.jl:870
Test Summary: | Fail Total
Doctests: CellListMap | 1 1
ERROR: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.

julia> 

````

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [August 19, 2021, 5:17pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/9 "2021-08-19T17:17:09Z")

</div>

Hmm, I should have taken a look at my pc but am at phone now. Is it maybe something with the syntax? Like missing backticks or so?

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 19, 2021, 10:28pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/10 "2021-08-19T22:28:59Z")

</div>

From reading the conversation, you want to make sure that you are exposing all the elements of the namespace you are referring to. For example, if you have `using MyPkg` and try calling `magic` which is not exposed by having `export magic` in `MyPkg.jl` you would still get that error.

What I tend to do is to run build the docs with the jldoctest as part of my test suite. For the documentation step of CI I just refer to the docs built during the testing.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 19, 2021, 11:03pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/11 "2021-08-19T23:03:57Z")

</div>

It is exported. I have no idea what’s wrong. For the moment I removed the tests. I tried to add a @meta block in the page as well, but that didn’t work either.

---

<div class="post-metadata">

### Author: ![mortenpi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mortenpi/32/158_2.png) [@mortenpi](https://discourse.julialang.org/u/mortenpi)
#### Post date: [August 20, 2021, 7:23am UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/12 "2021-08-20T07:23:31Z")

</div>

Looking at this error, the doctest is in a Markdown file? In that case, you need a [`DocTestSetup` in an at-meta block](https://juliadocs.github.io/Documenter.jl/stable/man/doctests/#DocTestSetup-in-@meta-blocks) in the same `.md` file before the doctest. That should fix this particular error at least.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 20, 2021, 9:35am UTC](https://discourse.julialang.org/t/how-to-avoid-using-mypackage-in-every-doctest/66675/13 "2021-08-20T09:35:39Z")

</div>

I tried that as well and got the same error. I will have to build a mwe when I have time.
