[Proposal] Contextual data in ExUnit.assert/1 or assert/2

56 views
Skip to first unread message

Derek Wiers

unread,
Sep 15, 2025, 1:17:49 PM (11 days ago) Sep 15
to elixir-lang-core
So, we have the assert/1 macro in ExUnit, which does a really nice job of pointing out actual failures - a diff, etc, depending on what you give it. We also have assert/2, a function where you can insert your own message. I propose that we have perhaps another option: a variant of assert(assertion, contextual_data: %{foo: "bar"}). Basically, I love that diffing we do in assert/1, and just want to append some relevant data to the message, which is particularly helpful if I'm running an assertion in a loop (think poor man's property-based testing) with different data -- the line number isn't going to allow me enough data to figure out what exactly just failed.
I understand it wouldn't be trivial to do, pretty much because there's already an assert/2 with opts and with message, and they're functions not macros...

Sorry if I'm missing anything about proposals - this is my first one!
I'm willing to maybe put some time into a PR here, if the feedback is positive and has a bit of guidance (Elixir is my day-job, so I do have some experience)

Thanks,
Derek

Ben Wilson

unread,
Sep 15, 2025, 2:31:31 PM (11 days ago) Sep 15
to elixir-lang-core
+1 to this from me. We have a variety of asserts inside of a list that always have commented out dbg statements above them to help with failures. Passing those into assert would be great.

Jason Axelson

unread,
Sep 15, 2025, 2:50:15 PM (11 days ago) Sep 15
to elixir-l...@googlegroups.com
Big +1 from me as well. I often skip using `assert/2` because of the need to re-implement the rich diffing logic that `assert/1` gives. Generally I only truly want to provide additional context that will help the developer understand what failed rather than wanting my own "diffing" output.

-Jason

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/afe45a12-091d-4980-b64d-e5974926e7cdn%40googlegroups.com.

Jon Rowe

unread,
Sep 16, 2025, 8:35:09 AM (10 days ago) Sep 16
to Elixir Lang Core
+1 here, more context is often useful but the diff support is crucial.

Cheers
Jon

José Valim

unread,
Sep 16, 2025, 8:36:35 AM (10 days ago) Sep 16
to elixir-l...@googlegroups.com
If someone would like to tackle this, please go ahead. I assume it will require a reasonable amount of work.



Reply all
Reply to author
Forward
0 new messages