lud

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

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

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

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.

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement