ExUnit.Assertions

This module contains a set of assertion functions that are imported by default into your test cases.

In general, a developer will want to use the general assert macro in tests. This macro tries to be smart and provide good reporting whenever there is a failure. For example, assert some_fun() == 10 will fail (assuming some_fun() returns 13):

Expected 10 to be equal to 13

This module also provides other convenience functions like assert_in_delta and assert_raise to easily handle other common cases such as checking a floating point number or handling exceptions.

Source

Summary

assert(expected)

Asserts the expected value is true

assert(expected, message)

Asserts the expected value is true. If it fails, raises the given message

assert(value, expected, actual, content)

Asserts the expected value is true. If it fails, it raises an expectation error using the given expected and actual values

assert_in_delta(expected, received, delta, message \\ nil)

Asserts the expected and received are within delta

assert_raise(exception, function)

Asserts the exception is raised during function execution. Returns the rescued exception, fails otherwise

assert_raise(exception, message, function)

Asserts the exception is raised during function execution with the expected_message. Returns the rescued exception, fails otherwise

assert_receive(expected, timeout \\ 100, message \\ nil)

Asserts a message was or is going to be received. Unlike assert_received, it has a default timeout of 100 milliseconds

assert_received(expected, message \\ nil)

Asserts a message was received and is in the current process' mailbox. Timeout is set to 0, so there is no waiting time

catch_error(expression)

Asserts the given expression will cause an error. Returns the error or fails otherwise

catch_exit(expression)

Asserts the given expression will exit. Returns the exit status/message or fails otherwise

catch_throw(expression)

Asserts the given expression will throw a value. Returns the thrown value or fails otherwise

flunk(message \\ "Flunked!")

Fails with a message

refute(expected)

Refutes the expected value is true

refute(not_expected, message)

Asserts the not_expected value is nil or false. In case it is a truthy value, raises the given message

refute_in_delta(expected, received, delta, message \\ nil)

Asserts the expected and received are not within delta

refute_receive(not_expected, timeout \\ 100, message \\ nil)

Asserts a message was not received and won't be within the timeout period

refute_received(not_expected, message \\ nil)

Asserts a message was not received (i.e. it is not in the current process mailbox). The not_expected argument must be a match pattern

Functions

assert(expected, message)

Asserts the expected value is true. If it fails, raises the given message.

Examples

assert false, "it will never be true"
Source
assert(value, expected, actual, content)

Asserts the expected value is true. If it fails, it raises an expectation error using the given expected and actual values.

Examples

assert this > that, this, that, "more than"
Source
assert_in_delta(expected, received, delta, message \\ nil)

Asserts the expected and received are within delta.

Examples

assert_in_delta 1.1, 1.5, 0.2
assert_in_delta 10, 15, 4
Source
assert_raise(exception, function)

Asserts the exception is raised during function execution. Returns the rescued exception, fails otherwise.

Examples

assert_raise ArithmeticError, fn ->
  1 + "test"
end
Source
assert_raise(exception, message, function)

Asserts the exception is raised during function execution with the expected_message. Returns the rescued exception, fails otherwise.

Examples

assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
  1 + "test"
end
Source
flunk(message \\ "Flunked!")

Specs:

Fails with a message.

Examples

flunk "This should raise an error"
Source
refute(not_expected, message)

Asserts the not_expected value is nil or false. In case it is a truthy value, raises the given message.

Examples

refute true, "This will obviously fail"
Source
refute_in_delta(expected, received, delta, message \\ nil)

Asserts the expected and received are not within delta.

Examples

refute_in_delta 1.1, 1.2, 0.2
refute_in_delta 10, 11, 2
Source

Macros

assert(expected)

Asserts the expected value is true.

assert in general tries to be smart and provide good reporting whenever there is a failure. For example, assert 10 > 15 is going to fail with the message:

Expected 10 to be more than 15

Examples

assert true
Source
assert_receive(expected, timeout \\ 100, message \\ nil)

Asserts a message was or is going to be received. Unlike assert_received, it has a default timeout of 100 milliseconds.

The given expected argument has to be a pattern.

Examples

assert_receive :hello

Asserts against a larger timeout:

assert_receive :hello, 20_000

You can also match against specific patterns:

assert_receive { :hello, _ }

x = 5
assert_receive { :count, ^x }
Source
assert_received(expected, message \\ nil)

Asserts a message was received and is in the current process' mailbox. Timeout is set to 0, so there is no waiting time.

The given expected argument has to be a pattern.

Examples

send self, :hello
assert_received :hello

You can also match against specific patterns:

send self, { :hello, "world" }
assert_received { :hello, _ }
Source
catch_error(expression)

Asserts the given expression will cause an error. Returns the error or fails otherwise.

Examples

assert catch_error(error 1) == 1
Source
catch_exit(expression)

Asserts the given expression will exit. Returns the exit status/message or fails otherwise.

Examples

assert catch_exit(exit 1) == 1
Source
catch_throw(expression)

Asserts the given expression will throw a value. Returns the thrown value or fails otherwise.

Examples

assert catch_throw(throw 1) == 1
Source
refute(expected)

Refutes the expected value is true.

refute in general tries to be smart and provide good reporting whenever there is a failure.

Examples

refute false
Source
refute_receive(not_expected, timeout \\ 100, message \\ nil)

Asserts a message was not received and won't be within the timeout period.

The not_expected argument must be a match pattern.

Examples

refute_receive :bye

Refute received with a explicit timeout:

refute_receive :bye, 1000
Source
refute_received(not_expected, message \\ nil)

Asserts a message was not received (i.e. it is not in the current process mailbox). The not_expected argument must be a match pattern.

Timeout is set to 0, so there is no waiting time.

Examples

send self, :hello
refute_received :bye
Source