ExUnit - add refute_raise() assertion

319 views
Skip to first unread message

sun...@gmail.com

unread,
Aug 11, 2016, 8:16:52 PM8/11/16
to elixir-lang-core
Hello,

Please provide a new refute_raise() assertion in ExUnit so that I can assert
that no exception was raised when a given function is called.

At present, the lack of refute_raise() makes some of my test cases look weird
because they just call a function and don't explicitly assert anything:

    ## a normal looking test case ##
    test "should raise when floating point number is given" do
      assert_raise ArgumentError, fn ->
        MyModule.my_func(123.456)
      end
    end

    ## a weird looking test case ##
    test "should not raise any error when integer is given" do
      MyModule.my_func(123) # <== looks weird; what is this asserting?
    end

This is also similar in principle to the existing refute_receive() assertion.

Thanks for your consideration.

Wiebe-Marten Wijnja

unread,
Aug 12, 2016, 2:10:00 AM8/12/16
to elixir-lang-core
Thank you very much for your proposal!

It seems like you are not at all interested in the actual output of MyModule.my_func(123) in your example. In cases like these, the convention in Elixir is usually to return `true` or `:ok`. These could than be asserted in the test, just like normal:

assert MyModule.my_func(123) == :ok

In cases like these, above test is stronger than anything you could test using `refute_raise`.

I can see one problem where assert becomes slightly less practical, which would be when `MyModule.my_func` returns an unpredictable value (such as a random number).
However, even in these cases we can use `assert` to check if the output:
- has the type we expect by using functions like `assert is_integer(myModule.my_func(123))`
- matches one of the possible results using `assert MyModule.my_func(123) in [:foo, :bar, :baz]`
etc.

Of course, non-deterministic tests are bad because when they fail it is hard to reproduce a failure, but that is probably a topic for another conversation.

In summary: I do not think that `refute_raise` is useful, since we can easily write more specific tests that will cover all cases that `refute_raise` would cover.


Sincerely,

~Wiebe-Marten/Qqwy

Suraj N. Kurapati

unread,
Aug 13, 2016, 9:15:23 PM8/13/16
to Wiebe-Marten Wijnja, elixir-lang-core
On Thu, 11 Aug 2016 23:10:00 -0700 (PDT), Wiebe-Marten Wijnja wrote:
> On Fri, 12 Aug 2016 02:16:52 UTC+2, Suraj N. Kurapati wrote:
> > Please provide a new refute_raise() assertion in ExUnit
>
> the convention in Elixir is usually to return `true` or `:ok`.
> These could than be asserted in the test, just like normal:
>
> assert MyModule.my_func(123) == :ok
>
> [it's] stronger than anything you could test using `refute_raise`.

Great idea! I'll follow this approach. :) Thanks for your feedback.
Reply all
Reply to author
Forward
0 new messages