# How to write a unit test for a conditional macro?

**URL:** <https://discourse.julialang.org/t/how-to-write-a-unit-test-for-a-conditional-macro/110168>\
**Category:** General Usage\
**Created:** [February 13, 2024, 6:06pm UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-for-a-conditional-macro/110168 "2024-02-13T18:06:40Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [February 13, 2024, 9:34pm UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-for-a-conditional-macro/110168/6 "2024-02-13T21:34:29Z")

</div>

> [@rssdev10](#):
>
> But I can’t force Julia to recompile the macro

I’d advise you to use the same trick as [`ToggleableAsserts.jl`](https://github.com/MasonProtter/ToggleableAsserts.jl), which I think originates from this discourse thread:

> [@@assert alternatives?](https://discourse.julialang.org/t/assert-alternatives/24775/14):
>
> You can also look at taking advantage of constant propogation to get the behavior you want. asserting() = false #making this a function results in code being invalidated and recompiled when this gets changed macro mayassert(test) esc(:(if $(@\_\_MODULE\_\_).asserting() @assert($test) end)) end f(x) = @mayassert x \< 2 If we use that in a function we can see it gets compile out julia\> @code\_llvm f(2) ; @ REPL[3]:1 within `f' define void @julia\_f\_12238(i64) { top: ret void } we can …

With this technique you explicitly toggle the behavior of your macro by redefining a function, which invalidates all code using the macro and forces a recompilation.

`ToggleableAsserts` show how easily such macros can be tested:

> <https://github.com/MasonProtter/ToggleableAsserts.jl/blob/master/test/runtests.jl>

---

_[View the full topic](https://discourse.julialang.org/t/how-to-write-a-unit-test-for-a-conditional-macro/110168)._
