diff options
Diffstat (limited to 'lib/eunit/doc')
-rw-r--r-- | lib/eunit/doc/edoc-info | 3 | ||||
-rw-r--r-- | lib/eunit/doc/erlang.png | bin | 0 -> 2109 bytes | |||
-rw-r--r-- | lib/eunit/doc/eunit.html | 71 | ||||
-rw-r--r-- | lib/eunit/doc/eunit_surefire.html | 78 | ||||
-rw-r--r-- | lib/eunit/doc/html/.gitignore | 0 | ||||
-rw-r--r-- | lib/eunit/doc/index.html | 17 | ||||
-rw-r--r-- | lib/eunit/doc/man3/.gitignore | 0 | ||||
-rw-r--r-- | lib/eunit/doc/modules-frame.html | 13 | ||||
-rw-r--r-- | lib/eunit/doc/overview-summary.html | 1032 | ||||
-rw-r--r-- | lib/eunit/doc/overview.edoc | 1028 | ||||
-rw-r--r-- | lib/eunit/doc/packages-frame.html | 11 | ||||
-rw-r--r-- | lib/eunit/doc/pdf/.gitignore | 0 | ||||
-rw-r--r-- | lib/eunit/doc/src/Makefile | 174 | ||||
-rw-r--r-- | lib/eunit/doc/src/book.xml | 51 | ||||
-rw-r--r-- | lib/eunit/doc/src/fascicules.xml | 18 | ||||
-rw-r--r-- | lib/eunit/doc/src/make.dep | 19 | ||||
-rw-r--r-- | lib/eunit/doc/src/notes.xml | 153 | ||||
-rw-r--r-- | lib/eunit/doc/src/part.xml | 38 | ||||
-rw-r--r-- | lib/eunit/doc/src/part_notes.xml | 39 | ||||
-rw-r--r-- | lib/eunit/doc/src/ref_man.xml | 40 | ||||
-rw-r--r-- | lib/eunit/doc/stylesheet.css | 55 |
21 files changed, 2840 insertions, 0 deletions
diff --git a/lib/eunit/doc/edoc-info b/lib/eunit/doc/edoc-info new file mode 100644 index 0000000000..1c04b2ed1a --- /dev/null +++ b/lib/eunit/doc/edoc-info @@ -0,0 +1,3 @@ +{application,eunit}. +{packages,[]}. +{modules,[eunit,eunit_surefire]}. diff --git a/lib/eunit/doc/erlang.png b/lib/eunit/doc/erlang.png Binary files differnew file mode 100644 index 0000000000..987a618e24 --- /dev/null +++ b/lib/eunit/doc/erlang.png diff --git a/lib/eunit/doc/eunit.html b/lib/eunit/doc/eunit.html new file mode 100644 index 0000000000..a181d12ce3 --- /dev/null +++ b/lib/eunit/doc/eunit.html @@ -0,0 +1,71 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>Module eunit</title> +<link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc"> +</head> +<body bgcolor="white"> +<div class="navbar"><a name="#navbar_top"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<hr> + +<h1>Module eunit</h1> +<ul class="index"><li><a href="#description">Description</a></li><li><a href="#index">Function Index</a></li><li><a href="#functions">Function Details</a></li></ul>This module is the main EUnit user interface. +<p>Copyright � 2004-2009 Micka�l R�mond, Richard Carlsson</p> + +<p><b>Version:</b> 2.1.1, Apr 22 2009 22:37:19</p> +<p><b>Authors:</b> Micka�l R�mond (<a href="mailto:[email protected]"><tt>[email protected]</tt></a>) [<em>web site:</em> <tt><a href="http://www.process-one.net/" target="_top">http://www.process-one.net/</a></tt>], Richard Carlsson (<a href="mailto:[email protected]"><tt>[email protected]</tt></a>) [<em>web site:</em> <tt><a href="http://user.it.uu.se/~richardc/" target="_top">http://user.it.uu.se/~richardc/</a></tt>].</p> + +<h2><a name="description">Description</a></h2>This module is the main EUnit user interface. +<h2><a name="index">Function Index</a></h2> +<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#start-0">start/0</a></td><td>Starts the EUnit server.</td></tr> +<tr><td valign="top"><a href="#stop-0">stop/0</a></td><td>Stops the EUnit server.</td></tr> +<tr><td valign="top"><a href="#test-1">test/1</a></td><td>Equivalent to <a href="#test-2"><tt>test(Tests, [])</tt></a>. +</td></tr> +<tr><td valign="top"><a href="#test-2">test/2</a></td><td>Runs a set of tests.</td></tr> +</table> + +<h2><a name="functions">Function Details</a></h2> + +<h3 class="function"><a name="start-0">start/0</a></h3> +<div class="spec"> +<p><tt>start() -> any()</tt></p> +</div><p>Starts the EUnit server. Normally, you don't need to call this + function; it is started automatically.</p> + +<h3 class="function"><a name="stop-0">stop/0</a></h3> +<div class="spec"> +<p><tt>stop() -> any()</tt></p> +</div><p>Stops the EUnit server. Normally, you don't need to call this + function.</p> + +<h3 class="function"><a name="test-1">test/1</a></h3> +<div class="spec"> +<p><tt>test(Tests) -> any()</tt></p> +</div><p>Equivalent to <a href="#test-2"><tt>test(Tests, [])</tt></a>.</p> + + +<h3 class="function"><a name="test-2">test/2</a></h3> +<div class="spec"> +<p><tt>test(Tests::term(), Options::[term()]) -> ok | {error, term()}</tt></p> +</div><p><p>Runs a set of tests. The format of <code>Tests</code> is described in the + section <a href="overview-summary.html#EUnit_test_representation">EUnit test + representation</a> of the overview.</p> + + Example: <pre> eunit:test(fred)</pre><p> runs all tests in the module <code>fred</code> + and also any tests in the module <code>fred_tests</code>, if that module exists.</p> + + Options: + <dl> + <dt><code>verbose</code></dt> + <dd>Displays more details about the running tests.</dd> + </dl> + + Options in the environment variable EUNIT are also included last in + the option list, i.e., have lower precedence than those in <code>Options</code>.</p> +<p><b>See also:</b> <a href="#test-1">test/1</a>.</p> +<hr> + +<div class="navbar"><a name="#navbar_bottom"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<p><i>Generated by EDoc, Apr 22 2009, 22:37:19.</i></p> +</body> +</html> diff --git a/lib/eunit/doc/eunit_surefire.html b/lib/eunit/doc/eunit_surefire.html new file mode 100644 index 0000000000..f2ecbae572 --- /dev/null +++ b/lib/eunit/doc/eunit_surefire.html @@ -0,0 +1,78 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>Module eunit_surefire</title> +<link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc"> +</head> +<body bgcolor="white"> +<div class="navbar"><a name="#navbar_top"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<hr> + +<h1>Module eunit_surefire</h1> +<ul class="index"><li><a href="#description">Description</a></li><li><a href="#index">Function Index</a></li><li><a href="#functions">Function Details</a></li></ul>Surefire reports for EUnit (Format used by Maven and Atlassian +Bamboo for example to integrate test results). +<p>Copyright � 2009 Micka�l R�mond, Paul Guyot</p> + +<p><b>Behaviours:</b> <a href="eunit_listener.html"><tt>eunit_listener</tt></a>.</p> +<p><b>Authors:</b> Micka�l R�mond (<a href="mailto:[email protected]"><tt>[email protected]</tt></a>).</p> +<p><b>See also:</b> <a href="eunit.html">eunit</a>.</p> + +<h2><a name="description">Description</a></h2><p>Surefire reports for EUnit (Format used by Maven and Atlassian +Bamboo for example to integrate test results). Based on initial code +from Paul Guyot.</p> + + Example: Generate XML result file in the current directory: + <pre> eunit:test([fib, eunit_examples], + [{report,{eunit_surefire,[{dir,"."}]}}]).</pre> +<h2><a name="index">Function Index</a></h2> +<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#handle_begin-3">handle_begin/3</a></td><td></td></tr> +<tr><td valign="top"><a href="#handle_cancel-3">handle_cancel/3</a></td><td></td></tr> +<tr><td valign="top"><a href="#handle_end-3">handle_end/3</a></td><td></td></tr> +<tr><td valign="top"><a href="#init-1">init/1</a></td><td></td></tr> +<tr><td valign="top"><a href="#start-0">start/0</a></td><td></td></tr> +<tr><td valign="top"><a href="#start-1">start/1</a></td><td></td></tr> +<tr><td valign="top"><a href="#terminate-2">terminate/2</a></td><td></td></tr> +</table> + +<h2><a name="functions">Function Details</a></h2> + +<h3 class="function"><a name="handle_begin-3">handle_begin/3</a></h3> +<div class="spec"> +<p><tt>handle_begin(X1, Data, St) -> any()</tt></p> +</div> + +<h3 class="function"><a name="handle_cancel-3">handle_cancel/3</a></h3> +<div class="spec"> +<p><tt>handle_cancel(X1, Data, St) -> any()</tt></p> +</div> + +<h3 class="function"><a name="handle_end-3">handle_end/3</a></h3> +<div class="spec"> +<p><tt>handle_end(X1, Data, St) -> any()</tt></p> +</div> + +<h3 class="function"><a name="init-1">init/1</a></h3> +<div class="spec"> +<p><tt>init(Options) -> any()</tt></p> +</div> + +<h3 class="function"><a name="start-0">start/0</a></h3> +<div class="spec"> +<p><tt>start() -> any()</tt></p> +</div> + +<h3 class="function"><a name="start-1">start/1</a></h3> +<div class="spec"> +<p><tt>start(Options) -> any()</tt></p> +</div> + +<h3 class="function"><a name="terminate-2">terminate/2</a></h3> +<div class="spec"> +<p><tt>terminate(X1, St) -> any()</tt></p> +</div> +<hr> + +<div class="navbar"><a name="#navbar_bottom"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<p><i>Generated by EDoc, Apr 22 2009, 22:37:19.</i></p> +</body> +</html> diff --git a/lib/eunit/doc/html/.gitignore b/lib/eunit/doc/html/.gitignore new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/lib/eunit/doc/html/.gitignore diff --git a/lib/eunit/doc/index.html b/lib/eunit/doc/index.html new file mode 100644 index 0000000000..9bd8e8cf6b --- /dev/null +++ b/lib/eunit/doc/index.html @@ -0,0 +1,17 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>The eunit application</title> +</head> +<frameset cols="20%,80%"> +<frame src="modules-frame.html" name="modulesFrame" title=""> + +<frame src="overview-summary.html" name="overviewFrame" title=""> +<noframes> +<h2>This page uses frames</h2> +<p>Your browser does not accept frames. +<br>You should go to the <a href="overview-summary.html">non-frame version</a> instead. +</p> +</noframes> +</frameset> +</html>
\ No newline at end of file diff --git a/lib/eunit/doc/man3/.gitignore b/lib/eunit/doc/man3/.gitignore new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/lib/eunit/doc/man3/.gitignore diff --git a/lib/eunit/doc/modules-frame.html b/lib/eunit/doc/modules-frame.html new file mode 100644 index 0000000000..a484e99d4c --- /dev/null +++ b/lib/eunit/doc/modules-frame.html @@ -0,0 +1,13 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>The eunit application</title> +<link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc"> +</head> +<body bgcolor="white"> +<h2 class="indextitle">Modules</h2> +<table width="100%" border="0" summary="list of modules"> +<tr><td><a href="eunit.html" target="overviewFrame" class="module">eunit</a></td></tr> +<tr><td><a href="eunit_surefire.html" target="overviewFrame" class="module">eunit_surefire</a></td></tr></table> +</body> +</html>
\ No newline at end of file diff --git a/lib/eunit/doc/overview-summary.html b/lib/eunit/doc/overview-summary.html new file mode 100644 index 0000000000..ea7beba8b3 --- /dev/null +++ b/lib/eunit/doc/overview-summary.html @@ -0,0 +1,1032 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>EUnit - a Lightweight Unit Testing Framework for Erlang +</title> +<link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc"> +</head> +<body bgcolor="white"> +<div class="navbar"><a name="#navbar_top"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<h1>EUnit - a Lightweight Unit Testing Framework for Erlang +</h1> +<p>Copyright � 2004-2007 Micka�l R�mond, Richard Carlsson</p> +<p><b>Version:</b> 2.1.1, Apr 22 2009 22:37:19 +</p> +<p><b>Authors:</b> Richard Carlsson (<a href="mailto:[email protected]"><tt>[email protected]</tt></a>) [<em>web site:</em> <tt><a href="http://user.it.uu.se/~richardc/" target="_top">http://user.it.uu.se/~richardc/</a></tt>], Micka�l R�mond (<a href="mailto:[email protected]"><tt>[email protected]</tt></a>) [<em>web site:</em> <tt><a href="http://www.process-one.net/" target="_top">http://www.process-one.net/</a></tt>].</p> +<p>EUnit is a unit testing framework for Erlang. It is very powerful +and flexible, is easy to use, and has small syntactical overhead.</p> + +<ul> +<li><a href="#Unit_testing">Unit testing</a></li> +<li><a href="#Terminology">Terminology</a></li> +<li><a href="#Getting_started">Getting started</a></li> +<li><a href="#EUnit_macros">EUnit macros</a></li> +<li><a href="#EUnit_test_representation">EUnit test representation</a></li> +</ul> + +<p>EUnit builds on ideas from the family of unit testing frameworks for +Object Oriented languages that originated with JUnit by Beck and Gamma +(and Beck's previous framework SUnit for Smalltalk). However, EUnit uses +techniques more adapted to functional and concurrent programming, and is +typically less verbose than its relatives.</p> + +<p>Although EUnit uses many preprocessor macros, they have been designed to +be as nonintrusive as possible, and should not cause conflicts with +existing code. Adding EUnit tests to a module should thus not normally +require changing existing code. Furthermore, tests that only exercise +the exported functions of a module can always be placed in a completely +separate module, avoiding any conflicts entirely.</p> + +<h3><a name="Unit_testing">Unit testing</a></h3> + +<p>Unit Testing is testing of individual program "units" in relative +isolation. There is no particular size requirement: a unit can be a +function, a module, a process, or even a whole application, but the most +typical testing units are individual functions or modules. In order to +test a unit, you specify a set of individual tests, set up the smallest +necessary environment for being able to run those tests (often, you +don't need to do any setup at all), you run the tests and collect the +results, and finally you do any necessary cleanup so that the test can +be run again later. A Unit Testing Framework tries to help you in each +stage of this process, so that it is easy to write tests, easy to run +them, and easy to see which tests failed (so you can fix the bugs).</p> + +<h4><a name="Advantages_of_unit_testing">Advantages of unit testing</a></h4> + +<dl> + <dt>Reduces the risks of changing the program</dt> + <dd>Most programs will be modified during their lifetime: bugs will be + fixed, features will be added, optimizations may become necessary, or + the code will need to be refactored or cleaned up in other ways to + make it easier to work with. But every change to a working program is + a risk of introducing new bugs - or reintroducing bugs that had + previously been fixed. Having a set of unit tests that you can run + with very little effort makes it easy to know that the code still + works as it should (this use is called <em>regression testing</em>; + see <a href="#Terminology">Terminology</a>). This goes a long way to reduce the + resistance to changing and refactoring code.</dd> + <dt>Helps guide and speed up the development process</dt> + <dd>By focusing on getting the code to pass the tests, the programmer + can become more productive, not overspecify or get lost in premature + optimizations, and create code that is correct from the very beginning + (so-called <em>test-driven development</em>; see <a href="#Terminology">Terminology</a>).</dd> + <dt>Helps separate interface from implementation</dt> + <dd>When writing tests, the programmer may discover dependencies + (in order to get the tests to run) that ought not to be there, and + which need to be abstracted away to get a cleaner design. This helps + eliminate bad dependencies before they spread throughout the + code.</dd> + <dt>Makes component integration easier</dt> + <dd>By testing in a bottom-up fashion, beginning with the smallest + program units and creating a confidence in that they work as they + should, it becomes easier to test that a higher-level component, + consisting of several such units, also behaves according to + specification (known as <em>integration testing</em>; see <a href="#Terminology">Terminology</a>).</dd> + <dt>Is self-documenting</dt> + <dd>The tests can be read as documentation, typically showing both + examples of correct and incorrect usage, along with the expected + consequences.</dd> +</dl> + +<h3><a name="Terminology">Terminology</a></h3> + +<dl> + <dt>Unit testing</dt> + <dd>Testing that a program unit behaves as it is supposed to do (in + itself), according to its specifications. Unit tests have an important + function as regression tests, when the program later is modified for + some reason, since they check that the program still behaves according + to specification.</dd> + <dt>Regression testing</dt> + <dd>Running a set of tests after making changes to a program, to check + that the program behaves as it did before the changes (except, of + course, for any intentional changes in behaviour). Unit tests are + important as regression tests, but regression testing can involve more + than just unit testing, and may also test behaviour that might not be + part of the normal specification (such as bug-for-bug-compatibility). + </dd> + <dt>Integration testing</dt> + <dd>Testing that a number of individually developed program units + (assumed to already have been separately unit tested) work together as + expected. Depending on the system being developed, integration testing + may be as simple as "just another level of unit testing", but might + also involve other kinds of tests (compare <em>system testing</em>). +</dd> + <dt>System testing</dt> + <dd>Testing that a complete system behaves according to its + specification. Specifically, system testing should not require knowing + any details about the implementation. It typically involves testing + many different aspects of the system behaviour apart from the basic + functionality, such as performance, usability, and reliability.</dd> + <dt>Test-driven development</dt> + <dd>A program development technique where you continuously write tests + <em>before</em> you implement the code that is supposed to pass those + tests. This can help you focus on solving the right problems, and not + make a more complicated implementation than necessary, by letting the + unit tests determine when a program is "done": if it fulfils its + specifications, there is no need to keep adding functionality.</dd> + <dt>Mock object</dt> + <dd>Sometimes, testing some unit <code>A</code> (e.g., a function) requires that + it collaborates somehow with some other unit <code>B</code> (perhaps being passed + as an argument, or by reference) - but <code>B</code> has not been implemented + yet. A "mock object" - an object which, for the purposes of testing + <code>A</code>, looks and behaves like a real <code>B</code> - might then be used instead. + (This is of course only useful if it would be significantly more work + to implement a real <code>B</code> than to create a mock object.)</dd> + <dt>Test case</dt> + <dd>A single, well-defined test, that somehow can be uniquely + identified. When executed, the test case either <em>passes</em> or + <em>fails</em>; the test report should identify exactly which test + cases failed.</dd> + <dt>Test suite</dt> + <dd>A collection of test cases, generally with a specific, common + target for testing, such as a single function, module, or subsystem. A + test suite may also be recursively composed by smaller test + suites.</dd> +</dl> + +<h3><a name="Getting_started">Getting started</a></h3> +<ul> + <li><a href="#Including_the_EUnit_header_file">Including the EUnit header file</a></li> + <li><a href="#Writing_simple_test_functions">Writing simple test functions</a></li> + <li><a href="#Running_EUnit">Running EUnit</a></li> + <li><a href="#Writing_test_generating_functions">Writing test generating functions</a></li> + <li><a href="#An_example">An example</a></li> + <li><a href="#Disabling_testing">Disabling testing</a></li> + <li><a href="#Avoiding_compile-time_dependency_on_EUnit">Avoiding compile-time dependency on EUnit</a></li> +</ul> + +<h4><a name="Including_the_EUnit_header_file">Including the EUnit header file</a></h4> + +The simplest way to use EUnit in an Erlang module is to add the +following line at the beginning of the module (after the <code>-module</code> +declaration, but before any function definitions): +<pre> -include_lib("eunit/include/eunit.hrl").</pre> + +This will have the following effect: +<ul> + <li>Creates an exported function <code>test()</code> (unless testing is turned + off, and the module does not already contain a test() function), that + can be used to run all the unit tests defined in the module</li> + <li>Causes all functions whose names match <code>..._test()</code> or <code>..._test_()</code> + to be automatically exported from the module (unless testing is + turned off, or the <code>EUNIT_NOAUTO</code> macro is defined)</li> + <li>Makes all the preprocessor macros of EUnit available, to help + writing tests</li> +</ul> + +<strong>Note:</strong> For <code>-include_lib(...)</code> to work, the Erlang +module search path <em>must</em> contain a directory whose name ends in +<code>eunit/ebin</code> (pointing to the <code>ebin</code> subdirectory of the EUnit +installation directory). If EUnit is installed as <code>lib/eunit</code> under your +Erlang/OTP system directory, its <code>ebin</code> subdirectory will be +automatically added to the search path when Erlang starts. Otherwise, +you need to add the directory explicitly, by passing a <code>-pa</code> flag to the +<code>erl</code> or <code>erlc</code> command. For example, a Makefile could contain the +following action for compiling <code>.erl</code> files: +<pre> erlc -pa "path/to/eunit/ebin" $(ERL_COMPILE_FLAGS) -o$(EBIN) $<</pre> +or if you want Eunit to always be available when you run Erlang +interactively, you can add a line like the following to your +<code>$HOME/.erlang</code> file: +<pre> code:add_path("/path/to/eunit/ebin").</pre> + +<h4><a name="Writing_simple_test_functions">Writing simple test functions</a></h4> + +<p>The EUnit framework makes it extremely easy to write unit tests in +Erlang. There are a few different ways of writing them, though, so we +start with the simplest:</p> + +<p>A function with a name ending in <code>..._test()</code> is recognized by EUnit as +a simple test function - it takes no arguments, and its execution either +succeeds (returning some arbitrary value that EUnit will throw away), or +fails by throwing an exception of some kind (or by not terminating, in +which case it will be aborted after a while).</p> + +An example of a simple test function could be the following: +<pre> reverse_test() -> lists:reverse([1,2,3]).</pre><p> +This just tests that the function <code>lists:reverse(List)</code> does not crash +when <code>List</code> is <code>[1,2,3]</code>. It is not a great test, but many people write +simple functions like this one to test the basic functionality of their +code, and those tests can be used directly by EUnit, without changes, +as long as their function names match.</p> + +<h5><a name="Use_exceptions_to_signal_failure">Use exceptions to signal failure</a></h5> + +To write more interesting tests, we need to make them crash (throw an +exception) when they don't get the result they expect. A simple way of +doing this is to use pattern matching with <code>=</code>, as in the following +examples: +<pre> reverse_nil_test() -> [] = lists:reverse([]). + reverse_one_test() -> [1] = lists:reverse([1]). + reverse_two_test() -> [2,1] = lists:reverse([1,2]).</pre><p> +If there was some bug in <code>lists:reverse/1</code> that made it return something +other than <code>[2,1]</code> when it got <code>[1,2]</code> as input, then the last test +above would throw a <code>badmatch</code> error. The first two (we assume they do +not get a <code>badmatch</code>) would simply return <code>[]</code> and <code>[1]</code>, respectively, +so both succeed. (Note that EUnit is not psychic: if you write a test +that returns a value, even if it is the wrong value, EUnit will consider +it a success. You must make sure that the test is written so that it +causes a crash if the result is not what it should be.)</p> + +<h5><a name="Using_assert_macros">Using assert macros</a></h5> + +If you want to use Boolean operators for your tests, the <code>assert</code> +macro comes in handy (see <a href="#EUnit_macros">EUnit macros</a> for details): +<pre> length_test() -> ?assert(length([1,2,3]) =:= 3).</pre><p> +The <code>?assert(Expression)</code> macro will evaluate <code>Expression</code>, and if that +does not evaluate to <code>true</code>, it will throw an exception; otherwise it +just returns <code>ok</code>. In the above example, the test will thus fail if the +call to <code>length</code> does not return 3.</p> + +<h4><a name="Running_EUnit">Running EUnit</a></h4> + +<p>If you have added the declaration +<code>-include_lib("eunit/include/eunit.hrl")</code> to your module, as described +above, you only need to compile the module, and run the automatically +exported function <code>test()</code>. For example, if your module was named <code>m</code>, +then calling <code>m:test()</code> will run EUnit on all the tests defined in the +module. You do not need to write <code>-export</code> declarations for the test +functions. This is all done by magic.</p> + +<p>You can also use the function <a href="eunit.html#test-1"><code>eunit:test/1</code></a> to run arbitrary +tests, for example to try out some more advanced test descriptors (see +<a href="#EUnit_test_representation">EUnit test representation</a>). For example, running +<code>eunit:test(m)</code> does the same thing as the auto-generated function +<code>m:test()</code>, while <code>eunit:test({inparallel, m})</code> runs the same test +cases but executes them all in parallel.</p> + +<h5><a name="Putting_tests_in_separate_modules">Putting tests in separate modules</a></h5> + +<p>If you want to separate your test code from your normal code (at least +for testing the exported functions), you can simply write the test +functions in a module named <code>m_tests</code> (note: not <code>m_test</code>), if your +module is named <code>m</code>. Then, whenever you ask EUnit to test the module +<code>m</code>, it will also look for the module <code>m_tests</code> and run those tests as +well. See <code>ModuleName</code> in the section <a href="#Primitives">Primitives</a> for details.</p> + +<h5><a name="EUnit_captures_standard_output">EUnit captures standard output</a></h5> + +<p>If your test code writes to the standard output, you may be surprised to +see that the text does not appear on the console when the tests are +running. This is because EUnit captures all standard output from test +functions (this also includes setup and cleanup functions, but not +generator functions), so that it can be included in the test report if +errors occur. To bypass EUnit and print text directly to the console +while testing, you can write to the <code>user</code> output stream, as in +<code>io:format(user, "~w", [Term])</code>. The recommended way of doing this is to +use the EUnit <a href="#Debugging_macros">Debugging macros</a>, which make it much simpler.</p> + +<h4><a name="Writing_test_generating_functions">Writing test generating functions</a></h4> + +<p>A drawback of simple test functions is that you must write a separate +function (with a separate name) for each test case. A more compact way +of writing tests (and much more flexible, as we shall see), is to write +functions that <em>return</em> tests, instead of <em>being</em> tests.</p> + +<p>A function with a name ending in <code>..._test_()</code> (note the final +underscore) is recognized by EUnit as a <em>test generator</em> +function. Test generators return a <em>representation</em> of a <em>set +of tests</em> to be executed by EUnit.</p> + +<h5><a name="Representing_a_test_as_data">Representing a test as data</a></h5> + +The most basic representation of a test is a single fun-expression that +takes no arguments. For example, the following test generator: +<pre> basic_test_() -> + fun () -> ?assert(1 + 1 =:= 2) end.</pre> +will have the same effect as the following simple test: +<pre> simple_test() -> + ?assert(1 + 1 =:= 2).</pre><p> +(in fact, EUnit will handle all simple tests just like it handles +fun-expressions: it will put them in a list, and run them one by one).</p> + +<h5><a name="Using_macros_to_write_tests">Using macros to write tests</a></h5> + +To make tests more compact and readable, as well as automatically add +information about the line number in the source code where a test +occurred (and reduce the number of characters you have to type), you can +use the <code>_test</code> macro (note the initial underscore character), like +this: +<pre> basic_test_() -> + ?_test(?assert(1 + 1 =:= 2)).</pre><p> +The <code>_test</code> macro takes any expression (the "body") as argument, and +places it within a fun-expression (along with some extra information). +The body can be any kind of test expression, just like the body of a +simple test function.</p> + +<h5><a name="Underscore-prefixed_macros_create_test_objects">Underscore-prefixed macros create test objects</a></h5> + +But this example can be made even shorter! Most test macros, such as the +family of <code>assert</code> macros, have a corresponding form with an initial +underscore character, which automatically adds a <code>?_test(...)</code> wrapper. +The above example can then simply be written: +<pre> basic_test_() -> + ?_assert(1 + 1 =:= 2).</pre><p> +which has exactly the same meaning (note the <code>_assert</code> instead of +<code>assert</code>). You can think of the initial underscore as signalling +<em>test object</em>.</p> + +<h4><a name="An_example">An example</a></h4> + +Sometimes, an example says more than a thousand words. The following +small Erlang module shows how EUnit can be used in practice. +<pre> -module(fib). + -export([fib/1]). + -include_lib("eunit/include/eunit.hrl"). + + fib(0) -> 1; + fib(1) -> 1; + fib(N) when N > 1 -> fib(N-1) + fib(N-2). + + fib_test_() -> + [?_assert(fib(0) =:= 1), + ?_assert(fib(1) =:= 1), + ?_assert(fib(2) =:= 2), + ?_assert(fib(3) =:= 3), + ?_assert(fib(4) =:= 5), + ?_assert(fib(5) =:= 8), + ?_assertException(error, function_clause, fib(-1)), + ?_assert(fib(31) =:= 2178309) + ].</pre> + +<p>(Author's note: When I first wrote this example, I happened to write a +<code>*</code> instead of <code>+</code> in the <code>fib</code> function. Of course, this showed up +immediately when I ran the tests.)</p> + +<p>See <a href="#EUnit_test_representation">EUnit test representation</a> for a full list of all the ways +you can specify test sets in EUnit.</p> + +<h4><a name="Disabling_testing">Disabling testing</a></h4> + +Testing can be turned off by defining the <code>NOTEST</code> macro when compiling, +for example as an option to <code>erlc</code>, as in: +<pre> erlc -DNOTEST my_module.erl</pre> +or by adding a macro definition to the code, <em>before the EUnit header +file is included</em>: +<pre> -define(NOTEST, 1).</pre><p> +(the value is not important, but should typically be 1 or <code>true</code>). +Note that unless the <code>EUNIT_NOAUTO</code> macro is defined, disabling testing +will also automatically strip all test functions from the code, except +for any that are explicitly declared as exported.</p> + +For instance, to use EUnit in your application, but with testing turned +off by default, put the following lines in a header file: +<pre> -define(NOTEST, true). + -include_lib("eunit/include/eunit.hrl").</pre> +and then make sure that every module of your application includes that +header file. This means that you have a only a single place to modify in +order to change the default setting for testing. To override the <code>NOTEST</code> +setting without modifying the code, you can define <code>TEST</code> in a compiler +option, like this: +<pre> erlc -DTEST my_module.erl</pre> + +<p>See <a href="#Compilation_control_macros">Compilation control macros</a> for details about these +macros.</p> + +<h4><a name="Avoiding_compile-time_dependency_on_EUnit">Avoiding compile-time dependency on EUnit</a></h4> + +If you are distributing the source code for your application for other +people to compile and run, you probably want to ensure that the code +compiles even if EUnit is not available. Like the example in the +previous section, you can put the following lines in a common header +file: +<pre> -ifdef(TEST). + -include_lib("eunit/include/eunit.hrl"). + -endif.</pre><p> +and, of course, also make sure that you place all test code that uses +EUnit macros within <code>-ifdef(TEST)</code> or <code>-ifdef(EUNIT)</code> sections.</p> + + +<h3><a name="EUnit_macros">EUnit macros</a></h3> + +<p>Although all the functionality of EUnit is avaliable even without the +use of preprocessor macros, the EUnit header file defines a number of +such macros in order to make it as easy as possible to write unit tests +as compactly as possible and without getting too many details in the +way.</p> + +<p>Except where explicitly stated, using EUnit macros will never introduce +run-time dependencies on the EUnit library code, regardless of whether +your code is compiled with testing enabled or disabled.</p> + +<ul> +<li><a href="#Basic_macros">Basic macros</a></li> +<li><a href="#Compilation_control_macros">Compilation control macros</a></li> +<li><a href="#Utility_macros">Utility macros</a></li> +<li><a href="#Assert_macros">Assert macros</a></li> +<li><a href="#Macros_for_running_external_commands">Macros for running external commands</a></li> +<li><a href="#Debugging_macros">Debugging macros</a></li> +</ul> + +<h4><a name="Basic_macros">Basic macros</a></h4> + +<dl> +<dt><code>_test(Expr)</code></dt> +<dd>Turns <code>Expr</code> into a "test object", by wrapping it in a +fun-expression and a source line number. Technically, this is the same +as <code>{?LINE, fun () -> (Expr) end}</code>. +</dd> +</dl> + +<h4><a name="Compilation_control_macros">Compilation control macros</a></h4> + +<dl> +<dt><code>EUNIT</code></dt> +<dd>This macro is always defined to <code>true</code> whenever EUnit is enabled at +compile time. This is typically used to place testing code within +conditional compilation, as in: +<pre> -ifdef(EUNIT). + % test code here + ... + -endif.</pre> +e.g., to ensure that the code can be compiled without including the +EUnit header file, when testing is disabled. See also the macros <code>TEST</code> +and <code>NOTEST</code>. +</dd> + +<dt><code>EUNIT_NOAUTO</code></dt> +<dd>If this macro is defined, the automatic exporting or stripping of +test functions will be disabled. +</dd> + +<dt><code>TEST</code></dt> +<dd><p>This macro is always defined (to <code>true</code>, unless previously defined +by the user to have another value) whenever EUnit is enabled at compile +time. This can be used to place testing code within conditional +compilation; see also the macros <code>NOTEST</code> and <code>EUNIT</code>.</p> + +<p>For testing code that is strictly dependent on EUnit, it may be +preferable to use the <code>EUNIT</code> macro for this purpose, while for code +that uses more generic testing conventions, using the <code>TEST</code> macro may +be preferred.</p> + +The <code>TEST</code> macro can also be used to override the <code>NOTEST</code> macro. If +<code>TEST</code> is defined <em>before</em> the EUnit header file is +included (even if <code>NOTEST</code> is also defined), then the code will be +compiled with EUnit enabled. +</dd> + +<dt><code>NOTEST</code></dt> +<dd><p>This macro is always defined (to <code>true</code>, unless previously defined +by the user to have another value) whenever EUnit is <em>disabled</em> +at compile time. (Compare the <code>TEST</code> macro.)</p> + +This macro can also be used for conditional compilation, but is more +typically used to disable testing: If <code>NOTEST</code> is defined +<em>before</em> the EUnit header file is included, and <code>TEST</code> +is <em>not</em> defined, then the code will be compiled with EUnit +disabled. See also <a href="#Disabling_testing">Disabling testing</a>. +</dd> + +<dt><code>NOASSERT</code></dt> +<dd>If this macro is defined, the assert macros will have no effect, +when testing is also disabled. See <a href="#Assert_macros">Assert macros</a>. When +testing is enabled, the assert macros are always enabled automatically +and cannot be disabled. +</dd> + +<dt><code>ASSERT</code></dt> +<dd>If this macro is defined, it overrides the NOASSERT macro, forcing +the assert macros to always be enabled regardless of other settings. +</dd> + +<dt><code>NODEBUG</code></dt> +<dd>If this macro is defined, the debugging macros will have no effect. +See <a href="#Debugging_macros">Debugging macros</a>. <code>NODEBUG</code> also implies <code>NOASSERT</code>, +unless testing is enabled. +</dd> + +<dt><code>DEBUG</code></dt> +<dd>If this macro is defined, it overrides the NODEBUG macro, forcing +the debugging macros to be enabled. +</dd> +</dl> + +<h4><a name="Utility_macros">Utility macros</a></h4> + +<p>The following macros can make tests more compact and readable:</p> + +<dl> +<dt><code>LET(Var,Arg,Expr)</code></dt> +<dd>Creates a local binding <code>Var = Arg</code> in <code>Expr</code>. (This is the same as +<code>(fun(Var)->(Expr)end)(Arg)</code>.) Note that the binding is not exported +outside of <code>Expr</code>, and that within <code>Expr</code>, this binding of <code>Var</code> will +shadow any binding of <code>Var</code> in the surrounding scope. +</dd> +<dt><code>IF(Cond,TrueCase,FalseCase)</code></dt> +<dd>Evaluates <code>TrueCase</code> if <code>Cond</code> evaluates to <code>true</code>, or otherwise +evaluates <code>FalseCase</code> if <code>Cond</code> evaluates to <code>false</code>. (This is the same +as <code>(case (Cond) of true->(TrueCase); false->(FalseCase) end)</code>.) Note +that it is an error if <code>Cond</code> does not yield a boolean value. +</dd> +</dl> + +<h4><a name="Assert_macros">Assert macros</a></h4> + +<p>(Note that these macros also have corresponding forms which start with +an "<code>_</code>" (underscore) character, as in <code>?_assert(BoolExpr)</code>, that create +a "test object" instead of performing the test immediately. This is +equivalent to writing <code>?_test(assert(BoolExpr))</code>, etc.)</p> + +<p>If the macro <code>NOASSERT</code> is defined before the EUnit header file is +included, these macros have no effect when testing is also disabled; see +<a href="#Compilation_control_macros">Compilation control macros</a> for details.</p> + +<dl> +<dt><code>assert(BoolExpr)</code></dt> +<dd><p>Evaluates the expression <code>BoolExpr</code>, if testing is enabled. Unless +the result is <code>true</code>, an informative exception will be generated. If +there is no exception, the result of the macro expression is the atom +<code>ok</code>, and the value of <code>BoolExpr</code> is discarded. If testing is disabled, +the macro will not generate any code except the atom <code>ok</code>, and +<code>BoolExpr</code> will not be evaluated.</p> + +Typical usage: +<pre> ?assert(f(X, Y) =:= [])</pre> + +The <code>assert</code> macro can be used anywhere in a program, not just in unit +tests, to check pre/postconditions and invariants. For example: +<pre> some_recursive_function(X, Y, Z) -> + ?assert(X + Y > Z), + ...</pre> +</dd> +<dt><code>assertNot(BoolExpr)</code></dt> +<dd>Equivalent to <code>assert(not (BoolExpr))</code>. +</dd> +<dt><code>assertMatch(GuardedPattern, Expr)</code></dt> +<dd><p>Evaluates <code>Expr</code> and matches the result against <code>GuardedPattern</code>, if +testing is enabled. If the match fails, an informative exception will be +generated; see the <code>assert</code> macro for further details. <code>GuardedPattern</code> +can be anything that you can write on the left hand side of the <code>-></code> +symbol in a case-clause, except that it cannot contain comma-separated +guard tests.</p> + +<p>The main reason for using <code>assertMatch</code> also for simple matches, instead +of matching with <code>=</code>, is that it produces more detailed error messages.</p> + +Examples: +<pre> ?assertMatch({found, {fred, _}}, lookup(bloggs, Table))</pre> +<pre> ?assertMatch([X|_] when X > 0, binary_to_list(B))</pre> +</dd> +<dt><code>assertEqual(Expect, Expr)</code></dt> +<dd><p>Evaluates the expressions <code>Expect</code> and <code>Expr</code> and compares the +results for equality, if testing is enabled. If the values are not +equal, an informative exception will be generated; see the <code>assert</code> +macro for further details.</p> + +<p><code>assertEqual</code> is more suitable than than <code>assertMatch</code> when the +left-hand side is a computed value rather than a simple pattern, and +gives more details than <code>?assert(Expect =:= Expr)</code>.</p> + +Examples: +<pre> ?assertEqual("b" ++ "a", lists:reverse("ab"))</pre> +<pre> ?assertEqual(foo(X), bar(Y))</pre> +</dd> +<dt><code>assertException(ClassPattern, TermPattern, Expr)</code></dt> +<dt><code>assertError(TermPattern, Expr)</code></dt> +<dt><code>assertExit(TermPattern, Expr)</code></dt> +<dt><code>assertThrow(TermPattern, Expr)</code></dt> +<dd><p>Evaluates <code>Expr</code>, catching any exception and testing that it matches +the expected <code>ClassPattern:TermPattern</code>. If the match fails, or if no +exception is thrown by <code>Expr</code>, an informative exception will be +generated; see the <code>assert</code> macro for further details. The +<code>assertError</code>, <code>assertExit</code>, and <code>assertThrow</code> macros, are equivalent to +using <code>assertException</code> with a <code>ClassPattern</code> of <code>error</code>, <code>exit</code>, or +<code>throw</code>, respectively.</p> + +Examples: +<pre> ?assertError(badarith, X/0)</pre> +<pre> ?assertExit(normal, exit(normal))</pre> +<pre> ?assertException(throw, {not_found,_}, throw({not_found,42}))</pre> +</dd> +</dl> + +<h4><a name="Macros_for_running_external_commands">Macros for running external commands</a></h4> + +<p>Keep in mind that external commands are highly dependent on the +operating system. You can use the standard library function <code>os:type()</code> +in test generator functions, to produce different sets of tests +depending on the current operating system.</p> + +<p>Note: these macros introduce a run-time dependency on the EUnit library +code, if compiled with testing enabled.</p> + +<dl> +<dt><code>assertCmd(CommandString)</code></dt> +<dd><p>Runs <code>CommandString</code> as an external command, if testing is enabled. +Unless the returned status value is 0, an informative exception will be +generated. If there is no exception, the result of the macro expression +is the atom <code>ok</code>. If testing is disabled, the macro will not generate +any code except the atom <code>ok</code>, and the command will not be executed.</p> + +Typical usage: +<pre> ?assertCmd("mkdir foo")</pre> +</dd> +<dt><code>assertCmdStatus(N, CommandString)</code></dt> +<dd>Like the <code>assertCmd(CommandString)</code> macro, but generates an +exception unless the returned status value is <code>N</code>. +</dd> +<dt><code>assertCmdOutput(Text, CommandString)</code></dt> +<dd>Runs <code>CommandString</code> as an external command, if testing is enabled. +Unless the output produced by the command exactly matches the specified +string <code>Text</code>, an informative exception will be generated. (Note that +the output is normalized to use a single LF character as line break on +all platforms.) If there is no exception, the result of the macro +expression is the atom <code>ok</code>. If testing is disabled, the macro will not +generate any code except the atom <code>ok</code>, and the command will not be +executed. +</dd> +<dt><code>cmd(CommandString)</code></dt> +<dd><p>Runs <code>CommandString</code> as an external command. Unless the returned +status value is 0 (indicating success), an informative exception will be +generated; otherwise, the result of the macro expression is the output +produced by the command, as a flat string. The output is normalized to +use a single LF character as line break on all platforms.</p> + +<p>This macro is useful in the setup and cleanup sections of fixtures, +e.g., for creating and deleting files or perform similar operating +system specific tasks, to make sure that the test system is informed of +any failures.</p> + +A Unix-specific example: +<pre> {setup, + fun () -> ?cmd("mktemp") end, + fun (FileName) -> ?cmd("rm " ++ FileName) end, + ...}</pre> +</dd> +</dl> + +<h4><a name="Debugging_macros">Debugging macros</a></h4> + +<p>To help with debugging, EUnit defines several useful macros for printing +messages directly to the console (rather than to the standard output). +Furthermore, these macros all use the same basic format, which includes +the file and line number where they occur, making it possible in some +development environments (e.g., when running Erlang in an Emacs buffer) +to simply click on the message and jump directly to the corresponding +line in the code.</p> + +<p>If the macro <code>NODEBUG</code> is defined before the EUnit header file is +included, these macros have no effect; see +<a href="#Compilation_control_macros">Compilation control macros</a> for details.</p> + +<dl> +<dt><code>debugHere</code></dt> +<dd>Just prints a marker showing the current file and line number. Note +that this is an argument-less macro. The result is always <code>ok</code>.</dd> +<dt><code>debugMsg(Text)</code></dt> +<dd>Outputs the message <code>Text</code> (which can be a plain string, an IO-list, +or just an atom). The result is always <code>ok</code>.</dd> +<dt><code>debugFmt(FmtString, Args)</code></dt> +<dd>This formats the text like <code>io:format(FmtString, Args)</code> and outputs +it like <code>debugMsg</code>. The result is always <code>ok</code>.</dd> +<dt><code>debugVal(Expr)</code></dt> +<dd>Prints both the source code for <code>Expr</code> and its current value. E.g., +<code>?debugVal(f(X))</code> might be displayed as "<code>f(X) = 42</code>". (Large terms are +shown truncated.) The result is always the value of <code>Expr</code>, so this +macro can be wrapped around any expression to display its value when +the code is compiled with debugging enabled.</dd> +<dt><code>debugTime(Text,Expr)</code></dt> +<dd>Prints <code>Text</code> and the wall clock time for evaluation of <code>Expr</code>. The +result is always the value of <code>Expr</code>, so this macro can be wrapped +around any expression to show its run time when the code is compiled +with debugging enabled. For example, <code>List1 = ?debugTime("sorting", +lists:sort(List))</code> might show as "<code>sorting: 0.015 s</code>".</dd> + +</dl> + + +<h3><a name="EUnit_test_representation">EUnit test representation</a></h3> + +<p>The way EUnit represents tests and test sets as data is flexible, +powerful, and concise. This section describes the representation in +detail.</p> + +<ul> +<li><a href="#Simple_test_objects">Simple test objects</a></li> +<li><a href="#Test_sets_and_deep_lists">Test sets and deep lists</a></li> +<li><a href="#Titles">Titles</a></li> +<li><a href="#Primitives">Primitives</a></li> +<li><a href="#Control">Control</a></li> +<li><a href="#Fixtures">Fixtures</a></li> +<li><a href="#Lazy_generators">Lazy generators</a></li> +</ul> + +<h4><a name="Simple_test_objects">Simple test objects</a></h4> + +A <em>simple test object</em> is one of the following: +<ul> + <li>A nullary functional value (i.e., a fun that takes zero + arguments). Examples: +<pre> fun () -> ... end</pre> +<pre> fun some_function/0</pre> +<pre> fun some_module:some_function/0</pre> + </li> + <li>A pair of atoms <code>{ModuleName, FunctionName}</code>, referring to the + function <code>ModuleName:FunctionName/0</code></li> + <li>A pair <code>{LineNumber, SimpleTest}</code>, where <code>LineNumber</code> is a + nonnegative integer and <code>SimpleTest</code> is another simple test + object. <code>LineNumber</code> should indicate the source line of the test. + Pairs like this are usually only created via <code>?_test(...)</code> macros; + see <a href="#Basic_macros">Basic macros</a>.</li> +</ul><p> +In brief, a simple test object consists of a single function that takes +no arguments (possibly annotated with some additional metadata, i.e., a +line number). Evaluation of the function either <em>succeeds</em>, by +returning some value (which is ignored), or <em>fails</em>, by throwing +an exception.</p> + +<h4><a name="Test_sets_and_deep_lists">Test sets and deep lists</a></h4> + +<p>A test set can be easily created by placing a sequence of test objects +in a list. If <code>T_1</code>, ..., <code>T_N</code> are individual test objects, then <code>[T_1, +..., T_N]</code> is a test set consisting of those objects (in that order).</p> + +<p>Test sets can be joined in the same way: if <code>S_1</code>, ..., <code>S_K</code> are test +sets, then <code>[S_1, ..., S_K]</code> is also a test set, where the tests of +<code>S_i</code> are ordered before those of <code>S_(i+1)</code>, for each subset <code>S_i</code>.</p> + +<p>Thus, the main representation of test sets is <em>deep lists</em>, and +a simple test object can be viewed as a test set containing only a +single test; there is no difference between <code>T</code> and <code>[T]</code>.</p> + +<p>A module can also be used to represent a test set; see <code>ModuleName</code> +under <a href="#Primitives">Primitives</a> below.</p> + +<h4><a name="Titles">Titles</a></h4> + +<p>Any test or test set <code>T</code> can be annotated with a title, by wrapping it +in a pair <code>{Title, T}</code>, where <code>Title</code> is a string. For convenience, any +test which is normally represented using a tuple can simply be given a +title string as the first element, i.e., writing <code>{"The Title", ...}</code> +instead of adding an extra tuple wrapper as in <code>{"The Title", {...}}</code>.</p> + + +<h4><a name="Primitives">Primitives</a></h4> + +The following are primitives, which do not contain other test sets as +arguments: +<dl> +<dt><code>ModuleName::atom()</code> +</dt> +<dd>A single atom represents a module name, and is equivalent to +<code>{module, ModuleName}</code>. This is often used as in the call +<code>eunit:test(some_module)</code>. +</dd> +<dt><code>{module, ModuleName::atom()}</code> +</dt> +<dd><p>This composes a test set from the exported test functions of the +named module, i.e., those functions with arity zero whose names end +with <code>_test</code> or <code>_test_</code>. Basically, the <code>..._test()</code> functions become +simple tests, while the <code>..._test_()</code> functions become generators.</p> + +In addition, EUnit will also look for another module whose name is +<code>ModuleName</code> plus the suffix <code>_tests</code>, and if it exists, all the tests +from that module will also be added. (If <code>ModuleName</code> already contains +the suffix <code>_tests</code>, this is not done.) E.g., the specification +<code>{module, mymodule}</code> will run all tests in the modules <code>mymodule</code> and +<code>mymodule_tests</code>. Typically, the <code>_tests</code> module should only contain +test cases that use the public interface of the main module (and no +other code). +</dd> +<dt><code>{application, AppName::atom(), Info::list()}</code> +</dt> +<dd>This is a normal Erlang/OTP application descriptor, as found in an + <code>.app</code> file. The resulting test set consists of the modules listed in + the <code>modules</code> entry in <code>Info</code>. +</dd> +<dt><code>{application, AppName::atom()}</code> +</dt> +<dd>This creates a test set from all the modules belonging to the +specified application, by consulting the application's <code>.app</code> file +(see <code>{file, FileName}</code>), or if no such file exists, by testing all +object files in the application's <tt>ebin</tt>-directory (see <code>{dir, +Path}</code>); if that does not exist, the <code>code:lib_dir(AppName)</code> directory +is used. +</dd> +<dt><code>Path::string()</code> +</dt> +<dd>A single string represents the path of a file or directory, and is +equivalent to <code>{file, Path}</code>, or <code>{dir, Path}</code>, respectively, depending +on what <code>Path</code> refers to in the file system. +</dd> +<dt><code>{file, FileName::string()}</code> +</dt> +<dd><p>If <code>FileName</code> has a suffix that indicates an object file (<code>.beam</code>), +EUnit will try to reload the module from the specified file and test it. +Otherwise, the file is assumed to be a text file containing test +specifications, which will be read using the standard library function +<code>file:path_consult/2</code>.</p> + +Unless the file name is absolute, the file is first searched for +relative to the current directory, and then using the normal search path +(<code>code:get_path()</code>). This means that the names of typical "app" files +can be used directly, without a path, e.g., <code>"mnesia.app"</code>. +</dd> +<dt><code>{dir, Path::string()}</code> +</dt> +<dd>This tests all object files in the specified directory, as if they +had been individually specified using <code>{file, FileName}</code>. +</dd> +<dt><code>{generator, GenFun::(() -> Tests)}</code> +</dt> +<dd>The generator function <code>GenFun</code> is called to produce a test +set. +</dd> +<dt><code>{generator, ModuleName::atom(), FunctionName::atom()}</code> +</dt> +<dd>The function <code>ModuleName:FunctionName()</code> is called to produce a test +set. +</dd> +<dt><code>{with, X::any(), [AbstractTestFun::((any()) -> any())]}</code> +</dt> +<dd>Distributes the value <code>X</code> over the unary functions in the list, +turning them into nullary test functions. An <code>AbstractTestFun</code> is like +an ordinary test fun, but takes one argument instead of zero - it's +basically missing some information before it can be a proper test. In +practice, <code>{with, X, [F_1, ..., F_N]}</code> is equivalent to <code>[fun () -> +F_1(X) end, ..., fun () -> F_N(X) end]</code>. This is particularly useful if +your abstract test functions are already implemented as proper +functions: <code>{with, FD, [fun filetest_a/1, fun filetest_b/1, fun +filetest_c/1]}</code> is equivalent to <code>[fun () -> filetest_a(FD) end, fun () +-> filetest_b(FD) end, fun () -> filetest_c(FD) end]</code>, but much more +compact. See also <a href="#Fixtures">Fixtures</a>, below. +</dd> +</dl> + +<h4><a name="Control">Control</a></h4> + +The following representations control how and where tests are executed: +<dl> +<dt><code>{spawn, Tests}</code></dt> +<dd>Runs the specified tests in a separate subprocess, while the current +test process waits for it to finish. This is useful for tests that need +a fresh, isolated process state. (Note that EUnit always starts at least +one such a subprocess automatically; tests are never executed by the +caller's own process.)</dd> +<dt><code>{spawn, Node::atom(), Tests}</code></dt> +<dd>Like <code>{spawn, Tests}</code>, but runs the specified tests on the given +Erlang node.</dd> +<dt><code>{timeout, Time::number(), Tests}</code></dt> +<dd>Runs the specified tests under the given timeout. Time is in +seconds; e.g., 60 means one minute and 0.1 means 1/10th of a second. If +the timeout is exceeded, the unfinished tests will be forced to +terminate. Note that if a timeout is set around a fixture, it includes +the time for setup and cleanup, and if the timeout is triggered, the +entire fixture is abruptly terminated (without running the +cleanup).</dd> +<dt><code>{inorder, Tests}</code></dt> +<dd>Runs the specified tests in strict order. Also see <code>{inparallel, +Tests}</code>. By default, tests are neither marked as <code>inorder</code> or +<code>inparallel</code>, but may be executed as the test framework chooses.</dd> +<dt><code>{inparallel, Tests}</code></dt> +<dd>Runs the specified tests in parallel (if possible). Also see +<code>{inorder, Tests}</code>.</dd> +<dt><code>{inparallel, N::integer(), Tests}</code></dt> +<dd>Like <code>{inparallel, Tests}</code>, but running no more than <code>N</code> subtests +simultaneously.</dd> +</dl> + +<h4><a name="Fixtures">Fixtures</a></h4> + +<p>A "fixture" is some state that is necessary for a particular set of +tests to run. EUnit's support for fixtures makes it easy to set up such +state locally for a test set, and automatically tear it down again when +the test set is finished, regardless of the outcome (success, failures, +timeouts, etc.).</p> + +<p>To make the descriptions simpler, we first list some definitions: +<center> +<table border="0" cellspacing="4"> +<tr> +<td><code>Setup</code></td><td><code>() -> (R::any())</code></td> +</tr> +<tr> +<td><code>SetupX</code></td><td><code>(X::any()) -> (R::any())</code></td> +</tr> +<tr> +<td><code>Cleanup</code></td><td><code>(R::any()) -> any()</code></td> +</tr> +<tr> +<td><code>CleanupX</code></td><td><code>(X::any(), R::any()) -> any()</code></td> +</tr> +<tr> +<td><code>Instantiator</code></td><td><code>((R::any()) -> Tests) | {with, [AbstractTestFun::((any()) -> any())]}</code></td> +</tr> +<tr> +<td><code>Where</code></td><td><code>local | spawn | {spawn, Node::atom()}</code></td> +</tr> +</table> +</center> +(these are explained in more detail further below.)</p> + +The following representations specify fixture handling for test sets: +<dl> +<dt><code>{setup, Setup, Tests | Instantiator}</code></dt> +<dt><code>{setup, Setup, Cleanup, Tests | Instantiator}</code></dt> +<dt><code>{setup, Where, Setup, Tests | Instantiator}</code></dt> +<dt><code>{setup, Where, Setup, Cleanup, Tests | Instantiator}</code></dt> +<dd><code>setup</code> sets up a single fixture for running all of the specified +tests, with optional teardown afterwards. The arguments are described in +detail below. +</dd> +<dt><code>{node, Node::atom(), Tests | Instantiator}</code></dt> +<dt><code>{node, Node::atom(), Args::string(), Tests | Instantiator}</code></dt> +<dd><code>node</code> is like <code>setup</code>, but with a built-in behaviour: it starts a +slave node for the duration of the tests. The atom <code>Node</code> should have +the format <code>[email protected]</code>, and <code>Args</code> are the optional +arguments to the new node; see <code>slave:start_link/3</code> for details. +</dd> +<dt><code>{foreach, Where, Setup, Cleanup, [Tests | Instantiator]}</code></dt> +<dt><code>{foreach, Setup, Cleanup, [Tests | Instantiator]}</code></dt> +<dt><code>{foreach, Where, Setup, [Tests | Instantiator]}</code></dt> +<dt><code>{foreach, Setup, [Tests | Instantiator]}</code></dt> +<dd><code>foreach</code> is used to set up a fixture and optionally tear it down +afterwards, repeated for each single one of the specified test sets. +</dd> +<dt><code>{foreachx, Where, SetupX, CleanupX, + Pairs::[{X::any(), ((X::any(), R::any()) -> Tests)}]}</code></dt> +<dt><code>{foreachx, SetupX, CleanupX, Pairs}</code></dt> +<dt><code>{foreachx, Where, SetupX, Pairs}</code></dt> +<dt><code>{foreachx, SetupX, Pairs}</code></dt> +<dd><code>foreachx</code> is like <code>foreach</code>, but uses a list of pairs, each +containing an extra argument <code>X</code> and an extended instantiator function. +</dd> +</dl> + +<p>A <code>Setup</code> function is executed just before any of the specified tests +are run, and a <code>Cleanup</code> function is executed when no more of the +specified tests will be run, regardless of the reason. A <code>Setup</code> +function takes no argument, and returns some value which will be passed +as it is to the <code>Cleanup</code> function. A <code>Cleanup</code> function should do +whatever necessary and return some arbitrary value, such as the atom +<code>ok</code>. (<code>SetupX</code> and <code>CleanupX</code> functions are similar, but receive one +additional argument: some value <code>X</code>, which depends on the context.) When +no <code>Cleanup</code> function is specified, a dummy function is used which has +no effect.</p> + +<p>An <code>Instantiator</code> function receives the same value as the <code>Cleanup</code> +function, i.e., the value returned by the <code>Setup</code> function. It should +then behave much like a generator (see <a href="#Primitives">Primitives</a>), and +return a test set whose tests have been <em>instantiated</em> with the +given value. A special case is the syntax <code>{with, [AbstractTestFun]}</code> +which represents an instantiator function that distributes the value +over a list of unary functions; see <a href="#Primitives">Primitives</a>: <code>{with, X, +[...]}</code> for more details.</p> + +A <code>Where</code> term controls how the specified tests are executed. The +default is <code>spawn</code>, which means that the current process handles the +setup and teardown, while the tests are executed in a subprocess. +<code>{spawn, Node}</code> is like <code>spawn</code>, but runs the subprocess on the +specified node. <code>local</code> means that the current process will handle both +setup/teardown and running the tests - the drawback is that if a test +times out so that the process is killed, the <em>cleanup will not be +performed</em>; hence, avoid this for persistent fixtures such as file +operations. In general, 'local' should only be used when: +<ul> + <li>the setup/teardown needs to be executed by the process that will + run the tests;</li> + <li>no further teardown needs to be done if the process is killed + (i.e., no state outside the process was affected by the setup)</li> +</ul> + +<h4><a name="Lazy_generators">Lazy generators</a></h4> + +<p>Sometimes, it can be convenient not to produce the whole set of test +descriptions before the testing begins; for example, if you want to +generate a huge amount of tests that would take up too much space to +keep in memory all at once.</p> + +<p>It is fairly easy to write a generator which, each time it is called, +either produces an empty list if it is done, or otherwise produces a +list containing a single test case plus a new generator which will +produce the rest of the tests. This demonstrates the basic pattern:</p> + +<pre> lazy_test_() -> + lazy_gen(10000). + + lazy_gen(N) -> + {generator, + fun () -> + if N > 0 -> + [?_test(...) + | lazy_gen(N-1)]; + true -> + [] + end + end}.</pre> + +<p>When EUnit traverses the test representation in order to run the tests, +the new generator will not be called to produce the next test until the +previous test has been executed.</p> + +Note that it is easiest to write this kind of recursive generator using +a help function, like the <code>lazy_gen/1</code> function above. It can also be +written using a recursive fun, if you prefer to not clutter your +function namespace and are comfortable with writing that kind of code. + +<hr> +<div class="navbar"><a name="#navbar_bottom"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div> +<p><i>Generated by EDoc, Apr 22 2009, 22:37:19.</i></p> +</body> +</html> diff --git a/lib/eunit/doc/overview.edoc b/lib/eunit/doc/overview.edoc new file mode 100644 index 0000000000..2583f0be25 --- /dev/null +++ b/lib/eunit/doc/overview.edoc @@ -0,0 +1,1028 @@ + + -*- html -*- + + EUnit overview page + +@title EUnit - a Lightweight Unit Testing Framework for Erlang + +@author Richard Carlsson <[email protected]> + [http://user.it.uu.se/~richardc/] +@author Micka�l R�mond <[email protected]> + [http://www.process-one.net/] +@copyright 2004-2007 Micka�l R�mond, Richard Carlsson +@version {@version}, {@date} {@time} + +@doc EUnit is a unit testing framework for Erlang. It is very powerful +and flexible, is easy to use, and has small syntactical overhead. + +<ul> +<li>{@section Unit testing}</li> +<li>{@section Terminology}</li> +<li>{@section Getting started}</li> +<li>{@section EUnit macros}</li> +<li>{@section EUnit test representation}</li> +</ul> + +EUnit builds on ideas from the family of unit testing frameworks for +Object Oriented languages that originated with JUnit by Beck and Gamma +(and Beck's previous framework SUnit for Smalltalk). However, EUnit uses +techniques more adapted to functional and concurrent programming, and is +typically less verbose than its relatives. + +Although EUnit uses many preprocessor macros, they have been designed to +be as nonintrusive as possible, and should not cause conflicts with +existing code. Adding EUnit tests to a module should thus not normally +require changing existing code. Furthermore, tests that only exercise +the exported functions of a module can always be placed in a completely +separate module, avoiding any conflicts entirely. + +== Unit testing == + +Unit Testing is testing of individual program "units" in relative +isolation. There is no particular size requirement: a unit can be a +function, a module, a process, or even a whole application, but the most +typical testing units are individual functions or modules. In order to +test a unit, you specify a set of individual tests, set up the smallest +necessary environment for being able to run those tests (often, you +don't need to do any setup at all), you run the tests and collect the +results, and finally you do any necessary cleanup so that the test can +be run again later. A Unit Testing Framework tries to help you in each +stage of this process, so that it is easy to write tests, easy to run +them, and easy to see which tests failed (so you can fix the bugs). + +=== Advantages of unit testing === + +<dl> + <dt>Reduces the risks of changing the program</dt> + <dd>Most programs will be modified during their lifetime: bugs will be + fixed, features will be added, optimizations may become necessary, or + the code will need to be refactored or cleaned up in other ways to + make it easier to work with. But every change to a working program is + a risk of introducing new bugs - or reintroducing bugs that had + previously been fixed. Having a set of unit tests that you can run + with very little effort makes it easy to know that the code still + works as it should (this use is called <em>regression testing</em>; + see {@section Terminology}). This goes a long way to reduce the + resistance to changing and refactoring code.</dd> + <dt>Helps guide and speed up the development process</dt> + <dd>By focusing on getting the code to pass the tests, the programmer + can become more productive, not overspecify or get lost in premature + optimizations, and create code that is correct from the very beginning + (so-called <em>test-driven development</em>; see {@section + Terminology}).</dd> + <dt>Helps separate interface from implementation</dt> + <dd>When writing tests, the programmer may discover dependencies + (in order to get the tests to run) that ought not to be there, and + which need to be abstracted away to get a cleaner design. This helps + eliminate bad dependencies before they spread throughout the + code.</dd> + <dt>Makes component integration easier</dt> + <dd>By testing in a bottom-up fashion, beginning with the smallest + program units and creating a confidence in that they work as they + should, it becomes easier to test that a higher-level component, + consisting of several such units, also behaves according to + specification (known as <em>integration testing</em>; see {@section + Terminology}).</dd> + <dt>Is self-documenting</dt> + <dd>The tests can be read as documentation, typically showing both + examples of correct and incorrect usage, along with the expected + consequences.</dd> +</dl> + +== Terminology == + +<dl> + <dt>Unit testing</dt> + <dd>Testing that a program unit behaves as it is supposed to do (in + itself), according to its specifications. Unit tests have an important + function as regression tests, when the program later is modified for + some reason, since they check that the program still behaves according + to specification.</dd> + <dt>Regression testing</dt> + <dd>Running a set of tests after making changes to a program, to check + that the program behaves as it did before the changes (except, of + course, for any intentional changes in behaviour). Unit tests are + important as regression tests, but regression testing can involve more + than just unit testing, and may also test behaviour that might not be + part of the normal specification (such as bug-for-bug-compatibility). + </dd> + <dt>Integration testing</dt> + <dd>Testing that a number of individually developed program units + (assumed to already have been separately unit tested) work together as + expected. Depending on the system being developed, integration testing + may be as simple as "just another level of unit testing", but might + also involve other kinds of tests (compare <em>system testing</em>). +</dd> + <dt>System testing</dt> + <dd>Testing that a complete system behaves according to its + specification. Specifically, system testing should not require knowing + any details about the implementation. It typically involves testing + many different aspects of the system behaviour apart from the basic + functionality, such as performance, usability, and reliability.</dd> + <dt>Test-driven development</dt> + <dd>A program development technique where you continuously write tests + <em>before</em> you implement the code that is supposed to pass those + tests. This can help you focus on solving the right problems, and not + make a more complicated implementation than necessary, by letting the + unit tests determine when a program is "done": if it fulfils its + specifications, there is no need to keep adding functionality.</dd> + <dt>Mock object</dt> + <dd>Sometimes, testing some unit `A' (e.g., a function) requires that + it collaborates somehow with some other unit `B' (perhaps being passed + as an argument, or by reference) - but `B' has not been implemented + yet. A "mock object" - an object which, for the purposes of testing + `A', looks and behaves like a real `B' - might then be used instead. + (This is of course only useful if it would be significantly more work + to implement a real `B' than to create a mock object.)</dd> + <dt>Test case</dt> + <dd>A single, well-defined test, that somehow can be uniquely + identified. When executed, the test case either <em>passes</em> or + <em>fails</em>; the test report should identify exactly which test + cases failed.</dd> + <dt>Test suite</dt> + <dd>A collection of test cases, generally with a specific, common + target for testing, such as a single function, module, or subsystem. A + test suite may also be recursively composed by smaller test + suites.</dd> +</dl> + +== Getting started == +<ul> + <li>{@section Including the EUnit header file}</li> + <li>{@section Writing simple test functions}</li> + <li>{@section Running EUnit}</li> + <li>{@section Writing test generating functions}</li> + <li>{@section An example}</li> + <li>{@section Disabling testing}</li> + <li>{@section Avoiding compile-time dependency on EUnit}</li> +</ul> + +=== Including the EUnit header file === + +The simplest way to use EUnit in an Erlang module is to add the +following line at the beginning of the module (after the `-module' +declaration, but before any function definitions): +```-include_lib("eunit/include/eunit.hrl").''' + +This will have the following effect: +<ul> + <li>Creates an exported function `test()' (unless testing is turned + off, and the module does not already contain a test() function), that + can be used to run all the unit tests defined in the module</li> + <li>Causes all functions whose names match `..._test()' or `..._test_()' + to be automatically exported from the module (unless testing is + turned off, or the `EUNIT_NOAUTO' macro is defined)</li> + <li>Makes all the preprocessor macros of EUnit available, to help + writing tests</li> +</ul> + +<strong>Note:</strong> For `-include_lib(...)' to work, the Erlang +module search path <em>must</em> contain a directory whose name ends in +`eunit/ebin' (pointing to the `ebin' subdirectory of the EUnit +installation directory). If EUnit is installed as `lib/eunit' under your +Erlang/OTP system directory, its `ebin' subdirectory will be +automatically added to the search path when Erlang starts. Otherwise, +you need to add the directory explicitly, by passing a `-pa' flag to the +`erl' or `erlc' command. For example, a Makefile could contain the +following action for compiling `.erl' files: +```erlc -pa "path/to/eunit/ebin" $(ERL_COMPILE_FLAGS) -o$(EBIN) $<''' +or if you want Eunit to always be available when you run Erlang +interactively, you can add a line like the following to your +`$HOME/.erlang' file: +```code:add_path("/path/to/eunit/ebin").''' + +=== Writing simple test functions === + +The EUnit framework makes it extremely easy to write unit tests in +Erlang. There are a few different ways of writing them, though, so we +start with the simplest: + +A function with a name ending in `..._test()' is recognized by EUnit as +a simple test function - it takes no arguments, and its execution either +succeeds (returning some arbitrary value that EUnit will throw away), or +fails by throwing an exception of some kind (or by not terminating, in +which case it will be aborted after a while). + +An example of a simple test function could be the following: +```reverse_test() -> lists:reverse([1,2,3]).''' +This just tests that the function `lists:reverse(List)' does not crash +when `List' is `[1,2,3]'. It is not a great test, but many people write +simple functions like this one to test the basic functionality of their +code, and those tests can be used directly by EUnit, without changes, +as long as their function names match. + +==== Use exceptions to signal failure ==== + +To write more interesting tests, we need to make them crash (throw an +exception) when they don't get the result they expect. A simple way of +doing this is to use pattern matching with `=', as in the following +examples: +```reverse_nil_test() -> [] = lists:reverse([]). + reverse_one_test() -> [1] = lists:reverse([1]). + reverse_two_test() -> [2,1] = lists:reverse([1,2]). +''' +If there was some bug in `lists:reverse/1' that made it return something +other than `[2,1]' when it got `[1,2]' as input, then the last test +above would throw a `badmatch' error. The first two (we assume they do +not get a `badmatch') would simply return `[]' and `[1]', respectively, +so both succeed. (Note that EUnit is not psychic: if you write a test +that returns a value, even if it is the wrong value, EUnit will consider +it a success. You must make sure that the test is written so that it +causes a crash if the result is not what it should be.) + +==== Using assert macros ==== + +If you want to use Boolean operators for your tests, the `assert' +macro comes in handy (see {@section EUnit macros} for details): +```length_test() -> ?assert(length([1,2,3]) =:= 3).''' +The `?assert(Expression)' macro will evaluate `Expression', and if that +does not evaluate to `true', it will throw an exception; otherwise it +just returns `ok'. In the above example, the test will thus fail if the +call to `length' does not return 3. + +=== Running EUnit === + +If you have added the declaration +`-include_lib("eunit/include/eunit.hrl")' to your module, as described +above, you only need to compile the module, and run the automatically +exported function `test()'. For example, if your module was named `m', +then calling `m:test()' will run EUnit on all the tests defined in the +module. You do not need to write `-export' declarations for the test +functions. This is all done by magic. + +You can also use the function {@link eunit:test/1} to run arbitrary +tests, for example to try out some more advanced test descriptors (see +{@section EUnit test representation}). For example, running +``eunit:test(m)'' does the same thing as the auto-generated function +``m:test()'', while ``eunit:test({inparallel, m})'' runs the same test +cases but executes them all in parallel. + +==== Putting tests in separate modules ==== + +If you want to separate your test code from your normal code (at least +for testing the exported functions), you can simply write the test +functions in a module named `m_tests' (note: not `m_test'), if your +module is named `m'. Then, whenever you ask EUnit to test the module +`m', it will also look for the module `m_tests' and run those tests as +well. See `ModuleName' in the section {@section Primitives} for details. + +==== EUnit captures standard output ==== + +If your test code writes to the standard output, you may be surprised to +see that the text does not appear on the console when the tests are +running. This is because EUnit captures all standard output from test +functions (this also includes setup and cleanup functions, but not +generator functions), so that it can be included in the test report if +errors occur. To bypass EUnit and print text directly to the console +while testing, you can write to the `user' output stream, as in +`io:format(user, "~w", [Term])'. The recommended way of doing this is to +use the EUnit {@section Debugging macros}, which make it much simpler. + +=== Writing test generating functions === + +A drawback of simple test functions is that you must write a separate +function (with a separate name) for each test case. A more compact way +of writing tests (and much more flexible, as we shall see), is to write +functions that <em>return</em> tests, instead of <em>being</em> tests. + +A function with a name ending in `..._test_()' (note the final +underscore) is recognized by EUnit as a <em>test generator</em> +function. Test generators return a <em>representation</em> of a <em>set +of tests</em> to be executed by EUnit. + +==== Representing a test as data ==== + +The most basic representation of a test is a single fun-expression that +takes no arguments. For example, the following test generator: +```basic_test_() -> + fun () -> ?assert(1 + 1 =:= 2) end.''' +will have the same effect as the following simple test: +```simple_test() -> + ?assert(1 + 1 =:= 2).''' +(in fact, EUnit will handle all simple tests just like it handles +fun-expressions: it will put them in a list, and run them one by one). + +==== Using macros to write tests ==== + +To make tests more compact and readable, as well as automatically add +information about the line number in the source code where a test +occurred (and reduce the number of characters you have to type), you can +use the `_test' macro (note the initial underscore character), like +this: +```basic_test_() -> + ?_test(?assert(1 + 1 =:= 2)).''' +The `_test' macro takes any expression (the "body") as argument, and +places it within a fun-expression (along with some extra information). +The body can be any kind of test expression, just like the body of a +simple test function. + +==== Underscore-prefixed macros create test objects ==== + +But this example can be made even shorter! Most test macros, such as the +family of `assert' macros, have a corresponding form with an initial +underscore character, which automatically adds a `?_test(...)' wrapper. +The above example can then simply be written: +```basic_test_() -> + ?_assert(1 + 1 =:= 2).''' +which has exactly the same meaning (note the `_assert' instead of +`assert'). You can think of the initial underscore as signalling +<em>test object</em>. + +=== An example === + +Sometimes, an example says more than a thousand words. The following +small Erlang module shows how EUnit can be used in practice. +```-module(fib). + -export([fib/1]). + -include_lib("eunit/include/eunit.hrl"). + + fib(0) -> 1; + fib(1) -> 1; + fib(N) when N > 1 -> fib(N-1) + fib(N-2). + + fib_test_() -> + [?_assert(fib(0) =:= 1), + ?_assert(fib(1) =:= 1), + ?_assert(fib(2) =:= 2), + ?_assert(fib(3) =:= 3), + ?_assert(fib(4) =:= 5), + ?_assert(fib(5) =:= 8), + ?_assertException(error, function_clause, fib(-1)), + ?_assert(fib(31) =:= 2178309) + ].''' + +(Author's note: When I first wrote this example, I happened to write a +`*' instead of `+' in the `fib' function. Of course, this showed up +immediately when I ran the tests.) + +See {@section EUnit test representation} for a full list of all the ways +you can specify test sets in EUnit. + +=== Disabling testing === + +Testing can be turned off by defining the `NOTEST' macro when compiling, +for example as an option to `erlc', as in: +```erlc -DNOTEST my_module.erl''' +or by adding a macro definition to the code, <em>before the EUnit header +file is included</em>: +```-define(NOTEST, 1).''' +(the value is not important, but should typically be 1 or `true'). +Note that unless the `EUNIT_NOAUTO' macro is defined, disabling testing +will also automatically strip all test functions from the code, except +for any that are explicitly declared as exported. + +For instance, to use EUnit in your application, but with testing turned +off by default, put the following lines in a header file: +```-define(NOTEST, true). + -include_lib("eunit/include/eunit.hrl").''' +and then make sure that every module of your application includes that +header file. This means that you have a only a single place to modify in +order to change the default setting for testing. To override the `NOTEST' +setting without modifying the code, you can define `TEST' in a compiler +option, like this: +```erlc -DTEST my_module.erl''' + +See {@section Compilation control macros} for details about these +macros. + +=== Avoiding compile-time dependency on EUnit === + +If you are distributing the source code for your application for other +people to compile and run, you probably want to ensure that the code +compiles even if EUnit is not available. Like the example in the +previous section, you can put the following lines in a common header +file: +```-ifdef(TEST). + -include_lib("eunit/include/eunit.hrl"). + -endif.''' +and, of course, also make sure that you place all test code that uses +EUnit macros within `-ifdef(TEST)' or `-ifdef(EUNIT)' sections. + + +== EUnit macros == + +Although all the functionality of EUnit is available even without the +use of preprocessor macros, the EUnit header file defines a number of +such macros in order to make it as easy as possible to write unit tests +as compactly as possible and without getting too many details in the +way. + +Except where explicitly stated, using EUnit macros will never introduce +run-time dependencies on the EUnit library code, regardless of whether +your code is compiled with testing enabled or disabled. + +<ul> +<li>{@section Basic macros}</li> +<li>{@section Compilation control macros}</li> +<li>{@section Utility macros}</li> +<li>{@section Assert macros}</li> +<li>{@section Macros for running external commands}</li> +<li>{@section Debugging macros}</li> +</ul> + +=== Basic macros === + +<dl> +<dt>`_test(Expr)'</dt> +<dd>Turns `Expr' into a "test object", by wrapping it in a +fun-expression and a source line number. Technically, this is the same +as `{?LINE, fun () -> (Expr) end}'. +</dd> +</dl> + +=== Compilation control macros === + +<dl> +<dt>`EUNIT'</dt> +<dd>This macro is always defined to `true' whenever EUnit is enabled at +compile time. This is typically used to place testing code within +conditional compilation, as in: +```-ifdef(EUNIT). + % test code here + ... + -endif.''' +e.g., to ensure that the code can be compiled without including the +EUnit header file, when testing is disabled. See also the macros `TEST' +and `NOTEST'. +</dd> + +<dt>`EUNIT_NOAUTO'</dt> +<dd>If this macro is defined, the automatic exporting or stripping of +test functions will be disabled. +</dd> + +<dt>`TEST'</dt> +<dd>This macro is always defined (to `true', unless previously defined +by the user to have another value) whenever EUnit is enabled at compile +time. This can be used to place testing code within conditional +compilation; see also the macros `NOTEST' and `EUNIT'. + +For testing code that is strictly dependent on EUnit, it may be +preferable to use the `EUNIT' macro for this purpose, while for code +that uses more generic testing conventions, using the `TEST' macro may +be preferred. + +The `TEST' macro can also be used to override the `NOTEST' macro. If +`TEST' is defined <em>before</em> the EUnit header file is +included (even if `NOTEST' is also defined), then the code will be +compiled with EUnit enabled. +</dd> + +<dt>`NOTEST'</dt> +<dd>This macro is always defined (to `true', unless previously defined +by the user to have another value) whenever EUnit is <em>disabled</em> +at compile time. (Compare the `TEST' macro.) + +This macro can also be used for conditional compilation, but is more +typically used to disable testing: If `NOTEST' is defined +<em>before</em> the EUnit header file is included, and `TEST' +is <em>not</em> defined, then the code will be compiled with EUnit +disabled. See also {@section Disabling testing}. +</dd> + +<dt>`NOASSERT'</dt> +<dd>If this macro is defined, the assert macros will have no effect, +when testing is also disabled. See {@section Assert macros}. When +testing is enabled, the assert macros are always enabled automatically +and cannot be disabled. +</dd> + +<dt>`ASSERT'</dt> +<dd>If this macro is defined, it overrides the NOASSERT macro, forcing +the assert macros to always be enabled regardless of other settings. +</dd> + +<dt>`NODEBUG'</dt> +<dd>If this macro is defined, the debugging macros will have no effect. +See {@section Debugging macros}. `NODEBUG' also implies `NOASSERT', +unless testing is enabled. +</dd> + +<dt>`DEBUG'</dt> +<dd>If this macro is defined, it overrides the NODEBUG macro, forcing +the debugging macros to be enabled. +</dd> +</dl> + +=== Utility macros === + +The following macros can make tests more compact and readable: + +<dl> +<dt>`LET(Var,Arg,Expr)'</dt> +<dd>Creates a local binding `Var = Arg' in `Expr'. (This is the same as +`(fun(Var)->(Expr)end)(Arg)'.) Note that the binding is not exported +outside of `Expr', and that within `Expr', this binding of `Var' will +shadow any binding of `Var' in the surrounding scope. +</dd> +<dt>`IF(Cond,TrueCase,FalseCase)'</dt> +<dd>Evaluates `TrueCase' if `Cond' evaluates to `true', or otherwise +evaluates `FalseCase' if `Cond' evaluates to `false'. (This is the same +as `(case (Cond) of true->(TrueCase); false->(FalseCase) end)'.) Note +that it is an error if `Cond' does not yield a boolean value. +</dd> +</dl> + +=== Assert macros === + +(Note that these macros also have corresponding forms which start with +an "`_'" (underscore) character, as in `?_assert(BoolExpr)', that create +a "test object" instead of performing the test immediately. This is +equivalent to writing `?_test(assert(BoolExpr))', etc.) + +If the macro `NOASSERT' is defined before the EUnit header file is +included, these macros have no effect when testing is also disabled; see +{@section Compilation control macros} for details. + +<dl> +<dt>`assert(BoolExpr)'</dt> +<dd>Evaluates the expression `BoolExpr', if testing is enabled. Unless +the result is `true', an informative exception will be generated. If +there is no exception, the result of the macro expression is the atom +`ok', and the value of `BoolExpr' is discarded. If testing is disabled, +the macro will not generate any code except the atom `ok', and +`BoolExpr' will not be evaluated. + +Typical usage: +```?assert(f(X, Y) =:= [])''' + +The `assert' macro can be used anywhere in a program, not just in unit +tests, to check pre/postconditions and invariants. For example: +```some_recursive_function(X, Y, Z) -> + ?assert(X + Y > Z), + ...''' +</dd> +<dt>`assertNot(BoolExpr)'</dt> +<dd>Equivalent to `assert(not (BoolExpr))'. +</dd> +<dt>`assertMatch(GuardedPattern, Expr)'</dt> +<dd>Evaluates `Expr' and matches the result against `GuardedPattern', if +testing is enabled. If the match fails, an informative exception will be +generated; see the `assert' macro for further details. `GuardedPattern' +can be anything that you can write on the left hand side of the `->' +symbol in a case-clause, except that it cannot contain comma-separated +guard tests. + +The main reason for using `assertMatch' also for simple matches, instead +of matching with `=', is that it produces more detailed error messages. + +Examples: +```?assertMatch({found, {fred, _}}, lookup(bloggs, Table))''' +```?assertMatch([X|_] when X > 0, binary_to_list(B))''' +</dd> +<dt>`assertEqual(Expect, Expr)'</dt> +<dd>Evaluates the expressions `Expect' and `Expr' and compares the +results for equality, if testing is enabled. If the values are not +equal, an informative exception will be generated; see the `assert' +macro for further details. + +`assertEqual' is more suitable than than `assertMatch' when the +left-hand side is a computed value rather than a simple pattern, and +gives more details than `?assert(Expect =:= Expr)'. + +Examples: +```?assertEqual("b" ++ "a", lists:reverse("ab"))''' +```?assertEqual(foo(X), bar(Y))''' +</dd> +<dt>`assertException(ClassPattern, TermPattern, Expr)'</dt> +<dt>`assertError(TermPattern, Expr)'</dt> +<dt>`assertExit(TermPattern, Expr)'</dt> +<dt>`assertThrow(TermPattern, Expr)'</dt> +<dd>Evaluates `Expr', catching any exception and testing that it matches +the expected `ClassPattern:TermPattern'. If the match fails, or if no +exception is thrown by `Expr', an informative exception will be +generated; see the `assert' macro for further details. The +`assertError', `assertExit', and `assertThrow' macros, are equivalent to +using `assertException' with a `ClassPattern' of `error', `exit', or +`throw', respectively. + +Examples: +```?assertError(badarith, X/0)''' +```?assertExit(normal, exit(normal))''' +```?assertException(throw, {not_found,_}, throw({not_found,42}))''' +</dd> +</dl> + +=== Macros for running external commands === + +Keep in mind that external commands are highly dependent on the +operating system. You can use the standard library function `os:type()' +in test generator functions, to produce different sets of tests +depending on the current operating system. + +Note: these macros introduce a run-time dependency on the EUnit library +code, if compiled with testing enabled. + +<dl> +<dt>`assertCmd(CommandString)'</dt> +<dd>Runs `CommandString' as an external command, if testing is enabled. +Unless the returned status value is 0, an informative exception will be +generated. If there is no exception, the result of the macro expression +is the atom `ok'. If testing is disabled, the macro will not generate +any code except the atom `ok', and the command will not be executed. + +Typical usage: +```?assertCmd("mkdir foo")''' +</dd> +<dt>`assertCmdStatus(N, CommandString)'</dt> +<dd>Like the `assertCmd(CommandString)' macro, but generates an +exception unless the returned status value is `N'. +</dd> +<dt>`assertCmdOutput(Text, CommandString)'</dt> +<dd>Runs `CommandString' as an external command, if testing is enabled. +Unless the output produced by the command exactly matches the specified +string `Text', an informative exception will be generated. (Note that +the output is normalized to use a single LF character as line break on +all platforms.) If there is no exception, the result of the macro +expression is the atom `ok'. If testing is disabled, the macro will not +generate any code except the atom `ok', and the command will not be +executed. +</dd> +<dt>`cmd(CommandString)'</dt> +<dd>Runs `CommandString' as an external command. Unless the returned +status value is 0 (indicating success), an informative exception will be +generated; otherwise, the result of the macro expression is the output +produced by the command, as a flat string. The output is normalized to +use a single LF character as line break on all platforms. + +This macro is useful in the setup and cleanup sections of fixtures, +e.g., for creating and deleting files or perform similar operating +system specific tasks, to make sure that the test system is informed of +any failures. + +A Unix-specific example: +```{setup, + fun () -> ?cmd("mktemp") end, + fun (FileName) -> ?cmd("rm " ++ FileName) end, + ...}''' +</dd> +</dl> + +=== Debugging macros === + +To help with debugging, EUnit defines several useful macros for printing +messages directly to the console (rather than to the standard output). +Furthermore, these macros all use the same basic format, which includes +the file and line number where they occur, making it possible in some +development environments (e.g., when running Erlang in an Emacs buffer) +to simply click on the message and jump directly to the corresponding +line in the code. + +If the macro `NODEBUG' is defined before the EUnit header file is +included, these macros have no effect; see +{@section Compilation control macros} for details. + +<dl> +<dt>`debugHere'</dt> +<dd>Just prints a marker showing the current file and line number. Note +that this is an argument-less macro. The result is always `ok'.</dd> +<dt>`debugMsg(Text)'</dt> +<dd>Outputs the message `Text' (which can be a plain string, an IO-list, +or just an atom). The result is always `ok'.</dd> +<dt>`debugFmt(FmtString, Args)'</dt> +<dd>This formats the text like `io:format(FmtString, Args)' and outputs +it like `debugMsg'. The result is always `ok'.</dd> +<dt>`debugVal(Expr)'</dt> +<dd>Prints both the source code for `Expr' and its current value. E.g., +`?debugVal(f(X))' might be displayed as "`f(X) = 42'". (Large terms are +shown truncated.) The result is always the value of `Expr', so this +macro can be wrapped around any expression to display its value when +the code is compiled with debugging enabled.</dd> +<dt>`debugTime(Text,Expr)'</dt> +<dd>Prints `Text' and the wall clock time for evaluation of `Expr'. The +result is always the value of `Expr', so this macro can be wrapped +around any expression to show its run time when the code is compiled +with debugging enabled. For example, `List1 = ?debugTime("sorting", +lists:sort(List))' might show as "`sorting: 0.015 s'".</dd> + +</dl> + + +== EUnit test representation == + +The way EUnit represents tests and test sets as data is flexible, +powerful, and concise. This section describes the representation in +detail. + +<ul> +<li>{@section Simple test objects}</li> +<li>{@section Test sets and deep lists}</li> +<li>{@section Titles}</li> +<li>{@section Primitives}</li> +<li>{@section Control}</li> +<li>{@section Fixtures}</li> +<li>{@section Lazy generators}</li> +</ul> + +=== Simple test objects === + +A <em>simple test object</em> is one of the following: +<ul> + <li>A nullary functional value (i.e., a fun that takes zero + arguments). Examples: +```fun () -> ... end''' +```fun some_function/0''' +```fun some_module:some_function/0''' + </li> + <li>A pair of atoms `{ModuleName, FunctionName}', referring to the + function `ModuleName:FunctionName/0'</li> + <li>A pair `{LineNumber, SimpleTest}', where `LineNumber' is a + nonnegative integer and `SimpleTest' is another simple test + object. `LineNumber' should indicate the source line of the test. + Pairs like this are usually only created via `?_test(...)' macros; + see {@section Basic macros}.</li> +</ul> +In brief, a simple test object consists of a single function that takes +no arguments (possibly annotated with some additional metadata, i.e., a +line number). Evaluation of the function either <em>succeeds</em>, by +returning some value (which is ignored), or <em>fails</em>, by throwing +an exception. + +=== Test sets and deep lists === + +A test set can be easily created by placing a sequence of test objects +in a list. If `T_1', ..., `T_N' are individual test objects, then `[T_1, +..., T_N]' is a test set consisting of those objects (in that order). + +Test sets can be joined in the same way: if `S_1', ..., `S_K' are test +sets, then `[S_1, ..., S_K]' is also a test set, where the tests of +`S_i' are ordered before those of `S_(i+1)', for each subset `S_i'. + +Thus, the main representation of test sets is <em>deep lists</em>, and +a simple test object can be viewed as a test set containing only a +single test; there is no difference between `T' and `[T]'. + +A module can also be used to represent a test set; see `ModuleName' +under {@section Primitives} below. + +=== Titles === + +Any test or test set `T' can be annotated with a title, by wrapping it +in a pair `{Title, T}', where `Title' is a string. For convenience, any +test which is normally represented using a tuple can simply be given a +title string as the first element, i.e., writing `{"The Title", ...}' +instead of adding an extra tuple wrapper as in `{"The Title", {...}}'. + + +=== Primitives === + +The following are primitives, which do not contain other test sets as +arguments: +<dl> +<dt>`ModuleName::atom()' +</dt> +<dd>A single atom represents a module name, and is equivalent to +`{module, ModuleName}'. This is often used as in the call +`eunit:test(some_module)'. +</dd> +<dt>`{module, ModuleName::atom()}' +</dt> +<dd>This composes a test set from the exported test functions of the +named module, i.e., those functions with arity zero whose names end +with `_test' or `_test_'. Basically, the `..._test()' functions become +simple tests, while the `..._test_()' functions become generators. + +In addition, EUnit will also look for another module whose name is +`ModuleName' plus the suffix `_tests', and if it exists, all the tests +from that module will also be added. (If `ModuleName' already contains +the suffix `_tests', this is not done.) E.g., the specification +`{module, mymodule}' will run all tests in the modules `mymodule' and +`mymodule_tests'. Typically, the `_tests' module should only contain +test cases that use the public interface of the main module (and no +other code). +</dd> +<dt>`{application, AppName::atom(), Info::list()}' +</dt> +<dd>This is a normal Erlang/OTP application descriptor, as found in an + `.app' file. The resulting test set consists of the modules listed in + the `modules' entry in `Info'. +</dd> +<dt>`{application, AppName::atom()}' +</dt> +<dd>This creates a test set from all the modules belonging to the +specified application, by consulting the application's `.app' file +(see `{file, FileName}'), or if no such file exists, by testing all +object files in the application's <tt>ebin</tt>-directory (see `{dir, +Path}'); if that does not exist, the `code:lib_dir(AppName)' directory +is used. +</dd> +<dt>`Path::string()' +</dt> +<dd>A single string represents the path of a file or directory, and is +equivalent to `{file, Path}', or `{dir, Path}', respectively, depending +on what `Path' refers to in the file system. +</dd> +<dt>`{file, FileName::string()}' +</dt> +<dd>If `FileName' has a suffix that indicates an object file (`.beam'), +EUnit will try to reload the module from the specified file and test it. +Otherwise, the file is assumed to be a text file containing test +specifications, which will be read using the standard library function +`file:path_consult/2'. + +Unless the file name is absolute, the file is first searched for +relative to the current directory, and then using the normal search path +(`code:get_path()'). This means that the names of typical "app" files +can be used directly, without a path, e.g., `"mnesia.app"'. +</dd> +<dt>`{dir, Path::string()}' +</dt> +<dd>This tests all object files in the specified directory, as if they +had been individually specified using `{file, FileName}'. +</dd> +<dt>`{generator, GenFun::(() -> Tests)}' +</dt> +<dd>The generator function `GenFun' is called to produce a test +set. +</dd> +<dt>`{generator, ModuleName::atom(), FunctionName::atom()}' +</dt> +<dd>The function `ModuleName:FunctionName()' is called to produce a test +set. +</dd> +<dt>`{with, X::any(), [AbstractTestFun::((any()) -> any())]}' +</dt> +<dd>Distributes the value `X' over the unary functions in the list, +turning them into nullary test functions. An `AbstractTestFun' is like +an ordinary test fun, but takes one argument instead of zero - it's +basically missing some information before it can be a proper test. In +practice, `{with, X, [F_1, ..., F_N]}' is equivalent to `[fun () -> +F_1(X) end, ..., fun () -> F_N(X) end]'. This is particularly useful if +your abstract test functions are already implemented as proper +functions: `{with, FD, [fun filetest_a/1, fun filetest_b/1, fun +filetest_c/1]}' is equivalent to `[fun () -> filetest_a(FD) end, fun () +-> filetest_b(FD) end, fun () -> filetest_c(FD) end]', but much more +compact. See also {@section Fixtures}, below. +</dd> +</dl> + +=== Control === + +The following representations control how and where tests are executed: +<dl> +<dt>`{spawn, Tests}'</dt> +<dd>Runs the specified tests in a separate subprocess, while the current +test process waits for it to finish. This is useful for tests that need +a fresh, isolated process state. (Note that EUnit always starts at least +one such a subprocess automatically; tests are never executed by the +caller's own process.)</dd> +<dt>`{spawn, Node::atom(), Tests}'</dt> +<dd>Like `{spawn, Tests}', but runs the specified tests on the given +Erlang node.</dd> +<dt>`{timeout, Time::number(), Tests}'</dt> +<dd>Runs the specified tests under the given timeout. Time is in +seconds; e.g., 60 means one minute and 0.1 means 1/10th of a second. If +the timeout is exceeded, the unfinished tests will be forced to +terminate. Note that if a timeout is set around a fixture, it includes +the time for setup and cleanup, and if the timeout is triggered, the +entire fixture is abruptly terminated (without running the +cleanup).</dd> +<dt>`{inorder, Tests}'</dt> +<dd>Runs the specified tests in strict order. Also see `{inparallel, +Tests}'. By default, tests are neither marked as `inorder' or +`inparallel', but may be executed as the test framework chooses.</dd> +<dt>`{inparallel, Tests}'</dt> +<dd>Runs the specified tests in parallel (if possible). Also see +`{inorder, Tests}'.</dd> +<dt>`{inparallel, N::integer(), Tests}'</dt> +<dd>Like `{inparallel, Tests}', but running no more than `N' subtests +simultaneously.</dd> +</dl> + +=== Fixtures === + +A "fixture" is some state that is necessary for a particular set of +tests to run. EUnit's support for fixtures makes it easy to set up such +state locally for a test set, and automatically tear it down again when +the test set is finished, regardless of the outcome (success, failures, +timeouts, etc.). + +To make the descriptions simpler, we first list some definitions: +<center> +<table border="0" cellspacing="4"> +<tr> +<td>`Setup'</td><td>`() -> (R::any())'</td> +</tr> +<tr> +<td>`SetupX'</td><td>`(X::any()) -> (R::any())'</td> +</tr> +<tr> +<td>`Cleanup'</td><td>`(R::any()) -> any()'</td> +</tr> +<tr> +<td>`CleanupX'</td><td>`(X::any(), R::any()) -> any()'</td> +</tr> +<tr> +<td>`Instantiator'</td><td>`((R::any()) -> Tests) | {with, [AbstractTestFun::((any()) -> any())]}'</td> +</tr> +<tr> +<td>`Where'</td><td>`local | spawn | {spawn, Node::atom()}'</td> +</tr> +</table> +</center> +(these are explained in more detail further below.) + +The following representations specify fixture handling for test sets: +<dl> +<dt>`{setup, Setup, Tests | Instantiator}'</dt> +<dt>`{setup, Setup, Cleanup, Tests | Instantiator}'</dt> +<dt>`{setup, Where, Setup, Tests | Instantiator}'</dt> +<dt>`{setup, Where, Setup, Cleanup, Tests | Instantiator}'</dt> +<dd>`setup' sets up a single fixture for running all of the specified +tests, with optional teardown afterwards. The arguments are described in +detail below. +</dd> +<dt>`{node, Node::atom(), Tests | Instantiator}'</dt> +<dt>`{node, Node::atom(), Args::string(), Tests | Instantiator}'</dt> +<dd>`node' is like `setup', but with a built-in behaviour: it starts a +slave node for the duration of the tests. The atom `Node' should have +the format `[email protected]', and `Args' are the optional +arguments to the new node; see `slave:start_link/3' for details. +</dd> +<dt>`{foreach, Where, Setup, Cleanup, [Tests | Instantiator]}'</dt> +<dt>`{foreach, Setup, Cleanup, [Tests | Instantiator]}'</dt> +<dt>`{foreach, Where, Setup, [Tests | Instantiator]}'</dt> +<dt>`{foreach, Setup, [Tests | Instantiator]}'</dt> +<dd>`foreach' is used to set up a fixture and optionally tear it down +afterwards, repeated for each single one of the specified test sets. +</dd> +<dt>`{foreachx, Where, SetupX, CleanupX, + Pairs::[{X::any(), ((X::any(), R::any()) -> Tests)}]}'</dt> +<dt>`{foreachx, SetupX, CleanupX, Pairs}'</dt> +<dt>`{foreachx, Where, SetupX, Pairs}'</dt> +<dt>`{foreachx, SetupX, Pairs}'</dt> +<dd>`foreachx' is like `foreach', but uses a list of pairs, each +containing an extra argument `X' and an extended instantiator function. +</dd> +</dl> + +A `Setup' function is executed just before any of the specified tests +are run, and a `Cleanup' function is executed when no more of the +specified tests will be run, regardless of the reason. A `Setup' +function takes no argument, and returns some value which will be passed +as it is to the `Cleanup' function. A `Cleanup' function should do +whatever necessary and return some arbitrary value, such as the atom +`ok'. (`SetupX' and `CleanupX' functions are similar, but receive one +additional argument: some value `X', which depends on the context.) When +no `Cleanup' function is specified, a dummy function is used which has +no effect. + +An `Instantiator' function receives the same value as the `Cleanup' +function, i.e., the value returned by the `Setup' function. It should +then behave much like a generator (see {@section Primitives}), and +return a test set whose tests have been <em>instantiated</em> with the +given value. A special case is the syntax `{with, [AbstractTestFun]}' +which represents an instantiator function that distributes the value +over a list of unary functions; see {@section Primitives}: `{with, X, +[...]}' for more details. + +A `Where' term controls how the specified tests are executed. The +default is `spawn', which means that the current process handles the +setup and teardown, while the tests are executed in a subprocess. +`{spawn, Node}' is like `spawn', but runs the subprocess on the +specified node. `local' means that the current process will handle both +setup/teardown and running the tests - the drawback is that if a test +times out so that the process is killed, the <em>cleanup will not be +performed</em>; hence, avoid this for persistent fixtures such as file +operations. In general, 'local' should only be used when: +<ul> + <li>the setup/teardown needs to be executed by the process that will + run the tests;</li> + <li>no further teardown needs to be done if the process is killed + (i.e., no state outside the process was affected by the setup)</li> +</ul> + +=== Lazy generators === + +Sometimes, it can be convenient not to produce the whole set of test +descriptions before the testing begins; for example, if you want to +generate a huge amount of tests that would take up too much space to +keep in memory all at once. + +It is fairly easy to write a generator which, each time it is called, +either produces an empty list if it is done, or otherwise produces a +list containing a single test case plus a new generator which will +produce the rest of the tests. This demonstrates the basic pattern: + +```lazy_test_() -> + lazy_gen(10000). + + lazy_gen(N) -> + {generator, + fun () -> + if N > 0 -> + [?_test(...) + | lazy_gen(N-1)]; + true -> + [] + end + end}.''' + +When EUnit traverses the test representation in order to run the tests, +the new generator will not be called to produce the next test until the +previous test has been executed. + +Note that it is easiest to write this kind of recursive generator using +a help function, like the `lazy_gen/1' function above. It can also be +written using a recursive fun, if you prefer to not clutter your +function namespace and are comfortable with writing that kind of code. diff --git a/lib/eunit/doc/packages-frame.html b/lib/eunit/doc/packages-frame.html new file mode 100644 index 0000000000..52b45534f5 --- /dev/null +++ b/lib/eunit/doc/packages-frame.html @@ -0,0 +1,11 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>The eunit application</title> +<link rel="stylesheet" type="text/css" href="stylesheet.css" title="EDoc"> +</head> +<body bgcolor="white"> +<h2 class="indextitle">Packages</h2> +<table width="100%" border="0" summary="list of packages"></table> +</body> +</html>
\ No newline at end of file diff --git a/lib/eunit/doc/pdf/.gitignore b/lib/eunit/doc/pdf/.gitignore new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/lib/eunit/doc/pdf/.gitignore diff --git a/lib/eunit/doc/src/Makefile b/lib/eunit/doc/src/Makefile new file mode 100644 index 0000000000..faf2f9a847 --- /dev/null +++ b/lib/eunit/doc/src/Makefile @@ -0,0 +1,174 @@ +#<copyright> +# <year>2004-2007</year> +# <holder>Ericsson AB, All Rights Reserved</holder> +#</copyright> +#<legalnotice> +# The contents of this file are subject to the Erlang Public License, +# Version 1.1, (the "License"); you may not use this file except in +# compliance with the License. You should have received a copy of the +# Erlang Public License along with this software. If not, it can be +# retrieved online at http://www.erlang.org/. +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +# the License for the specific language governing rights and limitations +# under the License. +# +# The Initial Developer of the Original Code is Ericsson AB. +#</legalnotice> +# +include $(ERL_TOP)/make/target.mk +include $(ERL_TOP)/make/$(TARGET)/otp.mk + +# ---------------------------------------------------- +# Application version +# ---------------------------------------------------- +include ../../vsn.mk +VSN=$(EUNIT_VSN) +APPLICATION=eunit + +# ---------------------------------------------------- +# Release directory specification +# ---------------------------------------------------- +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) + +# ---------------------------------------------------- +# Help application directory specification +# ---------------------------------------------------- + +EDOC_DIR = $(ERL_TOP)/lib/edoc +SYNTAX_TOOLS_DIR = $(ERL_TOP)/lib/syntax_tools + +# ---------------------------------------------------- +# Target Specs +# ---------------------------------------------------- +EUNIT_DIR = $(ERL_TOP)/lib/eunit/src +EUNIT_INC_DIR = $(ERL_TOP)/lib/eunit/include + +EUNIT_MODULES = \ + eunit eunit_surefire + +XML_APPLICATION_FILES = ref_man.xml + +XML_REF3_FILES = $(EUNIT_MODULES:=.xml) + +XML_PART_FILES = \ + part.xml \ + part_notes.xml + +XML_CHAPTER_FILES = \ + chapter.xml + +XML_NOTES_FILES = \ + notes.xml + +HTML_EXAMPLE_FILES = + +HTML_STYLESHEET_FILES = \ + ../stylesheet.css + +BOOK_FILES = book.xml + +XML_FILES = \ + $(BOOK_FILES) $(XML_CHAPTER_FILES) $(XML_NOTES_FILES) \ + $(XML_PART_FILES) $(XML_REF3_FILES) $(XML_APPLICATION_FILES) + + +# ---------------------------------------------------- +INFO_FILE = ../../info + +HTML_FILES = $(XML_APPLICATION_FILES:%.xml=$(HTMLDIR)/%.html) \ + $(XML_PART_FILES:%.xml=$(HTMLDIR)/%.html) + + +EXTRA_FILES = \ + $(DEFAULT_HTML_FILES) \ + $(DEFAULT_GIF_FILES) \ + $(XML_REF3_FILES:%.xml=$(HTMLDIR)/%.html) \ + $(XML_REF6_FILES:%.xml=$(HTMLDIR)/%.html) \ + $(XML_CHAPTER_FILES:%.xml=$(HTMLDIR)/%.html)\ + $(XML_NOTES_FILES:%.xml=$(HTMLDIR)/%.html) + +MAN3_FILES = $(XML_REF3_FILES:%.xml=$(MAN3DIR)/%.3) +MAN6_FILES = $(XML_REF6_FILES:%_app.xml=$(MAN6DIR)/%.6) + +HTML_REF_MAN_FILE = $(HTMLDIR)/index.html + +TOP_PDF_FILE = $(PDFDIR)/$(APPLICATION)-$(VSN).pdf + + +# ---------------------------------------------------- +# FLAGS +# ---------------------------------------------------- +XML_FLAGS += +DVIPS_FLAGS += + +# ---------------------------------------------------- +# Targets +# ---------------------------------------------------- +$(HTMLDIR)/%.gif: %.gif + $(INSTALL_DATA) $< $@ + +docs: pdf html man + +$(TOP_PDF_FILE): $(XML_FILES) + +pdf: $(TOP_PDF_FILE) + +html: gifs $(HTML_REF_MAN_FILE) + + +man: $(MAN3_FILES) + +gifs: $(GIF_FILES:%=$(HTMLDIR)/%) + +$(XML_REF3_FILES): + docb_gen -def vsn $(EUNIT_VSN) -includes $(EUNIT_INC_DIR) $(EUNIT_DIR)/$(@:%.xml=%.erl) + +$(XML_CHAPTER_FILES): + docb_gen -chapter -def vsn $(EUNIT_VSN) ../overview.edoc + + +info: + @echo "XML_PART_FILES: $(XML_PART_FILES)" + @echo "XML_APPLICATION_FILES: $(XML_APPLICATION_FILES)" + @echo "EUNIT_XML_FILES: $(EUNIT_XML_FILES)" + @echo "EUNIT_MODULES: $(EUNIT_MODULES)" + @echo "HTML_FILES: $(HTML_FILES)" + @echo "HTMLDIR: $(HTMLDIR)" + @echo "DEFAULT_GIF_FILES: $(DEFAULT_GIF_FILES)" + @echo "DEFAULT_HTML_FILES: $(DEFAULT_HTML_FILES)" + @echo "EXTRA_FILES: $(EXTRA_FILES)" + +xml: $(XML_REF3_FILES) $(XML_CHAPTER_FILES) + +debug opt: + +clean clean_docs: + rm -rf $(HTMLDIR)/* + rm -f $(MAN3DIR)/* + rm -f $(XML_CHAPTER_FILES) *.html + rm -f $(TOP_PDF_FILE) $(TOP_PDF_FILE:%.pdf=%.fo) + rm -f errs core *~ + + +# ---------------------------------------------------- +# Release Target +# ---------------------------------------------------- +include $(ERL_TOP)/make/otp_release_targets.mk + +release_docs_spec: docs + $(INSTALL_DIR) $(RELSYSDIR)/doc/pdf + $(INSTALL_DATA) $(TOP_PDF_FILE) $(RELSYSDIR)/doc/pdf + $(INSTALL_DIR) $(RELSYSDIR)/doc/html + $(INSTALL_DATA) $(HTMLDIR)/* \ + $(RELSYSDIR)/doc/html + $(INSTALL_DATA) $(INFO_FILE) $(RELSYSDIR) + $(INSTALL_DIR) $(RELEASE_PATH)/man/man3 + $(INSTALL_DATA) $(MAN3DIR)/* $(RELEASE_PATH)/man/man3 + + +release_spec: + + + diff --git a/lib/eunit/doc/src/book.xml b/lib/eunit/doc/src/book.xml new file mode 100644 index 0000000000..4444b1dd7a --- /dev/null +++ b/lib/eunit/doc/src/book.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE book SYSTEM "book.dtd"> + +<book xmlns:xi="http://www.w3.org/2001/XInclude"> + <header titlestyle="normal"> + <copyright> + <year>2008</year> + <year>2008</year> + <holder>Ericsson AB, All Rights Reserved</holder> + </copyright> + <legalnotice> + The contents of this file are subject to the Erlang Public License, + Version 1.1, (the "License"); you may not use this file except in + compliance with the License. You should have received a copy of the + Erlang Public License along with this software. If not, it can be + retrieved online at http://www.erlang.org/. + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + the License for the specific language governing rights and limitations + under the License. + + The Initial Developer of the Original Code is Ericsson AB. + </legalnotice> + + <title>EUnit</title> + <prepared>Richard Carlsson, Micka�l R�mond</prepared> + <docno></docno> + <date>2008-10-29</date> + <rev>2.0</rev> + <file>book.xml</file> + </header> + <insidecover> + </insidecover> + <pagetext>EUnit Application</pagetext> + <preamble> + <contents level="2"></contents> + </preamble> + <parts lift="no"> + <xi:include href="part.xml"/> + </parts> + <applications> + <xi:include href="ref_man.xml"/> + </applications> + <releasenotes> + <xi:include href="notes.xml"/> + </releasenotes> + <listofterms></listofterms> + <index></index> +</book> + diff --git a/lib/eunit/doc/src/fascicules.xml b/lib/eunit/doc/src/fascicules.xml new file mode 100644 index 0000000000..bbe1e6c5cc --- /dev/null +++ b/lib/eunit/doc/src/fascicules.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE fascicules SYSTEM "fascicules.dtd"> + +<fascicules> + <fascicule file="part" href="part_frame.html" entry="no"> + User's Guide + </fascicule> + <fascicule file="ref_man" href="ref_man_frame.html" entry="yes"> + Reference Manual + </fascicule> + <fascicule file="part_notes" href="part_notes_frame.html" entry="no"> + Release Notes + </fascicule> + <fascicule file="" href="../../../../doc/print.html" entry="no"> + Off-Print + </fascicule> +</fascicules> + diff --git a/lib/eunit/doc/src/make.dep b/lib/eunit/doc/src/make.dep new file mode 100644 index 0000000000..d68f888403 --- /dev/null +++ b/lib/eunit/doc/src/make.dep @@ -0,0 +1,19 @@ +# ---------------------------------------------------- +# >>>> Do not edit this file <<<< +# This file was automaticly generated by +# /home/otp/bin/docdepend +# ---------------------------------------------------- + + +# ---------------------------------------------------- +# TeX files that the DVI file depend on +# ---------------------------------------------------- + +book.dvi: book.tex chapter.tex eunit.tex part.tex ref_man.tex + +# ---------------------------------------------------- +# Source inlined when transforming from source to LaTeX +# ---------------------------------------------------- + +book.tex: ref_man.xml + diff --git a/lib/eunit/doc/src/notes.xml b/lib/eunit/doc/src/notes.xml new file mode 100644 index 0000000000..ac86448fab --- /dev/null +++ b/lib/eunit/doc/src/notes.xml @@ -0,0 +1,153 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE chapter SYSTEM "chapter.dtd"> + +<chapter> + <header> + <copyright> + <year>2008</year> + <year>2008</year> + <holder>Ericsson AB, All Rights Reserved</holder> + </copyright> + <legalnotice> + The contents of this file are subject to the Erlang Public License, + Version 1.1, (the "License"); you may not use this file except in + compliance with the License. You should have received a copy of the + Erlang Public License along with this software. If not, it can be + retrieved online at http://www.erlang.org/. + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + the License for the specific language governing rights and limitations + under the License. + + The Initial Developer of the Original Code is Ericsson AB. + </legalnotice> + + <title>EUnit Release Notes</title> + <prepared>otp_appnotes</prepared> + <docno>nil</docno> + <date>nil</date> + <rev>nil</rev> + <file>notes.xml</file> + </header> + <p>This document describes the changes made to the EUnit application.</p> + +<section><title>Eunit 2.1.4</title> + + <section><title>Improvements and New Features</title> + <list> + <item> + <p> + The documentation is now built with open source tools + (xsltproc and fop) that exists on most platforms. One + visible change is that the frames are removed.</p> + <p> + Own Id: OTP-8201</p> + </item> + </list> + </section> + +</section> + +<section><title>Eunit 2.1.3</title> + + <section><title>Improvements and New Features</title> + <list> + <item> + <p> + Miscellaneous updates.</p> + <p> + Own Id: OTP-8190</p> + </item> + </list> + </section> + +</section> + +<section><title>Eunit 2.1.2</title> + + <section><title>Improvements and New Features</title> + <list> + <item> + <p> + Miscellanous updates.</p> + <p> + Own Id: OTP-8038</p> + </item> + </list> + </section> + +</section> + +<section><title>Eunit 2.1.1</title> + + <section><title>Fixed Bugs and Malfunctions</title> + <list> + <item> + <p> + eunit was broken in R13B.</p> + <p> + Own Id: OTP-8018</p> + </item> + </list> + </section> + +</section> + +<section><title>Eunit 2.1</title> + + <section><title>Improvements and New Features</title> + <list> + <item> + <p>Mostly internal changes, in particular to the event + protocol; fixes problems with timeouts that could cause + eunit to hang, and makes it much easier to write new + reporting back-ends.</p> + <p>New "surefire" report backend for Maven and + Bamboo.</p> + <p>The test representation is no longer traversed twice + (the first pass was for enumeration only). This + eliminates some strange restrictions on how generators + can be written, but it also means that reports cannot be + quite as complete as before in the event of skipped + tests.</p> + <p> + Own Id: OTP-7964</p> + </item> + </list> + </section> + +</section> + <section> + <title>EUnit 2.0.1</title> + + <section> + <title>Improvements and New Features</title> + <list type="bulleted"> + <item> + <p> + Corrected the documentation build. + </p> + </item> + </list> + </section> + </section> + + + <section> + <title>EUnit 2.0</title> + + <section> + <title>Improvements and New Features</title> + <list type="bulleted"> + <item> + <p> + This is the first version of EUnit (for unit testing of Erlang modules) by Richard Carlsson + released in OTP. + </p> + </item> + </list> + </section> + </section> +</chapter> + diff --git a/lib/eunit/doc/src/part.xml b/lib/eunit/doc/src/part.xml new file mode 100644 index 0000000000..e31a8d1b78 --- /dev/null +++ b/lib/eunit/doc/src/part.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE part SYSTEM "part.dtd"> + +<part xmlns:xi="http://www.w3.org/2001/XInclude"> + <header> + <copyright> + <year>2008</year> + <year>2008</year> + <holder>Ericsson AB, All Rights Reserved</holder> + </copyright> + <legalnotice> + The contents of this file are subject to the Erlang Public License, + Version 1.1, (the "License"); you may not use this file except in + compliance with the License. You should have received a copy of the + Erlang Public License along with this software. If not, it can be + retrieved online at http://www.erlang.org/. + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + the License for the specific language governing rights and limitations + under the License. + + The Initial Developer of the Original Code is Ericsson AB. + </legalnotice> + + <title>EUnit User's Guide</title> + <prepared></prepared> + <docno></docno> + <date></date> + <rev></rev> + </header> + <description> + <p>The <em>EUnit</em> application + contains modules with support for unit testing.</p> + </description> + <xi:include href="chapter.xml"/> +</part> + diff --git a/lib/eunit/doc/src/part_notes.xml b/lib/eunit/doc/src/part_notes.xml new file mode 100644 index 0000000000..28644f961b --- /dev/null +++ b/lib/eunit/doc/src/part_notes.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE part SYSTEM "part.dtd"> + +<part xmlns:xi="http://www.w3.org/2001/XInclude"> + <header> + <copyright> + <year>2008</year> + <year>2008</year> + <holder>Ericsson AB, All Rights Reserved</holder> + </copyright> + <legalnotice> + The contents of this file are subject to the Erlang Public License, + Version 1.1, (the "License"); you may not use this file except in + compliance with the License. You should have received a copy of the + Erlang Public License along with this software. If not, it can be + retrieved online at http://www.erlang.org/. + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + the License for the specific language governing rights and limitations + under the License. + + The Initial Developer of the Original Code is Ericsson AB. + </legalnotice> + + <title>EUnit Release Notes</title> + <prepared></prepared> + <docno></docno> + <date>2008-10-29</date> + <rev></rev> + + </header> + <description> + <p>The <em>EUnit</em> application + contains modules with support for unit testing</p> + </description> + <xi:include href="notes.xml"/> +</part> + diff --git a/lib/eunit/doc/src/ref_man.xml b/lib/eunit/doc/src/ref_man.xml new file mode 100644 index 0000000000..02feef5e97 --- /dev/null +++ b/lib/eunit/doc/src/ref_man.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="latin1" ?> +<!DOCTYPE application SYSTEM "application.dtd"> + +<application xmlns:xi="http://www.w3.org/2001/XInclude"> + <header> + <copyright> + <year>2008</year> + <year>2008</year> + <holder>Ericsson AB, All Rights Reserved</holder> + </copyright> + <legalnotice> + The contents of this file are subject to the Erlang Public License, + Version 1.1, (the "License"); you may not use this file except in + compliance with the License. You should have received a copy of the + Erlang Public License along with this software. If not, it can be + retrieved online at http://www.erlang.org/. + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + the License for the specific language governing rights and limitations + under the License. + + The Initial Developer of the Original Code is Ericsson AB. + </legalnotice> + + <title>EUnit Reference Manual</title> + <prepared></prepared> + <docno></docno> + <date>2008-10-29</date> + <rev>2.0</rev> + <file>ref_man.xml</file> + </header> + <description> + <p>The <em>EUnit</em> application + contains modules with support for unit testing.</p> + </description> + <xi:include href="eunit.xml"/> + <xi:include href="eunit_surefire.xml"/> + </application> + diff --git a/lib/eunit/doc/stylesheet.css b/lib/eunit/doc/stylesheet.css new file mode 100644 index 0000000000..e426a90483 --- /dev/null +++ b/lib/eunit/doc/stylesheet.css @@ -0,0 +1,55 @@ +/* standard EDoc style sheet */ +body { + font-family: Verdana, Arial, Helvetica, sans-serif; + margin-left: .25in; + margin-right: .2in; + margin-top: 0.2in; + margin-bottom: 0.2in; + color: #000000; + background-color: #ffffff; +} +h1,h2 { + margin-left: -0.2in; +} +div.navbar { + background-color: #add8e6; + padding: 0.2em; +} +h2.indextitle { + padding: 0.4em; + background-color: #add8e6; +} +h3.function,h3.typedecl { + background-color: #add8e6; + padding-left: 1em; +} +div.spec { + margin-left: 2em; + background-color: #eeeeee; +} +a.module,a.package { + text-decoration:none +} +a.module:hover,a.package:hover { + background-color: #eeeeee; +} +ul.definitions { + list-style-type: none; +} +ul.index { + list-style-type: none; + background-color: #eeeeee; +} + +/* + * Minor style tweaks + */ +ul { + list-style-type: square; +} +table { + border-collapse: collapse; +} +td { + padding: 3 +} |