lud
I can't get actual code location (line/column) from macros used in tests
I’m trying to build a protocol and a test suite to implement different adapters (impls of the protocol).
I tried to use a CaseTemplate, or just regular macros, but all tests defined inside quote location: :keep ... will always show, on failure, the line where the macro is called or where the template is used.
Any idea how I could have the actual line of failure?
Thank you
Marked As Solved
NobbZ
Looks like you can’t do anything about that, test macro uses __CALLER__.file and enforces it.
perhaps you can use a similar technique to override it?
Also Liked
NobbZ
If I understand you right you want to see the file that defines Demo.SomeTest in the error, and if I understand the docs correctly, that’ll be the case if you remove : location.
https://hexdocs.pm/elixir/Kernel.SpecialForms.html#quote/2-stacktrace-information
hisa
Hey guys, long time since this question was asked but I was just looking for the same answer and lucikly I found a workaround, in Elixir 1.18.3 (compiled with Erlang/OTP 27).
Following the recommendations from the Macros docs, this is how I’m able to get a stacktrace ending with the exact line where the assertion error is happening, in the CaseTemplate file:
defmodule Demo.TestSuite do
use ExUnit.CaseTemplate
using do
quote do
import Demo.TestSuite
test "some test" do
assert_eq(1, 2)
end
end
end
def assert_eq(a, b) do
assert a == b
true # WITHOUT this, it just shows the line in the test file at the top of the stacktrace
end
end
I’m returning true because the function starts with assert_. I would return false if the function starts with refute_
I don’t know the exact reason of this behavior, I just noticed that if the last statement in the function is an assert or refute, the output shows only the line where the function is called, and not the line inside the function.








