etest
etest is a lightweight, convention-over-configuration test framework for Erlang.
etest expects an Erlang application / rebar-compatible directory structure with the following top level directories: src, deps, ebin and test.
Test module file names end with
_test.erl
ending and test case function names should start withtest_
.-
Each test file can implement some or all of the following callbacks:
-
before_suite
- Invoked once before all cases, -
before_test
- Invoked before each case, -
after_test
- Invoked after each case, -
after_suite
- Invoked once after all cases, independent of case failure,
-
- There are no test generators like in EUnit. If an assertion in a test case fails, the rest of the case will be skipped and etest continues executing the remaining cases.
Example Test Case
-module(my_first_test).
-compile(export_all).
% Include etest's assertion macros.
-include_lib("etest/include/etest.hrl").
before_suite() ->
setup_database(),
application:start(myapp).
before_test() ->
myapp_users:create_dummy_user().
test_application_has_one_user() ->
?assert_equal(1, length( myapp_users:all() )).
test_creating_a_new_user() ->
Old = myapp_users:first(),
New = myapp_users:create(
[{name, "Peter"}, {favorite_test_framework, "etest"}]),
?assert_equal(2, length( myapp_users:all() )),
?assert_not_equal(Old, New),
?assert_equal(New, myapp_users:last()).
after_test() ->
myapp_users:delete_all(),
after_suite() ->
application:stop(myapp).
Available Assertions
Positive Assertions
?assert(Expression)
?assert_equal(Expected, Actual)
?assert_match(Expected, Actual)
?assert_exception(Class, Pattern, Expression)
?assert_error(Pattern, Expression)
?assert_exit(Pattern, Expression)
?assert_throw(Pattern, Expression)
Negated Assertions
This is the same list as in the previous section, with _not
or _no
inserted in the name.
?assert_not(Expression)
?assert_not_equal(Unexpected, Actual)
?assert_no_match(Unexpected, Actual)
?assert_no_exception(Class, Pattern, Expression)
?assert_no_error(Pattern, Expression)
?assert_no_exit(Pattern, Expression)
?assert_no_throw(Pattern, Expression)
Fixtures
etest has no concept of fixtures like EUnit. If you need some data over and over inside of your tests, you can define macros or functions instead and call them from within your tests.
Focus Tests
From time to time you might want to run a single test case out of your suite to quickly pin down where the underlying program fails. Thus we introduced the concept of Focus Tests, where you would mark one or more test cases with the prefix focus_
, then upon running your tests only the marked cases will be executed.
Example:
-module(my_focus_test).
-compile(export_all).
% Include etest's assertion macros.
-include_lib("etest/include/etest.hrl").
% This case will be ignored.
test_bar() ->
% ...
?assert_equal(false, true).
% This case will be run.
focus_test_foo() ->
% ...
?assert(true).
Demo
There is a quick screencast on vimeo that shows how to use etest in your project: https://vimeo.com/43672318
Installation
To install etest add it as a dependency to your rebar.config and also add
the test directory to the src_dirs
option. This will compile the
tests whenever rebar compile
is executed and you don't have to compile
the tests manually.
Example rebar.config
% Compiler Options for rebar
{erl_opts, [
{src_dirs, ["src", "test"]}
]}.
% Dependencies
{deps, [
{etest, ".*", {git, "git://github.com/wooga/etest.git"}}
]}.
% Which files to cleanup when rebar clean is executed.
{clean_files, ["ebin/*.beam"]}.
After updating your rebar.config, run rebar get-deps
to install etest.
Running the Tests
Before running the tests, they need to be compiled by running
rebar compile
. You can write a simple shell script that compiles
everything before running the tests to make your life easier.
In your project directory, run deps/etest/bin/etest-runner
to execute all
tests in the test
directory.
Run deps/etest/bin/etest-runner test/integration/user_login_test.erl
to
execute a single test file.
To pass additional arguments to the erl
command you can use the
environment variable ERL_AFLAGS
. For example:
ERL_AFLAGS="-config priv/config/test.config" deps/etest/bin/etest-runner