    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Achieving FitNesse in C++</title>
        <link>https://members.accu.org/index.php/articles/305</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #60 - Apr 2004</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c152/">60</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-152/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+152/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Achieving FitNesse in C++</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 April 2004 22:54:59 +01:00 or Fri, 02 April 2004 22:54:59 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Sometimes a very simple idea can make a very big difference.
FitNesse (and &quot;Fit&quot; on which it builds) are very simple ideas - and
when I first encountered them my reaction was &quot;so what&quot;. It was
only after talking to people using them that I found time to
investigate them more seriously. What then, do they offer?</p>
<p>What they offer is feedback on a system under development
meeting functional requirements. They encode tests in a format that
can both be executed and explained (or even edited) by a customer
and can also be executed directly against the system to demonstrate
the results. As a perennial problem with software development is a
failure of communication, anything that helps narrow the gap
between requirements and deliverables is worthy of
investigation.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>Fit and
FitCpp</h2>
</div>
<p>Fit is a Java component that takes some HTML input, interprets
it into tests, exercises the tests against the system being tested,
and outputs an updated version of the HTML that incorporates the
results. The developer needs to write some lightweight classes that
define the mapping between fields in the requirements document and
the parameters of the methods to be invoked in the system. These
classes are called &quot;Fixtures&quot; and, in the Java implementation must
conform to a &quot;fixture&quot; convention that allows Fit to use the
reflections API to set values and call methods. (Conventions like
this are common in the Java world: Junit, EJBs, JavaBeans and
NakedObjects are all technologies that adopt this approach.)</p>
<p>FitCpp is a translation of the Fit functionality into C++.
Clearly, C++ lacks a direct analogue of &quot;reflections&quot;, but by using
a combination of macros and templates it allows the developer to
achieve the same effect.</p>
<p>What both of these tools do is to scan the input HTML for
tables; these are used to specify the fixture to use, together with
the input data and results expected. The input is shown in Table
1.</p>
<div class="table"><a name="d0e33" id="d0e33"></a>
<p class="title c2">Table 1. Input HTML</p>
<table summary="Input HTML" border="1" cellspacing="0" width="600">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td colspan="3">The system must be able to calculate the number of
vowels and/or consonants in a sentence.</td>
</tr>
<tr>
<td colspan="3">sentence_analyser</td>
</tr>
<tr>
<td>Sentence</td>
<td>vowels()</td>
<td>consonants()</td>
</tr>
<tr>
<td>Hello world!</td>
<td>3</td>
<td>7</td>
</tr>
<tr>
<td>This sentence has four vowels and thirty three consonants.</td>
<td>4</td>
<td>33</td>
</tr>
<tr>
<td>&quot;0123456789&quot; Does it handle numbers 0123456789?</td>
<td>7</td>
<td>12</td>
</tr>
<tr>
<td>*(@$&pound;)! &amp;^%$&pound;?@# &lt;&gt;/</td>
<td>0</td>
<td>0</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>The reporting is very simple: the results of tests that succeed
turn green (light grey in illustration) and the results (both
expected and actual are shown) of tests that fail turn red (dark
grey). The output is shown in Table 2.</p>
<div class="table"><a name="d0e86" id="d0e86"></a>
<p class="title c2">Table 2. Output Report</p>
<table summary="Output Report" border="1" cellspacing="0" width=
"600">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td colspan="3">sentence_analyser</td>
</tr>
<tr>
<td>Sentence</td>
<td>vowels()</td>
<td>consonants()</td>
</tr>
<tr>
<td>Hello world!</td>
<td bgcolor="#CCCCCC">3</td>
<td bgcolor="#CCCCCC">7</td>
</tr>
<tr>
<td rowspan="2">This sentence has four vowels and thirty three
consonants.</td>
<td bgcolor="#999999">4 expected</td>
<td bgcolor="#CCCCCC" rowspan="2">33</td>
</tr>
<tr>
<td bgcolor="#999999">16 actual</td>
</tr>
<tr>
<td>&quot;0123456789&quot; Does it handle numbers 0123456789?</td>
<td bgcolor="#CCCCCC">7</td>
<td bgcolor="#CCCCCC">12</td>
</tr>
<tr>
<td>*(@$&pound;)! &amp;^%$&pound;?@# &lt;&gt;/</td>
<td bgcolor="#CCCCCC">0</td>
<td bgcolor="#CCCCCC">0</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>I have to admit that my initial reaction to this was &quot;so what?&quot;
- but what I had missed is that tables of test data and results are
very easy to incorporate into requirements documents and are
understood by users. Of course, editing the raw HTML that lies
behind the above display would put off the typical user: that is
where FitNesse comes in...</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e157" id="d0e157"></a>FitNesse</h2>
</div>
<p>FitNesse is a Java based Wiki server that includes the facility
to passing the HTML on a page to Fit (or to FitCpp) and display the
results. It also allows the test pages to be organised into test
suites and provides summary reporting on the test results for each
page in the suite. (As with the individual tests these are colour
coded.)</p>
<p>The idea is that the functional requirements can be captured as
a combination of text and tables on the Wiki site. Obviously, when
these are first added all the tests will fail, but as the system is
developed more and more tests will pass - and as the tests change
colour they automatically provide visible feedback on progress for
both developers and customers.</p>
<p>All of this is easier to explain and confirm with the customer
than a test written in Java, C++, or a test scripting language.
(And the text that underlies the Wiki is easier to follow than
HTML.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e166" id="d0e166"></a>Doing it
yourself</h2>
</div>
<p>If you've read this far then you'll want to know what you need
to do to set this all up. There are basically four things to
do:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>get FitNesse and install it;</p>
</li>
<li>
<p>get fitcpp and install it;</p>
</li>
<li>
<p>capture the requirements as a set of Wiki pages; and,</p>
</li>
<li>
<p>write corresponding fixtures for your system.</p>
</li>
</ol>
</div>
<p>Getting FitNesse is pretty trivial (just go to the website given
in the references below and follow the links). There are even
helpful .bat and .sh scripts for starting it - basically it runs
straight out of the archive.</p>
<p>Getting fitcpp isn't quite as simple - the published code I
downloaded had a few bugs and is reliant on a number of Microsoft
C++ &quot;features&quot; that allow non-standard code to compile. I've
updated it with some fixes and to get it to compile with gcc - I've
made this updated version available on my website (and also passed
the changes back to the author).</p>
<p>Capturing the requirements as a Wiki page is pretty easy. The
above example looks like:</p>
<pre class="programlisting">
!define COMMAND_PATTERN {
../c++/fitcpp/overload/FitOverload.exe }

The system must be able to calculate the
number of vowels and/or consonants in a
sentence.

|!-sentence_analyser-!|
|sentence|vowels()|consonants()|
|Hello world!|3|7|
|This sentence has four vowels and thirty three consonants.|4|33|
|&quot;0123456789&quot; Does it handle numbers 0123456789?|7|12|
|*(@$&pound;)! &amp;^%$&pound;?@# &lt;&gt;/|0|0|
</pre>
<p>The first line overrides the default Java &quot;fit&quot; handler for the
page and directs FitNesse to invoke an alternative hander - we'll
describe creating and using this file below. The paragraph that
follows is transcribed without change, and the table is marked out
using the vertical bars.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e194" id="d0e194"></a>Writing a C++
fixture</h2>
</div>
<p>The &quot;.exe&quot; file mentioned in the preceding section needs to come
from somewhere: obviously it contains any fixtures used for the
tests and some code from the FitCpp library. (Clearly, in any real
world usage it will also link against the application code.)</p>
<p>Writing a fixture takes a little more explaining, because one
needs to understand the role of a fixture and the conventions
involved. While there are many types of fixture possible (they
simply provide an interface for processing tables) the most useful
in writing tests is the &quot;column&quot; fixture used in the illustration
above (I didn't choose the name). This fixture is defined as
follows:</p>
<pre class="programlisting">
#include &quot;ColumnFixture.h&quot;
#include &quot;MainFixtureMaker.h&quot;

class sentence_analyser
         : public ColumnFixture {
public:
  sentence_analyser();
  string sentence;
  int vowels();
  int consonants();
};
</pre>
<p>The name of the fixture corresponds to the first line of the
table - you would typically have multiple fixtures and multiple
requirements tables for an application. Looking at the body of the
fixture you will see that it exposes public member
data<sup>[<a name="d0e205" href="#ftn.d0e205" id=
"d0e205">1</a>]</sup> and parameter-less member functions that
return values. These correspond to the columns of the table.
Actually, in C++ (unlike Java) you don't have to make the names
correspond to the columns but it is easier to go with this
convention.</p>
<p>In Java the reflections API is used to detect these
corresponding data and methods, but in C++ we have to write a few
more lines:</p>
<pre class="programlisting">
sentence_analyser::sentence_analyser() {
  PUBLISH(sentence_analyser,
          string, sentence);
  PUBLISH(sentence_analyser,
          int, vowels);
  PUBLISH(sentence_analyser,
          int, consonants);
}
</pre>
<p>These macros register the corresponding members with the fitcpp
processing engine.</p>
<p>In a real test harness the <tt class="methodname">vowel()</tt>
and <tt class="methodname">consonant()</tt> member functions would
invoke functionality in the application being tested. However, for
the purposes of compiling and running the above demonstration the
vowel() method is implemented as follows:</p>
<pre class="programlisting">
int sentence_analyser::vowels() {
  return std::count_if(
    sentence.begin(),
    sentence.end(),
    is_vowel());
}
</pre>
<p>Where <tt class="function">is_vowel</tt> is a functor with the
obvious functionality.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e230" id="d0e230"></a>Joining the
dots</h2>
</div>
<p>In its original distribution FitCpp required some additional
&quot;boiler plate&quot; code that provides a &quot;Maker&quot; class to build the
Fixtures that are identified by the tables and a driver &quot;main&quot;
method that pushes the HTML through the FitCpp Parse engine. I have
factored all this out into a MainFixtureMaker class and a generic
main method and placed these into the FitCpp core library. The
result of this is that only a single line is needed to set up the
creation of a Fixture:</p>
<pre class="programlisting">
REGISTER_FIXTURE_MAKER(sentence_analyser)
</pre>
<p>This is based upon the assumption of statically linking the
Fixtures with the library and that statically linked objects of
global scope are initialised before entering <tt class=
"function">main()</tt>. (This behaviour isn't strictly required by
the ISO standard but, as far as I can tell, it is common across all
C++ implementations.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e242" id="d0e242"></a>Running the
example</h2>
</div>
<p>If we compile and link this code we will have an executable that
can be invoked from the FitNesse test page described above. What
happens when we invoke &quot;test&quot; on this page? And how do we combine
tests together?</p>
<p>Pressing &quot;test&quot; causes FitNesse to invoke the program specified
in the COMMAND_PATTERN directive. (This should be the program
containing the fixtures). The program then reads the HTML supplied
by Fitnesse from standard input and writes to standard output.</p>
<p>The FitCpp framework scans the input looking for HTML tables.
When it encounters one it examines the first cell and constructs a
corresponding fixture (the contents of the first cell specifies the
type of the fixture). The next row of the table is used to match up
the column names with the members of the corresponding Fixture
&quot;published&quot; during construction. For column fixtures (like the
example we've been following) the remaining rows are processed one
cell at a time setting data or calling functions as appropriate.
(The example shows how this allows the sentence parameter required
by <tt class="function">count_vowels()</tt> to be supplied prior to
invoking it). The results of any function calls are compared with
the values embedded in the HTML to select the appropriate output
(green if it matches, red if not).</p>
<p>There can be (and usually are) several tables within a
functional requirements page (which is a single invocation of the
program) the plugins corresponding to these tables will be invoked
sequentially as part of the same process. Neither FitNesse nor
FitCpp provides a mechanism for passing information between these
tests but, as program state is maintained, the various fixtures can
communicate using global data/objects.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e256" id="d0e256"></a>Scaling
up</h2>
</div>
<p>There are several issues of scale that need to be addressed: the
principal ones are managing a collection of functional
requirements, and allowing developers to apply the tests to their
local version of the system (instead of to the integration build on
a server somewhere). FitNesse addresses both of these concerns
quite neatly by having pages with special properties.</p>
<p>The web pages with FitNesse are organised into a hierarchy like
a file system (with the same convention seen in Java package names
- using a &quot;.&quot; character as a path separator). For example, a page
might be called &quot;SystemRequirements&quot; and beneath this would come
other pages such as &quot;SystemRequirements.ChapterOne&quot; etc. By setting
the &quot;suite&quot; or &quot;virtual&quot; property on a page then the processing of
the sub-pages can be tailored.</p>
<p>If the top-level page is given the &quot;suite&quot; property then it
acquires a &quot;suite&quot; option that can be used to run all the tests in
pages that lie beneath it. This results in a summary of the results
of these tests with links to the results of individual tests so
that the user can &quot;drill down&quot; if desired. Setting up a &quot;suite&quot;
page that covers all the functional requirements allows progress to
be demonstrated in an immediate and visible manner (as more
functionality is delivered more of the tests show &quot;green&quot;).</p>
<p>The &quot;virtual&quot; attribute is used in a local instance of the
FitNesse Wiki server running on a developer's machine. This causes
FitNesse to fetch the content of all the subpages from another URL
(the remote Wiki server - in the configuration I have set up this
is the integration &quot;build&quot; machine). The local Wiki server will run
tests (and suites) locally against the developer's version of the
system but the content of the functional tests is maintained on the
central server. To avoid confusion the pages retrieved from the
remote server are given a blue background before being shown by the
local server.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e267" id="d0e267"></a>Miscellaneous
Notes</h2>
</div>
<p>As with any test framework it is normally appropriate to
consider beginning the test with a &quot;StartUp&quot; fixture and ending
with a &quot;TearDown&quot; fixture. (There is more on patterns of test
design in the FitNesse documentation.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e272" id="d0e272"></a>Row
Fixtures</h2>
</div>
<p>In the discussion above I've only discussed Column Fixtures,
there is a second type of Fixture supported by FitNesse/FitCpp -
RowFixtures. These are used for describing collections of rows that
may be returned by the application (think &quot;result set&quot;) and allow
missing and/or extra rows to be reported. The way they are
implemented is directly analogous to the ColumnFixtures discussed
above, and their use is adequately covered in the FitNesse
documentation.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e277" id="d0e277"></a>References</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed">FitNesse: <span class="bibliomisc"><a href=
"http://sourceforge.net/projects/fitnesse/" target=
"_top">http://sourceforge.net/projects/fitnesse/</a></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="bibliomisc">FitCpp: <a href=
"http://fitnesse.org/" target="_top">http://fitnesse.org/</a>
(Updated version referred to in this article: <a href=
"http://www.octopull.demon.co.uk/download/fitcpp.tar.gz" target=
"_top">http://www.octopull.demon.co.uk/download/fitcpp.tar.gz</a>)</span></p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e205" href="#d0e205" id=
"ftn.d0e205">1</a>]</sup> Note: the use of public data in this
context isn't an argument for adopting this approach in general.
Fixtures have a very specialised design context and are only used
within that context.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
