1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <meta name="generator" content="Docutils 0.10: http://docutils.sourceforge.net/" />
7 <title>mutest - A simple micro unit testing framework for C</title>
8 <meta name="author" content="Leandro Lucarella" />
9 <meta name="date" content="2013-02-17" />
10 <meta name="copyright" content="Leandro Lucarella (2008), released under the BOLA license" />
11 <style type="text/css">
14 :Author: David Goodger (goodger@python.org)
15 :Id: $Id: html4css1.css 7514 2012-09-14 14:27:12Z milde $
16 :Copyright: This stylesheet has been placed in the public domain.
18 Default cascading style sheet for the HTML output of Docutils.
20 See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
21 customize this style sheet.
24 /* used to remove borders from tables and images */
25 .borderless, table.borderless td, table.borderless th {
28 table.borderless td, table.borderless th {
29 /* Override padding for "table.docutils td" with "! important".
30 The right padding separates the table cells. */
31 padding: 0 0.5em 0 0 ! important }
34 /* Override more specific margin styles with "! important". */
35 margin-top: 0 ! important }
37 .last, .with-subtitle {
38 margin-bottom: 0 ! important }
44 text-decoration: none ;
51 margin-bottom: 0.5em }
53 object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
57 /* Uncomment (and remove this text!) to get bold-faced definition list terms
65 div.abstract p.topic-title {
69 div.admonition, div.attention, div.caution, div.danger, div.error,
70 div.hint, div.important, div.note, div.tip, div.warning {
72 border: medium outset ;
75 div.admonition p.admonition-title, div.hint p.admonition-title,
76 div.important p.admonition-title, div.note p.admonition-title,
77 div.tip p.admonition-title {
79 font-family: sans-serif }
81 div.attention p.admonition-title, div.caution p.admonition-title,
82 div.danger p.admonition-title, div.error p.admonition-title,
83 div.warning p.admonition-title, .code .error {
86 font-family: sans-serif }
88 /* Uncomment (and remove this text!) to get reduced vertical space in
90 div.compound .compound-first, div.compound .compound-middle {
91 margin-bottom: 0.5em }
93 div.compound .compound-last, div.compound .compound-middle {
102 div.dedication p.topic-title {
110 div.footer, div.header {
119 div.line-block div.line-block {
125 margin: 0 0 0.5em 1em ;
126 border: medium outset ;
128 background-color: #ffffee ;
133 div.sidebar p.rubric {
134 font-family: sans-serif ;
137 div.system-messages {
140 div.system-messages h1 {
144 border: medium outset ;
147 div.system-message p.system-message-title {
154 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
155 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
167 img.align-left, .figure.align-left, object.align-left {
172 img.align-right, .figure.align-right, object.align-right {
177 img.align-center, .figure.align-center, object.align-center {
193 /* reset inner alignment in figures */
195 text-align: inherit }
197 /* div.align-center * { */
198 /* text-align: left } */
200 ol.simple, ul.simple {
204 list-style: decimal }
207 list-style: lower-alpha }
210 list-style: upper-alpha }
213 list-style: lower-roman }
216 list-style: upper-roman }
230 white-space: nowrap }
239 font-family: sans-serif ;
244 font-family: sans-serif ;
255 pre.literal-block, pre.doctest-block, pre.math, pre.code {
259 pre.code .ln { color: grey; } /* line numbers */
260 pre.code, code { background-color: #eeeeee }
261 pre.code .comment, code .comment { color: #5C6576 }
262 pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
263 pre.code .literal.string, code .literal.string { color: #0C5404 }
264 pre.code .name.builtin, code .name.builtin { color: #352B84 }
265 pre.code .deleted, code .deleted { background-color: #DEB0A1}
266 pre.code .inserted, code .inserted { background-color: #A3D289}
269 font-family: sans-serif ;
270 font-style: oblique }
272 span.classifier-delimiter {
273 font-family: sans-serif ;
277 font-family: sans-serif }
280 white-space: nowrap }
288 span.section-subtitle {
289 /* font-size relative to parent (h1..h6 element) */
293 border-left: solid 1px gray;
301 margin-bottom: 0.5em }
304 border-left: solid 1px black;
307 table.docutils td, table.docutils th,
308 table.docinfo td, table.docinfo th {
309 padding-left: 0.5em ;
310 padding-right: 0.5em ;
311 vertical-align: top }
313 table.docutils th.field-name, table.docinfo th.docinfo-name {
316 white-space: nowrap ;
319 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
320 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
324 list-style-type: none }
329 <div class="document" id="mutest-a-simple-micro-unit-testing-framework-for-c">
330 <h1 class="title"><em>mutest</em> - A simple micro unit testing framework for C</h1>
331 <table class="docinfo" frame="void" rules="none">
332 <col class="docinfo-name" />
333 <col class="docinfo-content" />
335 <tr><th class="docinfo-name">Author:</th>
336 <td>Leandro Lucarella</td></tr>
337 <tr><th class="docinfo-name">Contact:</th>
338 <td><a class="first last reference external" href="mailto:llucax@gmail.com">llucax@gmail.com</a></td></tr>
339 <tr><th class="docinfo-name">Version:</th>
341 <tr><th class="docinfo-name">Date:</th>
342 <td>2013-02-17</td></tr>
343 <tr><th class="docinfo-name">Copyright:</th>
344 <td>Leandro Lucarella (2008), released under the <a class="reference external" href="http://blitiri.com.ar/p/bola/">BOLA</a> license</td></tr>
347 <div class="abstract topic">
348 <p class="topic-title first">Abstract</p>
349 <p><em>mutest</em> is a micro <a class="reference external" href="http://en.wikipedia.org/wiki/Unit_testing">unit testing</a> framework for C (with some
350 <a class="reference internal" href="#c-support">C++ support</a>). It's mostly an idea (it even comes with
351 2 <a class="reference internal" href="#implementations">implementations</a> of the idea!) with the goal of being easy to use
352 (just write your <a class="reference internal" href="#test-case">test cases</a> grouped in <a class="reference internal" href="#test-suite">test suites</a> and you're
353 set) and so small and simple that you don't mind to copy the files to
354 your project and just use it (i.e., no dependencies).</p>
355 <p>The idea is simple: a source file is a <a class="reference internal" href="#test-suite">test suite</a>, a function is
356 a <a class="reference internal" href="#test-case">test case</a> (special functions can be used for <a class="reference internal" href="#test-suite">test suite</a>
357 <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a>), which can can have several <a class="reference internal" href="#checks">checks</a>.
358 <a class="reference internal" href="#checks">Checks</a> comes in 2 flavors, one that only prints an error, and one that
359 terminates the current <a class="reference internal" href="#test-case">test case</a> too. A (normally) automated <a class="reference internal" href="#test-program">test
360 program</a> run all the <a class="reference internal" href="#test-suite">test suites</a> and print some stats. It fails
361 (returns non-zero) if any <a class="reference internal" href="#test-suite">test suite</a> fails.</p>
362 <p><div style="width: 220px; height: 270px; float: right; margin-left: 1em; margin-top: 1em">
363 <iframe width="220" height="270" style="border: none; outline: none" src="http://tools.flattr.net/widgets/thing.html?thing=1141711"></iframe>
366 <div class="contents topic" id="contents">
367 <p class="topic-title first">Contents</p>
368 <ul class="auto-toc simple">
369 <li><a class="reference internal" href="#installation" id="id9">1. Installation</a></li>
370 <li><a class="reference internal" href="#quick-sample" id="id10">2. Quick Sample</a><ul class="auto-toc">
371 <li><a class="reference internal" href="#factorial-c" id="id11">2.1. factorial.c</a></li>
372 <li><a class="reference internal" href="#factorial-test-c" id="id12">2.2. factorial_test.c</a></li>
373 <li><a class="reference internal" href="#exception-test-cpp" id="id13">2.3. exception_test.cpp</a></li>
376 <li><a class="reference internal" href="#concepts" id="id14">3. Concepts</a><ul class="auto-toc">
377 <li><a class="reference internal" href="#test-program" id="id15">3.1. Test Program</a></li>
378 <li><a class="reference internal" href="#test-suite" id="id16">3.2. Test Suite</a></li>
379 <li><a class="reference internal" href="#test-case" id="id17">3.3. Test Case</a></li>
380 <li><a class="reference internal" href="#checks" id="id18">3.4. Checks</a></li>
381 <li><a class="reference internal" href="#initialization" id="id19">3.5. Initialization</a></li>
382 <li><a class="reference internal" href="#termination" id="id20">3.6. Termination</a></li>
385 <li><a class="reference internal" href="#c-support" id="id21">4. C++ Support</a></li>
386 <li><a class="reference internal" href="#implementations" id="id22">5. Implementations</a><ul class="auto-toc">
387 <li><a class="reference internal" href="#static-implementations" id="id23">5.1. Static Implementations</a><ul class="auto-toc">
388 <li><a class="reference internal" href="#c-implementation" id="id24">5.1.1. C implementation</a><ul class="auto-toc">
389 <li><a class="reference internal" href="#mkmutest-invocation" id="id25">5.1.1.1. <tt class="docutils literal">mkmutest</tt> Invocation</a></li>
390 <li><a class="reference internal" href="#test-program-invocation" id="id26">5.1.1.2. Test Program Invocation</a></li>
391 <li><a class="reference internal" href="#dependencies" id="id27">5.1.1.3. Dependencies</a></li>
396 <li><a class="reference internal" href="#dynamic-implementations" id="id28">5.2. Dynamic Implementations</a><ul class="auto-toc">
397 <li><a class="reference internal" href="#python-implementation" id="id29">5.2.1. Python implementation</a><ul class="auto-toc">
398 <li><a class="reference internal" href="#mutest-invocation" id="id30">5.2.1.1. <tt class="docutils literal">mutest</tt> Invocation</a></li>
399 <li><a class="reference internal" href="#id8" id="id31">5.2.1.2. Dependencies</a></li>
406 <li><a class="reference internal" href="#reference" id="id32">6. Reference</a><ul class="auto-toc">
407 <li><a class="reference internal" href="#mu-check" id="id33">6.1. <tt class="docutils literal">mu_check()</tt></a></li>
408 <li><a class="reference internal" href="#mu-ensure" id="id34">6.2. <tt class="docutils literal">mu_ensure()</tt></a></li>
409 <li><a class="reference internal" href="#mu-echeck" id="id35">6.3. <tt class="docutils literal">mu_echeck()</tt></a></li>
410 <li><a class="reference internal" href="#mu-eensure" id="id36">6.4. <tt class="docutils literal">mu_eensure()</tt></a></li>
413 <li><a class="reference internal" href="#about" id="id37">7. About</a></li>
416 <div class="section" id="installation">
417 <h1><a class="toc-backref" href="#id9">1. Installation</a></h1>
418 <p>Download the <a class="reference external" href="http://proj.llucax.com.ar/home/mutest/releases/mutest.tar.gz">latest distribution tarball</a> and uncompress it.</p>
419 <p>You can also download any release from the <a class="reference external" href="http://proj.llucax.com.ar/home/mutest/releases/">releases directory</a> or get it
420 using <a class="reference external" href="http://git.or.cz/">Git</a> directly from the <a class="reference external" href="http://git.llucax.com.ar/w/software/mutest.git">Git repository</a>.</p>
421 <p>You can get <a class="reference external" href="http://proj.llucax.com.ar/home/mutest/manual.html">this manual</a> too, or a <a class="reference external" href="http://proj.llucax.com.ar/home/mutest/manual.pdf">PDF version</a> of it.</p>
422 <p>To actually install <em>mutest</em> run:</p>
423 <pre class="literal-block">
426 <p>Default installation path is <tt class="docutils literal">/usr/local</tt> (because of that, you'll probably
427 need superuser privileges to install to the default location). You can override
428 that by passing the <tt class="docutils literal">prefix</tt> make variable, for example:</p>
429 <pre class="literal-block">
430 $ make prefix=/opt/mutest install
432 <p>If you want to install just the docs, you can do:</p>
433 <pre class="literal-block">
436 <p>Or even <tt class="docutils literal"><span class="pre">install-readme</span></tt>, <tt class="docutils literal"><span class="pre">install-html</span></tt> or <tt class="docutils literal"><span class="pre">install-pdf</span></tt> if you are too
438 <p>If you want to install just one particular <a class="reference internal" href="#implementations">implementation</a>, to can use the
439 <tt class="docutils literal"><span class="pre">install-c</span></tt> and <tt class="docutils literal"><span class="pre">install-py</span></tt> targets.</p>
441 <div class="section" id="quick-sample">
442 <h1><a class="toc-backref" href="#id10">2. Quick Sample</a></h1>
443 <p>You can find some samples in the <a class="reference external" href="http://git.llucax.com.ar/w/software/mutest.git?a=tree;f=sample;h=d8ad4dd9c3428fef5963107c82ab6a5e34ec6e00;hb=HEAD">sample</a> directory.</p>
444 <p>This is an example taken from there. A simple <em>module</em> called <a class="reference internal" href="#factorial-c">factorial.c</a>
445 with its corresponding <a class="reference internal" href="#test-suite">test suite</a> (<a class="reference internal" href="#factorial-test-c">factorial_test.c</a>).</p>
446 <p>You can see some <a class="reference internal" href="#c-support">C++ support</a> in the <a class="reference internal" href="#exception-test-cpp">exception_test.cpp</a> <a class="reference internal" href="#test-suite">test suite</a>.</p>
447 <div class="section" id="factorial-c">
448 <h2><a class="toc-backref" href="#id11">2.1. factorial.c</a></h2>
449 <pre class="code c literal-block">
450 <span class="comment multiline">/*
451 * This file is part of mutest, a simple micro unit testing framework for C.
453 * mutest was written by Leandro Lucarella <llucax@gmail.com> and is released
454 * under the BOLA license, please see the LICENSE file or visit:
455 * http://blitiri.com.ar/p/bola/
457 * This is an example module that calculates a factorial.
459 * Please, read the README file for more details.
462 <span class="keyword type">unsigned</span> <span class="name function">factorial</span><span class="punctuation">(</span><span class="keyword type">unsigned</span> <span class="name">x</span><span class="punctuation">)</span> <span class="punctuation">{</span>
463 <span class="keyword">if</span> <span class="punctuation">(</span><span class="name">x</span> <span class="operator"><=</span> <span class="literal number integer">1</span><span class="punctuation">)</span>
464 <span class="keyword">return</span> <span class="literal number integer">1</span><span class="punctuation">;</span>
465 <span class="keyword">return</span> <span class="name">x</span> <span class="operator">*</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="name">x</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">);</span>
466 <span class="punctuation">}</span>
469 <div class="section" id="factorial-test-c">
470 <h2><a class="toc-backref" href="#id12">2.2. factorial_test.c</a></h2>
471 <pre class="code c literal-block">
472 <span class="comment multiline">/*
473 * This file is part of mutest, a simple micro unit testing framework for C.
475 * mutest was written by Leandro Lucarella <llucax@gmail.com> and is released
476 * under the BOLA license, please see the LICENSE file or visit:
477 * http://blitiri.com.ar/p/bola/
479 * This is the factorial module test suite. Each (public) function starting
480 * with mu_test will be picked up by mkmutest as a test case.
482 * Please, read the README file for more details.
485 <span class="comment preproc">#include "factorial.h"
487 <span class="comment preproc">#include "../mutest.h"
489 <span class="keyword type">void</span> <span class="name function">mu_test_factorial_zero</span><span class="punctuation">()</span> <span class="punctuation">{</span>
490 <span class="keyword type">unsigned</span> <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">);</span>
491 <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">1</span><span class="punctuation">);</span>
492 <span class="punctuation">}</span>
494 <span class="keyword type">void</span> <span class="name function">mu_test_factorial_one</span><span class="punctuation">()</span> <span class="punctuation">{</span>
495 <span class="keyword type">unsigned</span> <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">);</span>
496 <span class="comment multiline">/* this test is wrong on purpose, to see how it fails */</span>
497 <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">2</span><span class="punctuation">);</span>
498 <span class="punctuation">}</span>
500 <span class="keyword type">void</span> <span class="name function">mu_test_factorial_positive</span><span class="punctuation">()</span> <span class="punctuation">{</span>
501 <span class="keyword type">unsigned</span> <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="literal number integer">2</span><span class="punctuation">);</span>
502 <span class="comment multiline">/* this test is wrong on purpose, to see how it fails */</span>
503 <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">3</span><span class="punctuation">);</span>
505 <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="literal number integer">3</span><span class="punctuation">);</span>
506 <span class="comment multiline">/* we don't want to continue if this fails, because the next result
507 * depends on this one. This one will succeed. */</span>
508 <span class="name">mu_ensure</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">6</span><span class="punctuation">);</span>
510 <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="name">x</span><span class="punctuation">);</span>
511 <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">720</span><span class="punctuation">);</span>
513 <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="literal number integer">4</span><span class="punctuation">);</span>
514 <span class="name">mu_ensure</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">6</span><span class="punctuation">);</span> <span class="comment multiline">/* same as before, but this one will fail. */</span>
516 <span class="name">x</span> <span class="operator">=</span> <span class="name">factorial</span><span class="punctuation">(</span><span class="name">x</span><span class="operator">-</span><span class="literal number integer">15</span><span class="punctuation">);</span> <span class="comment multiline">/* and this will never be executed */</span>
517 <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">x</span> <span class="operator">==</span> <span class="literal number integer">362881</span><span class="punctuation">);</span> <span class="comment multiline">/* but if excecuted, will fail */</span>
518 <span class="punctuation">}</span>
521 <div class="section" id="exception-test-cpp">
522 <h2><a class="toc-backref" href="#id13">2.3. exception_test.cpp</a></h2>
523 <pre class="code c++ literal-block">
524 <span class="comment multiline">/*
525 * This file is part of mutest, a simple micro unit testing framework for C.
527 * mutest was written by Leandro Lucarella <llucax@gmail.com> and is released
528 * under the BOLA license, please see the LICENSE file or visit:
529 * http://blitiri.com.ar/p/bola/
531 * This is a C++ module test suite. It shows how to use checks involving
534 * Please, read the README file for more details.
537 <span class="comment preproc">#include <stdexcept> </span><span class="comment single">// std::out_of_range
538 </span><span class="comment preproc">#include <vector> </span><span class="comment single">// std::vector
540 <span class="comment preproc">#include "../mutest.h"
542 <span class="keyword">extern</span> <span class="literal string">"C"</span> <span class="punctuation">{</span>
544 <span class="keyword type">void</span> <span class="name">mu_test_exceptions</span><span class="punctuation">()</span> <span class="punctuation">{</span>
545 <span class="name">std</span><span class="operator">::</span><span class="name">vector</span><span class="operator"><</span><span class="keyword type">int</span><span class="operator">></span> <span class="name">v</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">);</span>
546 <span class="comment single">// ok
547 </span> <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">v</span><span class="punctuation">.</span><span class="name">at</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">)</span> <span class="operator">==</span> <span class="literal number integer">0</span><span class="punctuation">);</span>
548 <span class="comment single">// throws! This fails
549 </span> <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">v</span><span class="punctuation">.</span><span class="name">at</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span> <span class="operator">==</span> <span class="literal number integer">0</span><span class="punctuation">);</span>
550 <span class="comment single">// ok, we expect the exception to be thrown, and it does
551 </span> <span class="name">mu_echeck</span><span class="punctuation">(</span><span class="name">std</span><span class="operator">::</span><span class="name">out_of_range</span><span class="punctuation">,</span> <span class="name">v</span><span class="punctuation">.</span><span class="name">at</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">));</span>
552 <span class="comment single">// fails! We expect this to throw, but it doesn't
553 </span> <span class="name">mu_echeck</span><span class="punctuation">(</span><span class="name">std</span><span class="operator">::</span><span class="name">out_of_range</span><span class="punctuation">,</span> <span class="name">v</span><span class="punctuation">.</span><span class="name">at</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">));</span>
554 <span class="comment single">// fails again, but this time the show is over (note the "ensure")
555 </span> <span class="name">mu_eensure</span><span class="punctuation">(</span><span class="name">std</span><span class="operator">::</span><span class="name">out_of_range</span><span class="punctuation">,</span> <span class="name">v</span><span class="punctuation">.</span><span class="name">at</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">));</span>
556 <span class="comment single">// this will never be executed (it should fail if it is)
557 </span> <span class="name">mu_check</span><span class="punctuation">(</span><span class="name">v</span><span class="punctuation">.</span><span class="name">empty</span><span class="punctuation">());</span>
558 <span class="punctuation">}</span>
560 <span class="punctuation">}</span> <span class="comment single">// extern "C"</span>
564 <div class="section" id="concepts">
565 <h1><a class="toc-backref" href="#id14">3. Concepts</a></h1>
566 <p><em>mutest</em> is about 4 simple concepts: <a class="reference internal" href="#test-program">test program</a>, <a class="reference internal" href="#test-suite">test suite</a>, <a class="reference internal" href="#test-case">test
567 case</a> and <a class="reference internal" href="#checks">checks</a>. Well, to be honest you probably will need <a class="reference internal" href="#test-suite">test suite</a>
568 <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a> too =)</p>
569 <div class="section" id="test-program">
570 <h2><a class="toc-backref" href="#id15">3.1. Test Program</a></h2>
571 <p>A <strong>test program</strong> is the higher level unit of <em>mutest</em>. The test program is
572 the one in charge of running all your tests. Probably one of the more important
573 features of <em>mutest</em> is that you are not supposed to bother about the test
574 program. So, different <a class="reference internal" href="#implementations">implementations</a> have different ways to tackle this.
575 Some need more or less interactions from your part, and each have their pros
577 <p>But this is all you need to know for now, for more details see how the test
578 program is implemented by your <a class="reference internal" href="#implementations">implementation</a> of choice.</p>
580 <div class="section" id="test-suite">
581 <h2><a class="toc-backref" href="#id16">3.2. Test Suite</a></h2>
582 <p>A <strong>test suite</strong> is the higher level unit of <em>mutest</em> that you should
583 care about =). Is not much more than a way to group <a class="reference internal" href="#test-case">test cases</a>. Code-wise,
584 a test suite is a C (or C++) module (or compilation unit). Not clear enough?
585 A unit test is an object file (could be a shared object depending on the
586 <a class="reference internal" href="#implementations">implementation</a>). This module should have one or more <a class="reference internal" href="#test-case">test cases</a> and it
587 could have any number (including zero) of <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a>
589 <p>A test suite, is inspected by the <a class="reference internal" href="#test-program">test program</a> for <a class="reference internal" href="#test-case">test cases</a> and
590 <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a> functions, and run them.</p>
591 <p>A test suite fail if one or more <a class="reference internal" href="#test-case">test cases</a> fail, and it's skipped if one or
592 more <a class="reference internal" href="#initialization">initialization</a> functions fail (or, depending on the implementation, if
593 the test suite can't be loaded at all).</p>
595 <div class="section" id="test-case">
596 <h2><a class="toc-backref" href="#id17">3.3. Test Case</a></h2>
597 <p>A <strong>test case</strong> is just a plain function with a special signature and name.
598 A test case function name must start with <tt class="docutils literal">mu_test</tt>, and take no arguments
599 and return nothing. For example:</p>
600 <pre class="literal-block">
601 void mu_test_something(void);
603 <p>A test case (probably) only make sense if it has <a class="reference internal" href="#checks">checks</a>. A test case succeed
604 only if all its checks succeed too.</p>
605 <p>Test are executed in an <a class="reference internal" href="#implementations">implementation</a>-dependant order, but usually the
606 default order is alphabetical.</p>
608 <div class="section" id="checks">
609 <h2><a class="toc-backref" href="#id18">3.4. Checks</a></h2>
610 <p>Checks are assertions that a <a class="reference internal" href="#test-case">test case</a> must pass (a boolean expression that
611 must evaluate to <em>true</em>). There are 2 big flavors of checks: <strong>check</strong> and
612 <strong>ensure</strong>. <strong>check</strong> just print an error (and <em>mark</em> the <a class="reference internal" href="#test-case">test case</a> as
613 failed) and <strong>ensure</strong> halt the <a class="reference internal" href="#test-case">test case</a> execution, jumping to the next
615 <p>For better <a class="reference internal" href="#c-support">C++ support</a> there are check macros that assert that a specified
616 exception is thrown (instead of check for a boolean expression to evaluate to
618 <p>You can take a look at the <a class="reference internal" href="#reference">reference</a> to see the different flavors of check
619 macros in more detail.</p>
621 <div class="section" id="initialization">
622 <h2><a class="toc-backref" href="#id19">3.5. Initialization</a></h2>
623 <p>Sometimes you need to setup some environment shared between all the <a class="reference internal" href="#test-case">test
624 cases</a> in a <a class="reference internal" href="#test-suite">test suite</a>. You can use <strong>initialization functions</strong> for this.</p>
625 <p>An initialization function, like a <a class="reference internal" href="#test-case">test case</a>, is a plain C function with
626 a special name and signature. The name must start with <tt class="docutils literal">mu_init</tt> and it must
627 take no arguments, and return an <em>error code</em> (<tt class="docutils literal">0</tt> being success). For
629 <pre class="literal-block">
630 int mu_init_something(void);
632 <p>All initialization functions are executed before any <a class="reference internal" href="#test-case">test case</a>, in an
633 <a class="reference internal" href="#implementations">implementation</a>-dependant order, and if one of them fail (returns non-zero),
634 the whole <a class="reference internal" href="#test-suite">test suite</a> is skipped immediately.</p>
636 <div class="section" id="termination">
637 <h2><a class="toc-backref" href="#id20">3.6. Termination</a></h2>
638 <p><strong>Termination functions</strong> are just like <a class="reference internal" href="#initialization">initialization</a> functions, but they're
639 executed <strong>after</strong> the <a class="reference internal" href="#test-case">test cases</a>, their names start with <tt class="docutils literal">mu_term</tt> and
640 they return nothing. For example:</p>
641 <pre class="literal-block">
642 void mu_term_something(void);
646 <div class="section" id="c-support">
647 <h1><a class="toc-backref" href="#id21">4. C++ Support</a></h1>
648 <p>You can use <em>mutest</em> with C++, the only care you must take is that, because of
649 C++ <a class="reference external" href="http://en.wikipedia.org/wiki/Name_mangling">name mangling</a> (and <em>mutest</em> relaying on function names), you must
650 declare your <a class="reference internal" href="#test-case">test cases</a> and <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a> functions as
651 <tt class="docutils literal">extern "C"</tt> (see <a class="reference internal" href="#exception-test-cpp">exception_test.cpp</a> for an example).</p>
652 <p><a class="reference internal" href="#checks">Checks</a> become <em>exception-safe</em> when using <em>mutest</em> with a C++ compiler, and
653 2 extra <a class="reference internal" href="#checks">checks</a> designed for C++ get defined (<a class="reference internal" href="#mu-echeck">mu_echeck()</a> and
654 <a class="reference internal" href="#mu-eensure">mu_eensure()</a>). They assert that an expression throws a particular exception.</p>
656 <div class="section" id="implementations">
657 <h1><a class="toc-backref" href="#id22">5. Implementations</a></h1>
658 <p>There are 2 big groups of possible implementations that I can think
659 of: <em>static</em> and <em>dynamic</em>.</p>
660 <p><em>mutest</em> comes with one implementation of each group.</p>
661 <div class="section" id="static-implementations">
662 <h2><a class="toc-backref" href="#id23">5.1. Static Implementations</a></h2>
663 <p>Static implementations can be only written in C/C++ (or other language that is
664 link-compatible with C, like the <a class="reference external" href="http://www.digitalmars.com/d/">D Programming Language</a>, but since one of
665 the main goals of <em>mutest</em> is avoid unnecessary dependencies, you probably
666 don't want to depend on an extra language/compiler to run your tests =).</p>
667 <p>The main advantage is better debugging support, because you can run the <a class="reference internal" href="#test-program">test
668 program</a> in a standard debugger and see what happens with <a class="reference internal" href="#test-case">test cases</a> very
670 <p>The main disadvantage is, the <a class="reference internal" href="#test-suite">test suites</a> must be figured out in
671 <em>compile-time</em>, usually using some kind of code generation (if you want to
672 avoid writing repetitive code yourself). There's also a limitation in the <a class="reference internal" href="#test-case">test
673 case</a>, <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a> functions names: they should be unique
674 for all the <a class="reference internal" href="#test-program">test program</a>.</p>
675 <div class="section" id="c-implementation">
676 <h3><a class="toc-backref" href="#id24">5.1.1. C implementation</a></h3>
677 <p><em>mutest</em> comes with a C static implementation. Only 3 files are needed:
678 <tt class="docutils literal">mutest.c</tt> (the <em>user-independent</em> part of the <a class="reference internal" href="#test-program">test program</a>), <tt class="docutils literal">mkmutest</tt>
679 (a bash script for generating the <em>user-dependent</em> part of the <a class="reference internal" href="#test-program">test program</a>)
680 and <tt class="docutils literal">mutest.h</tt> (the header file that <a class="reference internal" href="#test-suite">test suites</a> should include).</p>
681 <p>You can copy this 3 files to your project or install them at system-level and
682 use them globally.</p>
683 <p>The procedure is simple, You should compile you <a class="reference internal" href="#test-suite">test suites</a>, <tt class="docutils literal">mutest.c</tt>
684 and the generated output of <tt class="docutils literal">mkmutest</tt> as object files and link them
687 <pre class="literal-block">
688 $ cc -c -o mutest.o mutest.c
689 $ cc -c -o test1.o test1.c
690 $ cc -c -o test2.o test2.c
691 $ mkmutest mutest.h test1.o test2.o | cc -xc -c -o runmutest.o -
692 $ cc -o testprg mutest.o test1.o test2.o runmutest.o
694 <p>Then you can run the <a class="reference internal" href="#test-program">test program</a> invoking it with no arguments:</p>
695 <pre class="literal-block">
698 <div class="section" id="mkmutest-invocation">
699 <h4><a class="toc-backref" href="#id25">5.1.1.1. <tt class="docutils literal">mkmutest</tt> Invocation</a></h4>
700 <p>This small script take 1 mandatory positional argument: the path to the
701 <tt class="docutils literal">mutest.h</tt> file. All remaining positional arguments should be object files
702 representing <a class="reference internal" href="#test-suite">test suites</a>.</p>
704 <div class="section" id="test-program-invocation">
705 <h4><a class="toc-backref" href="#id26">5.1.1.2. Test Program Invocation</a></h4>
706 <p>The test program can be invoked without arguments, but can take some extra
708 <dl class="docutils">
709 <dt><tt class="docutils literal"><span class="pre">-v</span></tt></dt>
710 <dd><p class="first">Be verbose. This is accumulative, when you add extra <tt class="docutils literal"><span class="pre">-v</span></tt> you will
711 get extra verbosity.</p>
712 <p class="last">By default, you just get failed <a class="reference internal" href="#checks">checks</a> printed. If you use a single
713 <tt class="docutils literal"><span class="pre">-v</span></tt>, a summary of failed/passed <a class="reference internal" href="#test-suite">test suites</a>, <a class="reference internal" href="#test-case">test cases</a> and
714 <a class="reference internal" href="#checks">checks</a> will be printed. If an extra <tt class="docutils literal"><span class="pre">-v</span></tt> is used, you'll see the current
715 <a class="reference internal" href="#test-suite">test suite</a> being executed. Another <tt class="docutils literal"><span class="pre">-v</span></tt> and you'll get the current
716 <a class="reference internal" href="#test-case">test case</a>, and another one, and you'll get each <a class="reference internal" href="#checks">check</a>.</p>
720 <div class="section" id="dependencies">
721 <h4><a class="toc-backref" href="#id27">5.1.1.3. Dependencies</a></h4>
722 <p>Even when dependencies are kept minimal, there always be a few ;)</p>
723 <p>To use this implementation you just need:</p>
725 <li>A C compiler (you already needed that, so...)</li>
726 <li>The <tt class="docutils literal">nm</tt> program (from <a class="reference external" href="http://www.gnu.org/software/binutils/">GNU Binutils</a>, included in virtually any *NIX)</li>
727 <li>The <a class="reference external" href="http://www.gnu.org/software/bash/">GNU Bash</a> shell interpreter (also included in virtually any *NIX)</li>
732 <div class="section" id="dynamic-implementations">
733 <h2><a class="toc-backref" href="#id28">5.2. Dynamic Implementations</a></h2>
734 <p>Dynamic implementations, on the other hand, can be written in any language that
735 can access to shared objects. The idea is to inspect a shared object for <a class="reference internal" href="#test-suite">test
736 suites</a> and run them, without requiring any information about <a class="reference internal" href="#test-suite">test suites</a>
738 <p>There are several advantages in this kind of implementations. The dynamic
739 nature let you completely separate the <a class="reference internal" href="#test-program">test program</a> from the user-written
740 <a class="reference internal" href="#test-suite">test suites</a> and you can choose at <em>run-time</em> what <a class="reference internal" href="#test-suite">test suites</a> to execute
741 by just selecting the correct shared objects. Also, <a class="reference internal" href="#test-case">test case</a>,
742 <a class="reference internal" href="#initialization">initialization</a> and <a class="reference internal" href="#termination">termination</a> functions names only have to be unique in the
743 scope of the <a class="reference internal" href="#test-suite">test suites</a>, because <a class="reference internal" href="#test-suite">test suites</a> are completely isolated in
744 separate shared objects.</p>
745 <p>But everything comes at a price, and the higher price to pay is
746 <em>debuggability</em>. It's a little harder to plug a debugger to a shared object.</p>
747 <div class="section" id="python-implementation">
748 <h3><a class="toc-backref" href="#id29">5.2.1. Python implementation</a></h3>
749 <p>This implementation is much simpler and elegant than the <a class="reference internal" href="#c-implementation">C implementation</a>.
750 Only 2 files are needed: <tt class="docutils literal">mutest</tt> (<a class="reference internal" href="#test-program">test program</a> written in <a class="reference external" href="http://www.python.org/">Python</a> using
751 <a class="reference external" href="http://docs.python.org/library/ctypes.html">ctypes</a> module to access the shared object symbols) and <tt class="docutils literal">mutest.h</tt> (the
752 header file that <a class="reference internal" href="#test-suite">test suites</a> should include).</p>
753 <p>Since both implementations provided by <em>mutest</em> share the same <tt class="docutils literal">mutest.h</tt>,
754 you should define the <tt class="docutils literal">MUTEST_PY</tt> macro when compiling the <a class="reference internal" href="#test-suite">test suites</a> if
755 you will run them using this implementation.</p>
756 <p>As with the <a class="reference internal" href="#c-implementation">C implementation</a>, you can copy this 2 files to your project or
757 install them at system-level and use them globally.</p>
758 <p>The procedure is even simpler than the <a class="reference internal" href="#c-implementation">C implementation</a>: compile and link
759 you <a class="reference internal" href="#test-suite">test suites</a> as shared objects and then run the <tt class="docutils literal">mutest</tt> program
760 passing the shared objects as arguments. For example:</p>
761 <pre class="literal-block">
762 $ cc -c -fPIC -DMUTEST_PY -o test1.o test1.c
763 $ cc -shared -o test1.so test1.o
764 $ cc -c -fPIC -DMUTEST_PY -o test2.o test2.c
765 $ cc -shared -o test2.so test2.o
766 $ mutest test1.so test2.so
769 <div class="section" id="mutest-invocation">
770 <h4><a class="toc-backref" href="#id30">5.2.1.1. <tt class="docutils literal">mutest</tt> Invocation</a></h4>
771 <p><tt class="docutils literal">mutest</tt> program takes <a class="reference internal" href="#test-suite">test suites</a> shared objects to run as positional
772 arguments. It accepts the same options as the <a class="reference internal" href="#test-program-invocation">C implementation's test
773 program</a> and some extra options are accepted too:</p>
774 <dl class="docutils">
775 <dt><tt class="docutils literal"><span class="pre">--verbose</span></tt></dt>
776 <dd>Alias for <tt class="docutils literal"><span class="pre">-v</span></tt>.</dd>
777 <dt><tt class="docutils literal"><span class="pre">-q</span></tt>, <tt class="docutils literal"><span class="pre">--quiet</span></tt></dt>
778 <dd>Be quiet (no output is shown at all).</dd>
779 <dt><tt class="docutils literal"><span class="pre">-s</span></tt>, <tt class="docutils literal"><span class="pre">--search</span></tt></dt>
780 <dd>Search for <a class="reference internal" href="#test-suite">test suites</a> (*.so) in the current directory and add them
781 to the list of <a class="reference internal" href="#test-suite">test suites</a> to run.</dd>
782 <dt><tt class="docutils literal"><span class="pre">-h</span></tt>, <tt class="docutils literal"><span class="pre">--help</span></tt></dt>
783 <dd>Show a help message and exit.</dd>
786 <div class="section" id="id8">
787 <h4><a class="toc-backref" href="#id31">5.2.1.2. Dependencies</a></h4>
788 <p>As with the <a class="reference internal" href="#c-implementation">C implementation</a>, some minor dependencies are needed:</p>
790 <li><a class="reference external" href="http://www.python.org/">Python</a> (2.5 or later)</li>
791 <li>The <tt class="docutils literal">nm</tt> program (from <a class="reference external" href="http://www.gnu.org/software/binutils/">GNU Binutils</a>, included in virtually any *NIX)</li>
793 <p>You will need a C compiler for building the <a class="reference internal" href="#test-suite">test suites</a> too, but technically
794 is not needed by <em>mutest</em> itself ;)</p>
799 <div class="section" id="reference">
800 <h1><a class="toc-backref" href="#id32">6. Reference</a></h1>
801 <div class="section" id="mu-check">
802 <h2><a class="toc-backref" href="#id33">6.1. <tt class="docutils literal">mu_check()</tt></a></h2>
803 <dl class="docutils">
805 <dd><tt class="docutils literal">mu_check(expression)</tt></dd>
807 <dd>Check that the <tt class="docutils literal">expression</tt> evaluates to <em>true</em>. Continue with the
808 <a class="reference internal" href="#test-case">test case</a> if fail.</dd>
809 <dt>Availability</dt>
812 <dd><pre class="first last literal-block">
815 mu_check(5 == 4); /* fail */
816 mu_check(5 == 5); /* excecuted, pass */
822 <div class="section" id="mu-ensure">
823 <h2><a class="toc-backref" href="#id34">6.2. <tt class="docutils literal">mu_ensure()</tt></a></h2>
824 <dl class="docutils">
826 <dd><tt class="docutils literal">mu_ensure(expression)</tt></dd>
828 <dd>Check that the <tt class="docutils literal">expression</tt> evaluates to <em>true</em>. Interrupt the <a class="reference internal" href="#test-case">test
829 case</a> if fail.</dd>
830 <dt>Availability</dt>
833 <dd><pre class="first last literal-block">
836 mu_ensure(5 == 4); /* fail */
837 mu_check(5 == 5); /* not excecuted */
843 <div class="section" id="mu-echeck">
844 <h2><a class="toc-backref" href="#id35">6.3. <tt class="docutils literal">mu_echeck()</tt></a></h2>
845 <dl class="docutils">
847 <dd><tt class="docutils literal">mu_echeck(class, expression)</tt></dd>
849 <dd>Check that the <tt class="docutils literal">expression</tt> throws a specific exception <tt class="docutils literal">class</tt> (or
850 subclass). Continue with the <a class="reference internal" href="#test-case">test case</a> if fail.</dd>
851 <dt>Availability</dt>
854 <dd><pre class="first last literal-block">
855 #include <stdexcept>
861 mu_echeck(std::exception, true); /* fail */
862 mu_echeck(std::exception,
863 throw std::runtime_error("!")); /* excecuted, pass */
870 <div class="section" id="mu-eensure">
871 <h2><a class="toc-backref" href="#id36">6.4. <tt class="docutils literal">mu_eensure()</tt></a></h2>
872 <dl class="docutils">
874 <dd><tt class="docutils literal">mu_eensure(class, expression)</tt></dd>
876 <dd>Check that the <tt class="docutils literal">expression</tt> throws a specific exception <tt class="docutils literal">class</tt> (or
877 subclass). Interrupt the <a class="reference internal" href="#test-case">test case</a> if fail.</dd>
878 <dt>Availability</dt>
881 <dd><pre class="first last literal-block">
882 #include <stdexcept>
888 mu_eensure(std::exception, true); /* fail */
889 mu_echeck(std::exception,
890 throw std::runtime_error("!")); /* not excecuted */
898 <div class="section" id="about">
899 <h1><a class="toc-backref" href="#id37">7. About</a></h1>
900 <p>This manual was written using <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a>.</p>
901 <!-- Use section numbers -->
902 <!-- Internal Links (aliases): -->
903 <!-- External Links: -->
904 <!-- Substitutions: -->
905 <!-- vim: set filetype=rst expandtab shiftwidth=4 softtabstop=4 : -->