/[escript]/trunk/tools/CppUnitTest/doc/cookbook.html
ViewVC logotype

Annotation of /trunk/tools/CppUnitTest/doc/cookbook.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1387 - (hide annotations)
Fri Jan 11 07:45:26 2008 UTC (11 years, 10 months ago) by trankine
Original Path: temp/tools/CppUnitTest/doc/cookbook.html
File MIME type: text/html
File size: 12916 byte(s)
Restore the trunk that existed before the windows changes were committed to the (now moved to branches) old trunk.
1 jgs 82 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2     <html>
3     <head>
4     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5     <meta name="GENERATOR" content="Mozilla/4.7 [en] (X11; I; SunOS 5.7 sun4u) [Netscape]">
6     <meta name="CREATED" content="20010209;11071700">
7     <meta name="CHANGEDBY" content="Rice Ed">
8     <meta name="CHANGED" content="20010209;11352500">
9     <meta name="Template" content="C:\Program Files\MSOffice\Office\html.dot">
10     <title>CppUnit Cookbook</title>
11     <style>
12     <!--
13     A:link { color: #0000ff }
14     A:visited { color: #800080 }
15     -->
16     </style>
17     </head>
18     <body link="#0000FF" vlink="#800080">
19    
20     <h1>
21     <b><font color="#3366FF"><font size=+2>UnitTest Cookbook</font></font></b></h1>
22     <font color="#330000">The UnitTest library is derived from CppUnitTest
23     library developed by Michael Feathers. This has been</font>
24     <br><font color="#330000">modified to better suit the requirements of PGS
25     and the PGS development environment. Please read the</font> <a href="license.html">license</a>&nbsp;
26     agreement.
27     <p><font color="#330000">It is easiest to describe the UnitTest library
28     by way of an example. An incomplete UnitTest was provided for the <i>Range
29     </i>class. The description for the <i>Range</i> class is shown below.</font>
30     <p>&nbsp; <a href="Range.html">Range Class</a>
31     <p>In order to provide a unit test for the <i>Range</i> class the
32     <i>RangeTestCase</i>
33     class was developed. This inherits from the
34     <i>TestCase</i> class which
35     is part of the UnitTest library.&nbsp; Within this class 4 methods are
36     provided for testing various components of the <i>Range</i> class. The
37     four methods are
38     <br>&nbsp;&nbsp;&nbsp; testEquality
39     <br>&nbsp;&nbsp;&nbsp; testWithin
40     <br>&nbsp;&nbsp;&nbsp; testRangeException
41     <br>&nbsp;&nbsp;&nbsp; testOverlap
42     <br>These methods are bundled together into a suite. The facility provided
43     by the TestUnit library is the <i>TestSuite</i> class.&nbsp; All of the
44     tests within this suite are run within a <i>TestRunner</i> object.
45     <p><font color="#3366FF">Code for RangeTest.h</font>
46     <p>#if !defined&nbsp; RANGETEST_H
47     <br>#define RANGETEST_H
48     <p>#include "AlienNamespace.h"
49     <p>#include "TestCase.h"
50     <br>#include "TestSuite.h"
51     <br>#include "TestCaller.h"
52     <br>#include "Range.h"
53     <p>BEGIN_NAMESPACE_ALIEN
54     <p>class RangeTestCase : public TestCase
55     <br>{
56     <br>&nbsp;private:
57     <br>&nbsp; //
58     <br>&nbsp; // Two integer ranges created by setUp method. They are destructed
59     in the
60     <br>&nbsp; // tearDown method. All of the tests use these ranges.
61     <br>&nbsp; Range&lt;int> *r1,*r2;
62     <br>&nbsp;protected:
63     <br>&nbsp; //
64     <br>&nbsp; // The following methods are for testing various methods of
65     the Range class
66     <br>&nbsp; //
67     <br>&nbsp; // Method to test whether operator==() method is working
68     <br>&nbsp; void testEquality();
69     <br>&nbsp; //
70     <br>&nbsp; // Method to test whether within() method is working
71     <br>&nbsp; void testWithin();
72     <br>&nbsp; //
73     <br>&nbsp; // Method to test whether a RangeException is contructed and
74     thrown correctly
75     <br>&nbsp; void testRangeException();
76     <br>&nbsp; //
77     <br>&nbsp; // Method to test whether the overlap method works
78     <br>&nbsp; void testOverlap();
79     <p>&nbsp;public:
80     <br>&nbsp; RangeTestCase (string name) : TestCase (name) {}
81     <br>&nbsp; ~RangeTestCase() {}
82     <br>&nbsp; //
83     <br>&nbsp; // Virtual methods of TestCase. These are provided to ensure
84     the objects
85     <br>&nbsp; // being used in the tests are in a known state.
86     <br>&nbsp; // setUp is called before each test method is called
87     <br>&nbsp; void setUp();
88     <br>&nbsp; //
89     <br>&nbsp; // tearDown is called after each test method is called.
90     <br>&nbsp; void tearDown();
91     <br>&nbsp; //
92     <br>&nbsp; // return the suite of tests to perform
93     <br>&nbsp; static Test* suite ();
94     <br>};
95     <p>END_NAMESPACE_ALIEN
96     <p>#endif
97     <br>&nbsp;
98     <p><font color="#3366FF">Code for RangeTest.C</font>
99     <p>#include "RangeTest.h"
100     <p>void RangeTestCase::setUp() {
101     <br>&nbsp; //
102     <br>&nbsp; // create fresh objects for this test
103     <br>&nbsp; r1=new Range&lt;int>(1,5);
104     <br>&nbsp; r2=new Range&lt;int>(1,7);
105     <br>}
106     <p>void RangeTestCase::tearDown() {
107     <br>&nbsp; //
108     <br>&nbsp; // destroy the objects from the last test
109     <br>&nbsp; delete r1;
110     <br>&nbsp; delete r2;
111     <br>}
112     <p>void RangeTestCase::testEquality() {
113     <br>&nbsp; //
114     <br>&nbsp; // check whether the operator==() method returns r1 as equal
115     to itself.
116     <br>&nbsp; // This should return true if the method is working correctly
117     <br>&nbsp; // If false is returned the test will stop and a failure is
118     reported
119     <br>&nbsp; assert((*r1)==(*r1));
120     <br>&nbsp; //
121     <br>&nbsp; // This should return true as r1 does not equal r2.
122     <br>&nbsp; assert(!(*r1)==(*r2));
123     <br>}
124     <p>void RangeTestCase::testWithin() {
125     <br>&nbsp; //
126     <br>&nbsp; // test whether the minimum and maximum&nbsp; value for the
127     range are within
128     <br>&nbsp; // the range. Both should return true.
129     <br>&nbsp; assert((*r1).within((*r1).getMin()));
130     <br>&nbsp; assert((*r1).within((*r1).getMax()));
131     <br>}
132     <p>void RangeTestCase::testRangeException() {
133     <br>&nbsp; try {
134     <br>&nbsp;&nbsp;&nbsp; //
135     <br>&nbsp;&nbsp;&nbsp; // This call should throw an exception as the range
136     is invalid
137     <br>&nbsp;&nbsp;&nbsp; (*r1).setRange(3,1);
138     <br>&nbsp;&nbsp;&nbsp; //
139     <br>&nbsp;&nbsp;&nbsp; // if the test gets here the exception has failed
140     <br>&nbsp;&nbsp;&nbsp; assert(false);
141     <br>&nbsp; }
142     <br>&nbsp; catch (RangeException &amp;e) {
143     <br>&nbsp; }
144     <br>}
145     <br>void RangeTestCase::testOverlap() {
146     <br>&nbsp; //
147     <br>&nbsp; // Test whether r1 overlaps r2. Should return true.
148     <br>&nbsp; assert((*r1).overlap((*r2)));
149     <br>}
150     <br>&nbsp;
151     <p>Test* RangeTestCase::suite ()
152     <br>{
153     <br>&nbsp; //
154     <br>&nbsp; // create the suite of tests to perform.
155     <br>&nbsp; TestSuite *testSuite = new TestSuite ("RangeTestCase");
156     <p>&nbsp; testSuite->addTest (new TestCaller &lt;RangeTestCase> ("testEquality",&amp;RangeTestCase::testEquality));
157     <br>&nbsp; testSuite->addTest (new TestCaller &lt;RangeTestCase> ("testWithin",&amp;RangeTestCase::testWithin));
158     <br>&nbsp; testSuite->addTest (new TestCaller &lt;RangeTestCase> ("testRangeException",&amp;RangeTestCase::testRangeException));
159     <br>&nbsp; testSuite->addTest (new TestCaller &lt;RangeTestCase> ("testOverlap",&amp;RangeTestCase::testOverlap));
160     <br>&nbsp; return testSuite;
161     <br>}
162     <br>&nbsp;
163     <p><font color="#3366FF">Code for rangeTest.C (main)</font>
164     <p><font color="#330000">#include "RangeTest.h"</font>
165     <br><font color="#330000">#include "TestRunner.h"</font>
166     <p><font color="#330000">main(int argc, char **argv) {</font>
167     <br><font color="#330000">&nbsp; //</font>
168     <br><font color="#330000">&nbsp; // object which runs all of the tests</font>
169     <br><font color="#330000">&nbsp; TestRunner runner;</font>
170     <br><font color="#330000">&nbsp; //</font>
171     <br><font color="#330000">&nbsp; // add the RangeTestCase suite of tests
172     to the runner</font>
173     <br><font color="#330000">&nbsp; runner.addTest ("RangeTestCase", RangeTestCase::suite
174     ());</font>
175     <br><font color="#330000">&nbsp; //</font>
176     <br><font color="#330000">&nbsp; // add the RangeTestCase suite of tests
177     to the runner to demonstrate</font>
178     <br><font color="#330000">&nbsp; // behaviour with multiple suites</font>
179     <br><font color="#330000">&nbsp; runner.addTest ("R2Test",RangeTestCase::suite
180     ());</font>
181     <br><font color="#330000">&nbsp; //</font>
182     <br><font color="#330000">&nbsp; // actually run the unit tests.</font>
183     <br><font color="#330000">&nbsp; runner.run (argc, argv);</font>
184     <p><font color="#330000">&nbsp; return 0;</font>
185     <br><font color="#330000">}</font>
186     <br>&nbsp;
187     <p><font color="#3366FF"><font size=+2>Example Output</font></font>
188     <p>run rangeTest
189     <p><font color="#3366FF">Output as it should be</font>
190     <p>Suite: RangeTestCase
191     <br>&nbsp; Test: testEquality Ok
192     <br>&nbsp; Test: testWithin Ok
193     <br>&nbsp; Test: testRangeException Ok
194     <br>&nbsp; Test: testOverlap Ok
195     <p>OK (4 tests)
196     <p>Suite: R2Test
197     <br>&nbsp; Test: testEquality Ok
198     <br>&nbsp; Test: testWithin Ok
199     <br>&nbsp; Test: testRangeException Ok
200     <br>&nbsp; Test: testOverlap Ok
201     <p>OK (4 tests)
202     <p><font color="#3366FF">Output with some of the tests deliberately made
203     to fail.</font>
204     <br><font color="#3366FF">Note: There is one failure and one error generated.</font>
205     <p>Suite: RangeTestCase
206     <br>&nbsp; Test: testEquality Failure
207     <br>&nbsp; Test: testWithin Error
208     <br>&nbsp; Test: testRangeException&nbsp; Ok
209     <br>&nbsp; Test: testOverlap Ok
210     <p>!!!FAILURES!!!
211     <br>Test Results:
212     <br>Run:&nbsp; 4&nbsp;&nbsp; Failures: 1&nbsp;&nbsp; Errors: 1
213     <br>There was 1 error:
214     <br>1) testWithin line: -1 &lt;unknown> "RangeException: Deliberate exception
215     from testWithin"
216     <br>There was 1 failure:
217     <br>1) testEquality line: 22 RangeTest.C "!((*r1)==(*r1))"
218     <p>Suite: R2Test
219     <br>&nbsp; Test: testEquality Failure
220     <br>&nbsp; Test: testWithin Error
221     <br>&nbsp; Test: testRangeException&nbsp; Ok
222     <br>&nbsp; Test: testOverlap Ok
223     <p>!!!FAILURES!!!
224     <br>Test Results:
225     <br>Run:&nbsp; 4&nbsp;&nbsp; Failures: 1&nbsp;&nbsp; Errors: 1
226     <br>There was 1 error:
227     <br>1) testWithin line: -1 &lt;unknown> "RangeException: Deliberate exception
228     from testWithin"
229     <br>There was 1 failure:
230     <br>1) testEquality line: 22 RangeTest.C "!((*r1)==(*r1))"
231     <p><font color="#3366FF"><font size=+2>Building</font></font>
232     <p><font color="#330000">In order to build an executable linked with the
233     UnitTest library use the Perth standard makefile with the option</font>
234     <br><font color="#330000">&nbsp; DOUNITTEST = YES</font>
235     <br>&nbsp;
236     <br>&nbsp;
237     <p><font color="#3366FF"><font size=+2>Run-time options</font></font>
238     <p><font color="#330000"><font size=+0>You can specify the test suites
239     that you want to run in a test by using arguments on the command line -</font></font>
240     <p><font color="#330000"><font size=+0>TestExeName [suite=suite1[,suite2...]]
241     [suite=suitea[,suiteb...]]</font></font>
242     <p><font color="#330000"><font size=+0>where suite1, suite2, suitea and
243     suiteb are the names of suites you want to run - suites can be repeated.</font></font>
244     <br>&nbsp;
245     <br>&nbsp;
246     <br>&nbsp;
247     <p><font color="#330000"><font size=+0>For example, in the range test above
248     which has two suites (RangeTestCase and R2Test). The test executable is
249     called RangeTest. You could use</font></font>
250     <p><font color="#330000"><font size=+0>RangeTest</font></font>
251     <p><font color="#330000"><font size=+0>to run both suites, or</font></font>
252     <p><font color="#330000"><font size=+0>RangeTest suite=R2Test</font></font>
253     <p><font color="#330000"><font size=+0>to just run the R2Test suite, or</font></font>
254     <p><font color="#330000"><font size=+0>RangeTest suite=R2Test,R2Test,RangeTestCase
255     suite=RangeTestCase suite=R2Test,R2Test</font></font>
256     <p><font color="#330000"><font size=+0>to run R2Test, followed by R2Test
257     , followed by RangeTestCase, followed by RangeTestCase, followed by two
258     more R2Test runs.</font></font>
259     <br>&nbsp;
260     <br>&nbsp;
261     <br>&nbsp;
262     <p><font color="#3366FF"><font size=+2>Run-time options</font></font>
263     <p><font color="#330000"><font size=+0>Run-time arguments can be passed
264     through to the individual tests. In a test case, you can call the getArgs
265     function to obtain a const vector&lt;string>&amp; to a vector that contains
266     the command line arguments - note that all the suite=... arguments will
267     have been stripped out. If the system being run provides the name of the
268     executable as the first argument to the main function, then this will be
269     available to you as the first string in the argument vector (in whatever
270     format the system provides it).</font></font>
271     <p><font color="#330000"><font size=+0>For example in the testRangeException
272     function, the run time arguments could be obtained by changing the code
273     to the following -</font></font>
274     <p><font color="#330000"><font size=+0>void RangeTestCase::testRangeException()
275     {</font></font>
276     <br><b><font color="#330000"><font size=+0>const vector&lt;string>&amp;
277     args = getArgs();</font></font></b>
278     <br><font color="#330000"><font size=+0>if(args.size()>0) {</font></font>
279     <br><font color="#330000"><font size=+0>cout &lt;&lt; "exe name is " &lt;&lt;
280     args[0] &lt;&lt; endl;</font></font>
281     <br><font color="#330000"><font size=+0>}</font></font>
282     <br><font color="#330000"><font size=+0>&nbsp; try {</font></font>
283     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; //</font></font>
284     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; // This call
285     should throw an exception as the range is invalid</font></font>
286     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; (*r1).setRange(3,1);</font></font>
287     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; //</font></font>
288     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; // if the test
289     gets here the exception has failed</font></font>
290     <br><font color="#330000"><font size=+0>&nbsp;&nbsp;&nbsp; assert(false);</font></font>
291     <br><font color="#330000"><font size=+0>&nbsp; }</font></font>
292     <br><font color="#330000"><font size=+0>&nbsp; catch (RangeException &amp;e)
293     {</font></font>
294     <br><font color="#330000"><font size=+0>&nbsp; }</font></font>
295     <br><font color="#330000"><font size=+0>}</font></font>
296     <br>&nbsp;
297     <br>&nbsp;
298     </body>
299     </html>

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26