Skip to content

Instantly share code, notes, and snippets.

@Morgul
Last active January 4, 2016 08:19
Show Gist options
  • Save Morgul/8594399 to your computer and use it in GitHub Desktop.
Save Morgul/8594399 to your computer and use it in GitHub Desktop.
An example erlang test module, written using a Jasmine/Mocha-like BDD module, which doesn't exist yet. As this is all theoretical, this is very much open to some discussion.
%%% @doc Tests for the `example' module.
%%%
%%% This uses an as-of-yet unwritten BDD framework for Erlang.
%%% --------------------------------------------------------------------------------------------------------------------
-module(example_spec).
-include ("bdd.hrl").
-export([spec/0]).
%% --------------------------------------------------------------------------------------------------------------------
spec() ->
%% ----------------------------------------------------------------------------------------------------------------
%% Example Module Tests
%% ----------------------------------------------------------------------------------------------------------------
?define("Example Module", fun() ->
% Setup code - called before any 'it' function is run.
?before(fun() ->
example:init([]).
end).
% Teardown code - called after every 'it' function has run.
?after(fun() ->
example:cleanup();
end).
% Test for the 'foo' function.
?it("has a foo function", fun() ->
Ret = example:foo(),
assert:equals(bar, Ret).
end),
% Test for the 'bar' function
?it("has a bar function", fun() ->
Ret = example:bar(),
assert:not_equals(bar, Ret),
assert:equals(foo, Ret).
end),
% Example of a test failing
?it("will return the answer to life, the universe, and everything", fun() ->
Ret = example:dont_panic(),
assert:equals(42, Ret).
end),
% Define a category of behavior
?define("Superflous Math Operations", fun() ->
% Tests for addition
?it("can add two numbers", fun() ->
example:add(2, 2),
assert:greater_than(2, Ret), % Only here to show more possibly assert functions.
assert:less_than(5, Ret), % Only here to show more possibly assert functions.
assert:equals(4, Ret).
end),
?it("can add a negative number", fun() ->
example:add(2, -2),
assert:equals(0, Ret).
end),
% This test fails
?it("can multiply two numbers", fun() ->
example:mult(2, 2),
assert:fail("Not implemented yet.").
end),
% This test is skipped
?xit("can divide two numbers", fun() -> % Also can support `?skip` for a more descriptive usage.
pass.
end),
end),
end),
%% ----------------------------------------------------------------------------------------------------------------
%% Example Supervisor Tests
%% ----------------------------------------------------------------------------------------------------------------
?define("Example Supervisor", fun() ->
% This is simply to show that multiple behavior specifications can be defined in the same test module.
pass.
end).
Example Module
✓ has a foo function
✓ has a bar function
x will return the answer to life, the universe, and everything
Superflous Math Operations
✓ can add two numbers
✓ can add a negative number
x can multiply two numbers
- can divide two numbers
Example Supervisor
- no tests found
4 passing (8ms)
1 skipped
2 failing
1) Example Module :: will return the answer to life, the universe, and everything:
Not Equal: expected 42, got 41
at example_spec:spec("Example Module"):37
2) Example Module :: Superflous Math Operations :: can multiple two numbers:
Fail: "Not implemented yet."
at example_spec:spec("Example Module"):62
@Morgul
Copy link
Author

Morgul commented Jan 24, 2014

Couple of comments:

First, I have to assume we'll be limited by whatever traceback information erlang will give us, for the failures. The test_output.txt file obviously doesn't accurately reflect the formatting we might be forced to use for tracebacks.

Also, there's quite a bit of funs being used. I don't know if there's any good way to reduce that; the advantage they have is that we can wrap them in try/catch, and report the traceback of tests that explode.

Also, not sure if spec or assert will conflict with any built-ins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment