# Testing for warnings emitted at macro expansion time

**URL:** <https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091>\
**Category:** General Usage\
**Tags:** question, testing, metaprogramming\
**Created:** [March 8, 2026, 2:40pm UTC](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091 "2026-03-08T14:40:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![impact-basin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impact-basin/32/220810_2.png) [@impact-basin](https://discourse.julialang.org/u/impact-basin)\
**Post date:** [March 8, 2026, 2:40pm UTC](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091/1 "2026-03-08T14:40:22Z")

</div>

Hi all,

I’m working on some macros for PikaParser.jl grammars (very WIP!), and I had a question regarding their testing.

One thing I’m trying to do is build the grammar at macro-time; this allows us to return a lambda function applying the parser with rules into the caller, which _should_ allow for precompilable parsers. Because the user may be calling helper functions, this requires an `@eval` into ` __module__ `.

However, this requires symbols to be resolvable at module top-level. If this errors (e.g. because helper functions were defined in a local scope), we recover by issuing a warning about the offending symbol, and defer the grammar build to runtime. That warning is line 74 of [this file](https://github.com/impact-basin/PikaParser.jl/blob/master/src/macros.jl).

Now, this all works fairly well, and I am writing test-cases. Naively, I expected a `@test_warn` to work; after some digging, I also tried with `@test_logs` and by constructing a `TestLogger` directly.

The fundamental issue is that the `@warn` invoke happens at macro-time, where `stdout`/`stderr` is not captured by the test suite. That test happens [here](https://github.com/impact-basin/PikaParser.jl/blob/master/test/macros.jl) – specifically, at the `@syntax` macro invoke at line 54.

What are the options here? I would very much like to ensure that this warning is occurring as expected. Should I redirect output streams at macro-expansion time?

Thanks all in advance!

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 8, 2026, 4:14pm UTC](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091/2 "2026-03-08T16:14:57Z")

</div>

> [@impact-basin](#):
>
> That test happens [here](https://github.com/impact-basin/PikaParser.jl/blob/master/test/macros.jl) – specifically, at the `@syntax` macro invoke at line 54.

For the record, it’s always better to provide [permanent links to files](https://docs.github.com/en/repositories/working-with-files/using-files/getting-permanent-links-to-files) or [code snippets](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-a-permanent-link-to-a-code-snippet) (and it looks like the line number already change since you posted your message):

> <https://github.com/impact-basin/PikaParser.jl/blob/63a266ae05c6b559d4166b161b7d88e4305a8794/test/macros.jl#L51-L62>

---

<div class="post-metadata">

**Author:** ![impact-basin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impact-basin/32/220810_2.png) [@impact-basin](https://discourse.julialang.org/u/impact-basin)\
**Post date:** [March 8, 2026, 9:07pm UTC](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091/3 "2026-03-08T21:07:56Z")

</div>

Ah, yes, probably 🙂 I don’t often collaborate with others on code, so my version-control practices are dubious at best!

I’ve just allowed turning off the warning at macro invoke; this solves the issue for me, while letting me test by inspecting the result of `@macroexpand`.

As it happens, the relevant test line is still at 54:

```julia
syntax_exprs = @macroexpand P.@syntax :top begin
    :ws => many(satisfy(myisspace))
    :number => some(:digit => satisfy(myisdigit))
    :top => seq(:ws, :number)
end true

```
