# How to test what happens in a function deep in the call stack

**URL:** <https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120>\
**Category:** General Usage\
**Tags:** testing\
**Created:** [May 10, 2024, 6:51pm UTC](https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120 "2024-05-10T18:51:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 10, 2024, 6:51pm UTC](https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120/1 "2024-05-10T18:51:55Z")

</div>

I was wondering how I could improve tests for certain packages of mine, let’s take Makie as an example. Makie turns input data to output images but only testing reference images is not satisfactory. We have lots of internal functions that are worth testing, however often it’s reasonably annoying to create the input data they need, as those are usually prepared by pipeline functions further upstream. And we don’t want to copy pipeline implementation into the tests, because that’s a source for errors.

So my question is, if I have functions f, g, h which call each other, but h returns to g and g to f, and the output of f is not of interest. How can I test the output of h given some input for f? Like temporarily overwriting h with a special version that I can grab the output of.

---

<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 10, 2024, 7:08pm UTC](https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120/2 "2024-05-10T19:08:25Z")

</div>

It seems like some kind of mocking might be appropriate here.

I never have used it in Julia, but this says it does what I mean:

> **[GitHub - JuliaTesting/Mocking.jl: Allows Julia function calls to be...](https://github.com/JuliaTesting/Mocking.jl)**
>
> Allows Julia function calls to be temporarily overloaded for purpose of testing - JuliaTesting/Mocking.jl

> **[Mocking in Unit Tests - Engineering Fundamentals Playbook](https://microsoft.github.io/code-with-engineering-playbook/automated-testing/unit-testing/mocking/)**
>
> ISE Engineering Fundamentals Engineering Playbook

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 10, 2024, 9:14pm UTC](https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120/3 "2024-05-10T21:14:36Z")

</div>

Yeah that looks very applicable, the main drawback seems to be that one has to apply `@mock` to all callsites one is interested in. I never really use packages meant for testing that require to instrument the actual source code

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 10, 2024, 9:20pm UTC](https://discourse.julialang.org/t/how-to-test-what-happens-in-a-function-deep-in-the-call-stack/114120/4 "2024-05-10T21:20:30Z")

</div>

I found [GitHub - JuliaTesting/SimpleMock.jl: A basic mocking module](https://github.com/JuliaTesting/SimpleMock.jl) but it seems to be broken after 1.6 sadly
