/* -*- Indented-Text -*- */
+/* $Id$ */
/** @page cookbook CCUnit Cookbook
Here is a short cookbook to help you get started.
To make a simple test, here is what you do:
<ol>
-<li> Create a test function
+<li> Create a test function</li>
<li> When you want to check a value, call
@link CCUNIT_ASSERT() CCUNIT_ASSERT(bool) @endlink
- and pass a bool that is true if the test succeeds
+ and pass a bool that is true if the test succeeds</li>
</ol>
For example, to test that the sum of two Moneys with the
@code
void testSimpleAdd ()
{
- Money* m120Yen = Money_new (120, "Yen");
- Money* m140Yen = Money_new (140, "Yen");
- Money* expected = Money_new (260, "Yen");
- Money* result = Money_add (m120Yen, m140Yen);
+ Money* m120JPY = Money_new (120, "JPY");
+ Money* m140JPY = Money_new (140, "JPY");
+ Money* expected = Money_new (260, "JPY");
+ Money* result = Money_add (m120JPY, m140JPY);
CCUNIT_ASSERT (Money_equals (expected, result));
- Money_delete (m120Yen);
- Money_delete (m140Yen);
+ Money_delete (m120JPY);
+ Money_delete (m140JPY);
Money_delete (expected);
Money_delete (result);
}
When you have a common fixture, here is what you do:
<ol>
-<li> Create a @link CCUnitTestCase TestCase @endlink object
-<li> Add an global variable for each part of the fixture
-<li> Write @c setUp() function to initialize the valiables
-<li> Write @c tearDown() to release any permanent resources
-you allocated in @c setUp
-<li> Create a @link CCUnitTestFixture TestFixture @endlink
-object
-<li> Add test case objects to fixture object
+<li>Create a @link CCUnitTestCase TestCase @endlink object</li>
+<li>Add an global variable for each part of the fixture</li>
+<li>Write <code>setUp()</code> function to initialize the valiables</li>
+<li>Write <code>tearDown()</code> to release any permanent
+ resources you allocated in <code>setUp</code></li>
+<li>Create a @link CCUnitTestFixture TestFixture @endlink object</li>
+<li>Add test case objects to fixture object</li>
</ol>
For example, to write several test cases that want to work
Japanese Yen, and 28 US Dollars, first create a fixture:
@code
-/*\* TEST CASE: Money test */
+//** TEST CASE: Money test *\/
-static Money* s120Yen;
-static Money* s140Yen;
+#include "Money.h"
+
+static Money* s120JPY;
+static Money* s140JPY;
static Money* s23USD;
void setUp_moneyTest ()
{
- s120Yen = Money_new (120, "Yen");
- s140Yen = Money_new (140, "Yen");
+ s120JPY = Money_new (120, "JPY");
+ s140JPY = Money_new (140, "JPY");
s23USD = Money_new (23, "USD");
}
-
+
void tearDown_moneyTest ()
{
- Money_delete (s120Yen);
- Money_delete (s140Yen);
+ Money_delete (s120JPY);
+ Money_delete (s140JPY);
Money_delete (s23USD);
}
@code
void testMoneyMoneyBag ()
{
- Money* bag[] = { s260Yen, s28USD };
+ Money* bag[] = { s260JPY, s28USD };
MoneyBag expected = MoneyBag_new (2, bag);
- Money* subtotal = Money_add (s120Yen, s28USD);
- Money* total = Money_add (subtotal, s140Yen);
+ Money* subtotal = Money_add (s120JPY, s28USD);
+ Money* total = Money_add (subtotal, s140JPY);
CCUNIT_ASSERT (MoneyBag_equals (expected, total));
MoneyBag_delete (expected);
Money_delete (subtotal);
MoneyTest.c:
@code
-$ ccunit_makeSuite -f makeSuite -o makeMoneyTestSuite.c MoneyTest.c
+$ ccunit_makeSuite -f MoneyTest_suite -o makeMoneyTestSuite.c MoneyTest.c
@endcode
@anchor test_runner_code
To use the TestRunner, include the header files for the tests in Main.c:
@code
-\#include <ccunit/CCUnitTestRunner.h>
-\#include <ccunit/CCUnitTestSuite.h>
+#include <ccunit/CCUnitTestRunner.h>
+#include <ccunit/CCUnitTestSuite.h>
@endcode
And add a call to
in the <code>main()</code> function:
@code
-extern CCUnitTestSuite* makeSuite ();
+extern CCUnitTestSuite* MoneyTest_suite(const char* name);
int main( int argc, char **argv)
{
CCUnitTestRunner* runner;
CCUnitTestSuite* suite;
runner = ccunit_newTestRunner (stdout);
- suite = makeSuite ("MathTest Suite");
+ suite = MoneyTest_suite ("MathTest Suite");
ccunit_runTestRunner (runner, suite);
return 0;
}
The following code is a rewrite of MoneyTest using those command:
@code
-\#include <cppunit/CCUnitAssert.h>
+#include <cppunit/CCUnitAssert.h>
@endcode
First, you declare the fixture, passing the test fixture
C-style comment block starting with two <tt>*</tt>'s:
@code
-/*\* test case: Money test */
+//** test case: Money test *\/
@endcode
The suite created by the <code>ccunit_suite()</code>
<code>setUp</code>, <code>tearDown</code>:
@code
-static Money* s120Yen;
-static Money* s140Yen;
-static Money* s260Yen;
+static Money* s120JPY;
+static Money* s140JPY;
+static Money* s260JPY;
static Money* s23USD;
void setUp_MathTest ()
{
- s120Yen = Money_new (120, "Yen");
- s140Yen = Money_new (140, "Yen");
- s260Yen = Money_new (260, "Yen");
+ s120JPY = Money_new (120, "JPY");
+ s140JPY = Money_new (140, "JPY");
+ s260JPY = Money_new (260, "JPY");
s23USD = Money_new (23, "USD");
}
void tearDown_Mathtest ()
{
- Money_delete (s120Yen);
- Money_delete (s140Yen);
- Money_delete (s260Yen);
+ Money_delete (s120JPY);
+ Money_delete (s140JPY);
+ Money_delete (s260JPY);
Money_delete (s23USD);
}
-/*\* simple add test */
+//** simple add test *\/
void test_simpleAdd ()
{
- Money* result = Money_add (s120Yen, s140Yen);
- CCUNIT_ASSERT (Money_equals (s260Yen, result));
+ Money* result = Money_add (s120JPY, s140JPY);
+ CCUNIT_ASSERT (Money_equals (s260JPY, result));
Money_delete (result);
}
-/*\* money bug test */
+//** money bug test *\/
void test_moneyBag()
{
- Money* bag[] = { s260Yen, s28USD };
+ Money* bag[] = { s260JPY, s28USD };
MoneyBag expected = MoneyBag_new (2, bag);
- Money* subtotal = Money_add (s120Yen, s28USD);
- Money* total = Money_add (subtotal, s140Yen);
+ Money* subtotal = Money_add (s120JPY, s28USD);
+ Money* total = Money_add (subtotal, s140JPY);
CCUNIT_ASSERT (MoneyBag_equals (expected, total));
MoneyBag_delete (expected);
Money_delete (subtotal);
Finally, you end the fixture declaration:
@code
-/*\* end test case */
+//** end test case *\/
@endcode
To generate creating suite function code, run
@code
$ ccunit_makeSuite MoneyTest.c
-\#include <ccunit/CCUnitTestSuite.h>
+#include <ccunit/CCUnitTestSuite.h>
static CCUnitTestSuite* newSuite_001 (const char* name);
-/* test fixture: Money test */
-/* setUp_MathTest */
+//* test fixture: Money test *\/
+//* setUp_MathTest *\/
extern void setUp_MathTest ();
-/* tearDown_Mathtest */
+//* tearDown_Mathtest *\/
extern void tearDown_Mathtest ();
-/* simple add test */
+//* simple add test *\/
extern void test_simpleAdd ();
-/* money bug test */
+//* money bug test *\/
extern void test_moneyBag ();
static CCUnitTestSuite* newSuite_001 (const char* name)
Updating our main programm, we obtains:
@code
-\#include <ccunit/TestRunner.h>
+#include <ccunit/CCUnitTestRunner.h>
int main (int argc, char** argv)
{