# Testing for correct handling of (missing) globals

**URL:** <https://discourse.julialang.org/t/testing-for-correct-handling-of-missing-globals/126158>\
**Category:** General Usage\
**Tags:** testing, distributed\
**Created:** [February 21, 2025, 4:09pm UTC](https://discourse.julialang.org/t/testing-for-correct-handling-of-missing-globals/126158 "2025-02-21T16:09:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rbthn](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@Rbthn](https://discourse.julialang.org/u/Rbthn)\
**Post date:** [February 21, 2025, 4:09pm UTC](https://discourse.julialang.org/t/testing-for-correct-handling-of-missing-globals/126158/1 "2025-02-21T16:09:46Z")

</div>

When writing a script to e.g. solve an ODE, I often find myself using global (const) variables and using them in my right-hand-side function. This all works well, until I want to solve this ODE on another worker. This worker does not know the global variables referenced in the RHS function, since I forgot to declare them `@everywhere`! The corresponding code looks something like this:

```julia
const parameter = -1.0
function deriv!(du, u, p, t)
    du[:] = parameter .* u
    return nothing
end
prob = ODEProblem(deriv!, [1.0], (0.0, 1.0))

my_function(prob) # calls solve(prob) on another worker

```

As this happened to me far too often already, I want to make sure that `my_function` checks for such errors and throws an exception when this happens.  
While writing a test, I found some surprising behavior. While the above code throws an exception (as expected) when running in a script or directly in the REPL, wrapping the same code in a `@testset` does not throw an Exception. I assume that whatever I define in a `@testset` is not in the global scope, but even if I declare the parameter outside of the testset, like:

```julia
const parameter = -1.0

@testset "test" begin
function deriv!(du, u, p, t)
    du[:] = parameter .* u
    return nothing
end
prob = ODEProblem(deriv!, [1.0], (0.0, 1.0))

@test_throws Exception my_function(prob) # calls solve(prob) on another worker
end

```

my function does not throw an exception. Why is that? And how could I test whether my function responds as I expect to globals referenced but not defined on another worker?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 21, 2025, 4:30pm UTC](https://discourse.julialang.org/t/testing-for-correct-handling-of-missing-globals/126158/2 "2025-02-21T16:30:57Z")

</div>

This is exactly what `p` (the parameters) of the ODE is meant for. This example can be written as

```julia
parameter = -1.0
function deriv!(du, u, parameter, t)
    du[:] = parameter .* u
    return nothing
end
prob = ODEProblem(deriv!, [1.0], (0.0, 1.0), parameter)

```

and by doing so you make it so the external input you need is passed in explicitly.
