Improve ExUnit.AssertionError exception messages
ExUnit prints a very nice exception message when it rescues the assertion. However if the assertion is raised in another process the displayed error is sometimes lacking because the raw exception message is used and it does not contain much information.
An example from ecto:
** (EXIT from #PID<0.467.0>) an exception was raised:
** (ExUnit.AssertionError) match (=) failed
integration_test/pg/deadlock_test.exs:40: Ecto.Integration.DeadlockTest.acquire_deadlock/2
(ecto) lib/ecto/adapters/sql.ex:489: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1162: DBConnection.transaction_run/3
(db_connection) lib/db_connection.ex:1084: DBConnection.run_begin/3
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
I am taking this issue, currently working on it
@fishcakez
Mix tasks give us suggestion if there is a small typo. For example, for mix testy
mix testy
** (Mix) The task "testy" could not be found. Did you mean "test"?
This is calculated considering String jaro distance. If there is any mix task name with jar score more than 0.80, it is shown.
I was thinking similar message style.
assert “foobar” = “fooba”,
# the error message could be
match (=) failed. did you mean “foobar” on rhs ?
assert “foo” = “bar”,
# the error message could be
match (=) failed. rhs = “bar"
https://github.com/elixir-lang/elixir/blob/master/lib/ex_unit/lib/ex_unit/assertions.ex#L128
rhs value is available. but how do i get lhs value here ? need help.
┃ iex> String.jaro_distance("dwayne", "duane")
┃ 0.8222222222222223
┃ iex> String.jaro_distance("even", "odd")
┃ 0.0
The right hand side is likely to be code and the left hand side a pattern, so I don't think strings are a common enough case to worry about just yet.
The issues is not about improving this output:
1) test foo (AppTest)
test/app_test.exs:11
match (=) failed
code: :a = :b
rhs: :b
stacktrace:
test/app_test.exs:12
Its about improving the case where assert is used in a process linked to the test process:
Task.async(fn() -> assert :a = :b end) |> Task.await()
Which outputs:
1) test foo (AppTest)
test/app_test.exs:11
** (EXIT from #PID<0.189.0>) an exception was raised:
** (ExUnit.AssertionError) match (=) failed
test/app_test.exs:12: anonymous fn/0 in AppTest.test foo/1
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Notice it doesn't tell use the right hand side value and not enough information to debug the issue in most cases. If we didn't have assert, we get a much better error:
1) test foo (AppTest)
test/app_test.exs:11
** (EXIT from #PID<0.189.0>) an exception was raised:
** (MatchError) no match of right hand side value: :b
test/app_test.exs:12: anonymous fn/0 in AppTest.test foo/1
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Ok, i will look into this.
I am still working on this issue. Giving most of my time. But if this is blocking any other issue, you make take this one. Not surrendered the issue yet.
Ignore this comment. It just conveys work in progress.
As discussed, changing the error messages.
At the same time, checking for a possibility supervised.ex (around line no 115), function defp exit/4 has more details like message, value of right etc. on inspecting get_reason(log_reason), we can see value of message as well as right. Trying to figure out how is it that only value of message is picked up. what code is doing this. can we attach value of right/left here itself.
:error_logger.for
@fishcakez Here is an update. Can i have your feedback on error message.
No, I haven't had to keep changing message everywhere or add value of right everywhere. The code to be changed is different.
I feel i am close to what is expected.
1) test assert is used inside a process linked to other process (ExUnit.AssertionsTest)
test/ex_unit/assertions_test.exs:348
** (EXIT from #PID<0.261.0>) an exception was raised:
** (ExUnit.AssertionError) "match (=) failed. RHS: :b"
> here the expression was :
> Task.async(fn() -> assert :a = :b end) |> Task.await()
test/ex_unit/assertions_test.exs:350: anonymous fn/0 in ExUnit.AssertionsTest.test assert is used inside a process linked to other process/1
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
.................................
23:51:38.482 [error] Task #PID<0.297.0> started from #PID<0.296.0> terminating
** (ExUnit.AssertionError) "**Assertion with in failed. RHS: ['lorem', 'epsum']**"
> here the expression was:
> Task.async(fn() -> assert 'foo' in ['lorem', 'epsum'] end) |> Task.await()
(ex_unit) lib/ex_unit/assertions.ex:281: ExUnit.Assertions.assert/2
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Function: #Function<4.52216389/0 in ExUnit.AssertionsTest.test assert is used inside a process linked to other process, 2nd example/1>
Args: []
2) test assert is used inside a process linked to other process, 2nd example (ExUnit.AssertionsTest)
test/ex_unit/assertions_test.exs:359
** (EXIT from #PID<0.296.0>) an exception was raised:
** (ExUnit.AssertionError) "**Assertion with in failed. RHS: ['lorem', 'epsum']**"
(ex_unit) lib/ex_unit/assertions.ex:281: ExUnit.Assertions.assert/2
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
@fishcakez After making few more changes, here is the result.
1) test assert is used inside a process linked to other process, 3rd example (ExUnit.AssertionsTest)
test/ex_unit/assertions_test.exs:372
** (EXIT from #PID<0.254.0>) an exception was raised:
** (ExUnit.AssertionError) match (match?) failed
The following variables were pinned:
a = 1. RHS: {2, 1}
(ex_unit) lib/ex_unit/assertions.ex:285: ExUnit.Assertions.assert/2
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
2) test assert is used inside a process linked to other process (ExUnit.AssertionsTest)
test/ex_unit/assertions_test.exs:348
** (EXIT from #PID<0.316.0>) an exception was raised:
** (ExUnit.AssertionError) match (=) failed. RHS: :b
test/ex_unit/assertions_test.exs:350: anonymous fn/0 in ExUnit.AssertionsTest.test assert is used inside a process linked to other process/1
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
3) test assert is used inside a process linked to other process, 2nd example (ExUnit.AssertionsTest)
test/ex_unit/assertions_test.exs:359
** (EXIT from #PID<0.324.0>) an exception was raised:
** (ExUnit.AssertionError) Expected exception ArgumentError but nothing was raised
(ex_unit) lib/ex_unit/assertions.ex:285: ExUnit.Assertions.assert/2
(elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
(elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
@josevalim @lexmag how much information should we move to the message and how much should we leave to in the struct to only be pretty/colour printed if in the test process? I think if we move all the information to the message it will be less clear for 95% of cases. Perhaps we only need to show the right (and if relevant left) when the assert uses =, ==, >=, match??
@fishcakez I would say that, whatever we can show in the message, with similar formatting to the one in the formatter, will be a welcome addition.
But without colors, of course.
@sandeshsoni this looks great except I think we want the current behaviour when the error occurs in the test process, and only display the extended the message when the error occurs elsewhere.
Hard to say without looking at current approach, are we going to have message/1 function in ExUnit.AssertionError?
I looked briefly how we can get it on formatter level, we can match on {:EXIT, %ExUnit.AssertionError{}} there and apply more extensive formatting I think.
@fishcakez does that mean, continue the old behavior i.e message when error occurs in normal behavior.
> for :a = :b
match (=) failed
code: :a = :b
rhs: :b
When error occurs in test process.
> for Task.async(fn() -> assert :a = :b end) |> Task.await()
** (ExUnit.AssertionError) match (=) failed. RHS: :b
The changes i made should not have any effect on regular error message. i.e MatchError etc. The changes have effect only on ExUnit.AssertionError
@lexmag Yes, my current approach is having message/1 in ExUnit.AssertionError
@sandeshsoni hows your progress on this? I'd be happy to chip in if you'd like any help.
@ybur-yug Its in progress. I am giving a try to thought of lexmag. If i need help in adding tests, i will ask for help. Thanks.
Sounds good! Thanks @sandeshsoni
wip update. safely Ignore this comment.
After making changes as suggested by lexmag. So far achieved this. Adding necessary tests.
test/ex_unit/assertions_test.exs:370
rhs: {2, 1}
"match (match?) failed\nThe following variables were pinned:\n a = 1"
code: {:match?, [line: 374],
[{{:^, [line: 374], [{:a, [line: 374], nil}]}, 1}, {{:., [line: 374], [{:__aliases__, [counter: 0, line: 374], [:Value]}, :tuple]}, [line: 374], []}]}
test/ex_unit/assertions_test.exs:346
rhs: :b
"match (=) failed"
code: {:=, [line: 348], [:a, :b]}