Teal

Stratus3D

Description

An Erlang assertion library. Writing unit tests for Erlang applications is usually pretty straightforward. After all, most of the time you are just testing functions. Testing functions is easy because you just invoke a function with some parameters and then check if the expected value was returned. With pattern matching this is trivial. However, not everything is so simple. Say for example, you want to test that a module implements a certain behavior. You could do something like this:

% Check if a module exports all the callbacks defined in a behavior
Exports = module_under_test:module_info(exports),
lists:foreach(fun(Callback) ->
        % Assert the callback is in the list of exported functions
        true = lists:member(Callback, Exports)
        end, [ % Callbacks defined in behaviour
            {callback_1, 1},
            {another_callback, 2},
            {a_third, 0}
        ]).

Or with Teal, it would be as simple as:

teal_behaviours:assert_implements_behaviour(module_under_test, expected_behavior).

Teal does all the hard work behind the scenes verifying that the module does in fact implement the behavior. And unlike the custom code in the first example, Teal's implements_behaviour function will work with any behavior.

I created Teal because I kept writing similar test helper functions in my unit tests. I tried to extract all the common patterns I saw in my test helpers into generic functions in Teal. This library by no means complete. I am sure there are common assertions that I missed. If you have anything that you think should be part of Teal feel free to create a pull request or issue on GitHub.

For Elixir, checkout Lilac, an Elixir wrapper for Teal.

Installation

To build Teal cd into the root and run make.

To make the Teal modules available during your tests. Added it to your ERL_LIBS environment variable(most likely defined in ~/.bashrc if you are using Bash):

export ERL_LIBS=/full/path/to/teal/

Or add it to your Erlang resource file (~/.erlang) like so:

code:load_abs("/full/path/to/teal/").

All the Teal modules should now be able in your tests.

Usage

Once you have the Teal installed you should be able to use any of the functions below in your Common Test or EUnit test suites. You could also use Teal directly in your application if you wanted. But most of the functions are only useful in unit tests. For example, using functions like teal:raises_exception in your application code would violate Erlang's "fail fast" principle in most cases.

API

The API is documented below. Most of the functions listed return a boolean. Of the functions that return booleans, there are two additional variations of each function that are not documented below. By prefixing one of these functions with assert_ (e.g. teal_lists:includes_members becomes teal_lists:assert_includes_members) an exception is raised if the assertion fails instead of returning false. This allows you to ignore the return value since failure causes an error to be raised. Functions prefixed with assert_ can also take an additional argument. The extra argument is the message in the error that is raised when the assertion fails. This allows you to generate more readable failure messages.

teal

teal_lists

teal_types

teal_modules

teal_processes

teal_behaviours

teal_numbers

TODO

Similar Tools

Known Issues

No known issues. If you see something that could be improved feel free to open an issue on GitHub (https://github.com/Stratus3D/teal/issues)

Contributing

Feel free to create an issue or pull request if you find a bug or if you have additional assertions that you would like to be included in this library.