# Running tests on code defined in package extension

**URL:** <https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691>\
**Category:** General Usage\
**Tags:** testing, package-extensions\
**Created:** [May 31, 2023, 8:41pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691 "2023-05-31T20:41:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![FredW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredw/32/49196_2.png) [@FredW](https://discourse.julialang.org/u/FredW)\
**Post date:** [May 31, 2023, 8:41pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/1 "2023-05-31T20:41:22Z")

</div>

Hello,

Is there any way to run unit tests on methods/types defined in a package extension (but not exposed by the main package) ?

As an example, say that our package exposes only one function:  
`sample(model, rng ....)`  
We write an extension to define `sample` on a different `model` type, but for that to work we need to wrap the `model` and run a few pre-processing steps before running `sample(wrapped(model), rng...)`.  
I’d like to write some tests on the intermediate functions defined in the extension without exposing them in the main package.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 31, 2023, 9:55pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/2 "2023-05-31T21:55:53Z")

</div>

Just load the other dependent package during your tests, which will load the package extension.

---

<div class="post-metadata">

**Author:** ![FredW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredw/32/49196_2.png) [@FredW](https://discourse.julialang.org/u/FredW)\
**Post date:** [June 1, 2023, 5:46pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/3 "2023-06-01T17:46:06Z")

</div>

That wouldn’t expose the functions defined in the extension code that are not exposed in the main package

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 1, 2023, 5:53pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/4 "2023-06-01T17:53:54Z")

</div>

If you extend a function by definitions (i.e. import something and add new signatures to it) then loading both solves the problem

If you define _new_ functions in the extension, these might not even be in global scope – ever, if I understand extensions correctly for now.  
So how would you normally call these functions then? The same way you would do the test.

_edit:_ Sorry I was not careful enough in reading – you import the `sample` function in your extension so you add method definitions to it, these are actually available when loading both packages as @mkitti already wrote.

Example: Manifolds.jl has an NLSolve extension, that also does the same as yours, it extends a method by further signatures… and here  
[Manifolds.jl/approx\_inverse\_retraction.jl at master · JuliaManifolds/Manifolds.jl · GitHub](https://github.com/JuliaManifolds/Manifolds.jl/blob/master/test/approx_inverse_retraction.jl) is the test – Manifolds.jl is loaded either before or through utils.jl, so with NLSolve together the extension and its functionality is loaded and afterwards tested.

---

<div class="post-metadata">

**Author:** ![FredW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredw/32/49196_2.png) [@FredW](https://discourse.julialang.org/u/FredW)\
**Post date:** [June 1, 2023, 6:20pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/6 "2023-06-01T18:20:27Z")

</div>

Thanks for the help, that explains half of my problem. Should have given a bit more details, in my case the `sample` function is quite complicated and I split it in a few smaller functions, something like this

```julia
module MainPackage
  function sample end
  export sample
end

module MainPackageExt

 function subfn() end
 function wrapper() end

 function sample()
   subfn(...)
   wrapper(...)
 end
end

```

Just wondering if there’s a way to unit test `subfn` instead of just testing `MainPackageExt.sample`

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 1, 2023, 6:55pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/7 "2023-06-01T18:55:58Z")

</div>

if you use `import MainPackage: sample`  
as the first line within `MainPackageExt` you can call both sample functions with just `sample()` (if you happen to have a default implementation without the extension).

For `subfn` directly I am not aware how that could directly be tested, but can you issue sample-calls that end up covering all cases of `subfn`?

PS: I just saw atop the topic here – Welcome to the Julia Discourse! 🥳

PPS: In naming, the extension Names I saw until now are `MainPackageSecondPackageExt` when you main package gets extended once the second package is loaded.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 20, 2024, 9:58pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/8 "2024-01-20T21:58:25Z")

</div>

I encountered a similar issue. My workaround was to define a stub in MainPackage:

```julia
module MainPackage
  function sample end
  function subfun end 
  export sample
end

```

In the extension module, I imported `subfn`:

```julia
module MainPackageExt
 import MainPackage: subfn
 function subfn() end
 function wrapper() end

 function sample()
   subfn(...)
   wrapper(...)
 end
end

```

In my test block, I did the following:

```julia
@testset "subfn" begin 
  using MainPackage 
  using WeakDependency
  using MainPackage: subfn

  ...

end

```

That seemed to accomplish both goals: keeping `subfn` hidden from the user, but allowing it to be explicitly loaded for the purpose of testing. I don’t know if there is a better approach but this seems to check the boxes.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 20, 2024, 10:49pm UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/9 "2024-01-20T22:49:18Z")

</div>

> [@Christopher\_Fisher](#):
>
> I don’t know if there is a better approach

I’d avoid introducing names in the package if you are only doing it to test extensions, a user absolutely can access it. I would also avoid testing subfunctions so finely, I prefer tests for public names or longlasting code. But since there are always good exceptions, `Base.get_extension` gets you the module instance of a loaded extension. You still can’t import any names from it (that’s what packages are for, not extensions), but it’ll let you examine it like any module, and you can assign it or its properties to a variable for convenience. Obviously don’t abuse this to make extensions act like hampered packages if new names warrant a true package.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 21, 2024, 10:51am UTC](https://discourse.julialang.org/t/running-tests-on-code-defined-in-package-extension/99691/10 "2024-01-21T10:51:23Z")

</div>

Thank you. `Base.get_extension` is what I need.
