# Using dependency package in doctests

**URL:** <https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262>\
**Category:** New to Julia\
**Tags:** documenter\
**Created:** [May 16, 2021, 10:10pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262 "2021-05-16T22:10:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 16, 2021, 10:10pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/1 "2021-05-16T22:10:41Z")

</div>

I’ve created a package and successfully pushed CI-Documenter-generated docs on its github page, but I’m having trouble adding doctests. They currently fail because of a mismatch in the output, and I’m not sure how I should resolve it.

Suppose the function I want documented is:

````julia
"""
    euler_active(φ::Real, θ::Real, ψ::Real)

3D rotation matrix

- `φ`: Euler angle (longitude, in [0,2π])
- `θ`: Euler angle (colatitude, in [0,2π])
- `ψ`: Euler angle (rotation around z", in [0,2π])

# Examples

```jldoctest
julia> using StaticArrays
julia> euler_active(π/2,0,0)
3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
  6.12323e-17 -1.0 0.0
  1.0 6.12323e-17 0.0
 -0.0 0.0 1.0
```

"""
function euler_active(φ, θ, ψ = 0)

    cosφ = cos(φ)
    cosψ = cos(ψ)
    cosθ = cos(θ)
    sinφ = sin(φ)
    sinψ = sin(ψ)
    sinθ = sin(θ)

    # filled by columns
    R = SMatrix{3,3}(
        cosφ * cosθ * cosψ - sinφ * sinψ,
        sinφ * cosθ * cosψ + cosφ * sinψ,
        -sinθ * cosψ,
        -cosφ * cosθ * sinψ - sinφ * cosψ,
        -sinφ * cosθ * sinψ + cosφ * cosψ,
        sinθ * sinψ,
        cosφ * sinθ,
        sinφ * sinθ,
        cosθ,
    )

    return R

end

````

My package’s main module loads StaticArrays (used throughout), with `using StaticArrays`, so I never need to prefix its functions.

The error I’m getting on github’s CI side is:

````julia
┌ Error: doctest failure in ~/work/CoupledDipole.jl/CoupledDipole.jl/src/Utils.jl:15-22
│ 
│ ```jldoctest
│ julia> using StaticArrays
│ julia> euler_active(π/2,0,0)
│ 3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
│ 6.12323e-17 -1.0 0.0
│ 1.0 6.12323e-17 0.0
│ -0.0 0.0 1.0
│ ```
│ 
│ Subexpression:
│ 
│ euler_active(π/2,0,0)
│ 
│ Evaluated output:
│ 
│ 3×3 StaticArrays.SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
│ 6.12323e-17 -1.0 0.0
│ 1.0 6.12323e-17 0.0
│ -0.0 0.0 1.0
│ 
│ Expected output:
│ 
│ using StaticArrays
│ 
│ diff =
│ Warning: Diff output requires color.
│ using StaticArrays3×3 StaticArrays.SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
│ 6.12323e-17 -1.0 0.0
│ 1.0 6.12323e-17 0.0
│ -0.0 0.0 1.0

````

How should I remedy this? Ideally all docs examples would be run in an environment where StaticArrays is loaded, but I don’t really see where to add this. Also curious about resolving the colour issue.

---

<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:** [May 16, 2021, 10:24pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/2 "2021-05-16T22:24:17Z")

</div>

It looks like you’re running into [#944](https://github.com/JuliaDocs/Documenter.jl/issues/944). It should be enough if you add

```julia
using StaticArrays

```

at the top of `make.jl`.

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 16, 2021, 11:28pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/3 "2021-05-16T23:28:22Z")

</div>

Thanks – I’m still getting failures though. Do I understand correctly that doctests embedded in the docstrings also get tested outside Documenter’s make.jl process? I can’t work it out but locally

```julia
$ julia make.jl 
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.

```

works fine, while in github’s CI I get all kinds of error when it’s running

```julia
Run julia --project=docs -e '
  julia --project=docs -e '
    using Documenter: DocMeta, doctest
    using CoupledDipole
    DocMeta.setdocmeta!(CoupledDipole, :DocTestSetup, :(using CoupledDipole); recursive=true)
    doctest(CoupledDipole)'
  shell: /usr/bin/bash -e {0}
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
┌ Error: doctest failure in ~/work/CoupledDipole.jl/CoupledDipole.jl/src/Materials.jl:182-189

```

---

<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:** [May 16, 2021, 11:42pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/4 "2021-05-16T23:42:05Z")

</div>

Ah, yes, you’re using a separate CI step for doctests which doesn’t use `make.jl`. You need to add `using StaticArrays` to the workflow file too, something like:

```julia
  julia --project=docs -e '
    using Documenter: DocMeta, doctest
    using StaticArrays
    using CoupledDipole
...

```

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 16, 2021, 11:58pm UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/5 "2021-05-16T23:58:52Z")

</div>

Thanks – I found this workflow file and added `using ...` but still no luck 😕

 ![Screen Shot 2021-05-17 at 11.57.22 AM](https://global.discourse-cdn.com/julialang/original/3X/6/2/62cf4730dd798cf39e493e49e2ed46491dae8486.png)

---

<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:** [May 17, 2021, 4:14am UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/6 "2021-05-17T04:14:11Z")

</div>

What if you also put it into the `setdocmeta!` call? So have `:(using CoupledDipole, StaticArrays)` as the third argument?

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 17, 2021, 5:06am UTC](https://discourse.julialang.org/t/using-dependency-package-in-doctests/61262/7 "2021-05-17T05:06:08Z")

</div>

Thanks, I think that did it!  
I’d tried that by itself earlier, but apparently it was the combination of all the previous tips on this thread that was needed.

Now I just need to figure out how to deal with approximate (floating point) equalities in these tests 🙂
