1 /* -*- Indented-Text -*- */
2 /* Copyright (C) 2003, 2010 TSUTSUMI Kikuo.
3 This file is part of the CCUnit Library.
5 The CCUnit Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
10 The CCUnit Library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the CCUnit Library; see the file COPYING.LESSER.
17 If not, see <http://www.gnu.org/licenses/>
22 @page cookbook CCUnit Cookbook
24 @page cookbook CCUnit ¥¯¥Ã¥¯¥Ö¥Ã¥¯
28 Here is a short cookbook to help you get started.
30 ¤³¤ì¤Ï CCUnit ¤ò»È¤¤»Ï¤á¤ë¤Ë¤¢¤¿¤Ã¤Æ¡¢
31 Íý²ò¤Î½õ¤±¤È¤Ê¤ë¤è¤¦¤Êû¤¤¥¯¥Ã¥¯¥Ö¥Ã¥¯¤Ç¤¹¡£
34 [see also <a href="../ja/cookbook.html">Japanese</a> documents]
36 [see also <a href="../../cookbook.html">English</a> documents]
40 @section simple_test_case Simple Test Case
42 @section simple_test_case ¥·¥ó¥×¥ë¤Ê¥Æ¥¹¥È¥±¡¼¥¹
46 Tests in <b>CCUnit</b> can be run automatically. They are
47 easy to set up and once you have written them, they are
48 always there to help you keep confidence in the quality of
51 CCUnit¤ò»È¤Ã¤¿¥Æ¥¹¥È¤Ï¼«Æ°Åª¤Ë¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
52 CCUnit ¤Î¥Æ¥¹¥È¤Ï´Êñ¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¤³¤È¤¬¤Ç¤¡¢
53 °ìÅ٥ƥ¹¥È¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
54 ¤¤¤Ä¤Ç¤â¥×¥í¥°¥é¥à¤ÎÉʼÁ¤ò¿®Íê¤Ç¤¤ë¤â¤Î¤ËÊݤĤ³¤È¤¬¤Ç¤¤ë¤Ç¤·¤ç¤¦¡£
58 To make a simple test, here is what you do:
60 ñ½ã¤Ê¥Æ¥¹¥È¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
64 <li> @~english Create a test function
65 @~japanese ¥Æ¥¹¥È´Ø¿ô¤òºî¤ë
68 <li> @~english When you want to check a value, call
69 @~japanese Ãͤò¥Á¥§¥Ã¥¯¤·¤¿¤¤¾ì¹ç¤Ï¡¢@~
70 @link CCUNIT_ASSERT() CCUNIT_ASSERT(bool) @endlink
71 @~english and pass a bool that is true if the test succeeds
72 @~japanese ¤ò¸Æ¤Ó½Ð¤·¤Æ¡¢
73 ¤â¤·¥Æ¥¹¥È¤¬À®¸ù¤¹¤ë¤Ê¤é¿¿¤òÊÖ¤¹¤è¤¦¤Ê¿¿µ¶ÃͤòÅϤ·¤Þ¤¹@~
75 @~english ASSERT macro is listed in others as well in
76 @link Assertions Making assertions @endlink.
77 @~japanese ASSERT ´Ø·¸¤Î¥Þ¥¯¥í¤Ï¾¤Ë¤â
78 @link Assertions Making assertions @endlink
79 ¤Ë¥ê¥¹¥È¤·¤Æ¤¢¤ê¤Þ¤¹¡£
85 For example, to test that the sum of two complex number
86 which is the sum of the values of two complex numbers,
89 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤Î¹ç·×¤¬¡¢
90 Æó¤Ä¤ÎÊ£ÁÇ¿ô¤ÎÃͤò²Ã»»¤·¤¿ÃͤÈƱ¤¸¤Ç¤¢¤ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤È¤·¤Þ¤¹¡£
91 ¡Ê¤³¤³¤Ç»ÈÍѤ¹¤ë¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï
92 examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£¡Ë
96 This sample program is in the examples/complex directory.
100 void test_complex_add ()
102 complex_t c10_1 = { 10.0, 1.0 };
103 complex_t c1_1 = { 1.0, 1.0 };
105 complex_t c11_2 = { 11.0, 2.0 };
106 CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
111 That was a very simple test. Ordinarily, you'll have many
112 little test cases that you'll want to run on the same set of
113 objects. To do this, use a fixture.
115 ¤³¤ì¤ÏÂçÊÑñ½ã¤Ê¥Æ¥¹¥È¤Ç¤¹¡£
116 Ä̾Ʊ¤¸¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ÇÁö¤é¤»¤ë¤¿¤á¤Ë¡¢
117 ¤¿¤¯¤µ¤ó¤Î¾®¤µ¤Ê¥Æ¥¹¥È¥±¡¼¥¹¤òºî¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤Ç¤·¤ç¤¦¡£
118 ¤½¤³¤Ç¥Õ¥£¥¯¥¹¥Á¥ã¡ÊÈ÷Éʡˤò»È¤¤¤Þ¤¹¡£
122 @section fixture Fixture
124 @section fixture ¥Õ¥£¥¯¥¹¥Á¥ã
128 What if you have two or more tests that operate on the same
129 similar set of objects?
131 ¤â¤·Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤¬¤¢¤ë¤Ê¤é¡¢
132 Ʊ¤¸Îà»÷¤Î¥Ç¡¼¥¿¤Î½¸¤Þ¤ê¤ÇÁàºî¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¤«¡£
137 node [ fontsize=9, fontname=Helvetica ]
138 edge [ fontsize=9, dir=back, arrowtail=vee ]
139 gv [ label="Global variables", shape=box ]
141 tc1 [ label="Test case 1" ];
142 tcn [ label="Test case n" ];
146 tc1 -> tcn [ style=dotted, arrowtail=none ];
151 Tests need to run against the background of a known set of
152 objects. This set of objects is called a test fixture. When
153 you are writing tests you will often find that you spend
154 more time writing the code to set up the fixture than you do
155 in actually testing values.
157 ¥Æ¥¹¥È¤Ï¼þÃΤΥǡ¼¥¿¤Î½¸¤Þ¤ê¤òÇطʤˤ·¤Æ¼Â¹Ô¤µ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
158 ¤³¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡£
159 ¥Æ¥¹¥È¤ò½ñ¤¤¤Æ¤¤¤ë¤È¡¢
160 ¼ÂºÝ¤Î¥Æ¥¹¥È¤¹¤ëÃͤò¥Õ¥£¥¯¥¹¥Á¥ã¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¥³¡¼¥É¤ò½ñ¤¯Êý¤Ë¡¢
161 ¤è¤ê»þ´Ö¤ò¤«¤±¤Æ¤¤¤ë¤³¤È¤Ëµ¤¤Å¤¯¤³¤È¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£
165 Often, you will be able to use the same fixture for several
166 different tests. Each case will send slightly different
167 messages or parameters to the fixture and will check for
170 ¿¤¯¤Î¾ì¹ç¡¢¤¤¤¯¤Ä¤«¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¤Î¤¿¤á¤ËƱ¤¸¥Õ¥£¥¯¥¹¥Á¥ã¤ò»È¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
171 ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ï¾¯¤·°Û¤Ê¤Ã¤¿¥á¥Ã¥»¡¼¥¸¡¢
172 ¤¢¤ë¤¤¤Ï¥Ñ¥é¥á¡¼¥¿¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤ËÁ÷¤ê¡¢
173 °Û¤Ê¤ë·ë²Ì¤òÄ´¤Ù¤ë¤³¤È¤Ë¤Ê¤ë¤Ç¤·¤ç¤¦¡£
177 When you have a common fixture, here is what you do:
179 ¤â¤·¶¦Ä̤¹¤ë¥Õ¥£¥¯¥¹¥Á¥ã¤¬¤¢¤ì¤Ð¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤¹¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
184 Create a @link CCUnitTestCase TestCase @endlink object
186 @link CCUnitTestCase TestCase @endlink ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
187 ¡Ê°Ê¹ß¡¢¹½Â¤ÂΤ˳ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò¡¢¥ª¥Ö¥¸¥§¥¯¥È¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡Ë
191 Add an global variable for each part of the fixture
193 ¥Õ¥£¥¯¥¹¥Á¥ã¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ËÂç°èÊÑ¿ô¤òÄɲä·¤Þ¤¹
197 Write <code>setUp()</code> function to initialize the valiables
199 <code>setUp()</code> ´Ø¿ô¤ò½ñ¤¤¤ÆÊÑ¿ô¤ò½é´ü²½¤·¤Þ¤¹
203 Write <code>tearDown()</code> to release any permanent
204 resources you allocated in <code>setUp</code>
206 <code>tearDown()</code> ´Ø¿ô¤ò½ñ¤¤¤Æ<code>setUp</code>
207 ¤Ç³ä¤êÅö¤Æ¤¿±Ê³Ū¥ê¥½¡¼¥¹¤ò²òÊü¤·¤Þ¤¹
211 Create a @link CCUnitTestFixture TestFixture @endlink object
213 @link CCUnitTestFixture TestFixture @endlink
214 ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
218 Add @link CCUnitTestCase TestCase @endlink objects to fixture object
220 @link CCUnitTestCase TestCase @endlink ¥ª¥Ö¥¸¥§¥¯¥È¤ò
221 @link CCUnitTestFixture TestFixture @endlink
222 ¥Õ¥£¥¯¥¹¥Á¥ã¥ª¥Ö¥¸¥§¥¯¥È¤ËÅÐÏ¿¤·¤Þ¤¹¡£
228 digraph TestFixture {
229 node [ fontsize=9, fontname=Helvetica ]
230 edge [ fontsize=9, dir=back, arrowtail=vee ]
231 gv [ label="Global variables", shape=box ]
233 su [ label="setUp ()" ];
234 tc [ label="TestCase" ];
235 td [ label="tearDown ()" ];
237 gv -> su [ label="initialize" ];
238 gv -> tc [ label="access" ];
239 gv -> td [ label="clean-up" ];
244 For example, to write several test cases, first create a
247 Î㤨¤Ð¡¢¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¾ì¹ç¡¢
248 ºÇ½é¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Þ¤¹¡£
252 //** TEST CASE: complex number test *\/
256 static complex_t* s10_1;
257 static complex_t* s1_1;
258 static complex_t* s11_2;
260 void setUp_complex_test ()
262 s10_1 = complex_new (10, 1);
263 s1_1 = complex_new (1, 1);
264 s11_2 = complex_new (11, 2);
267 void tearDown_complex_test ()
269 complex_delete (s10_1);
270 complex_delete (s1_1);
271 complex_delete (s11_2);
276 CCUnitTestFixture* fixture;
277 fixture = ccunit_newTestFixture ("complex test",
278 CCUNIT_NEWTESTFUNC(setUp_complex_test),
279 CCUNIT_NEWTESTFUNC(tearDown_complex_test));
283 Once you have the Fixture in place, you can write as complex
284 Test Cases as you'd like.
286 °ìÅÙ·è¤Þ¤Ã¤¿¤È¤³¤í¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
287 ¤¢¤Ê¤¿¤¬¹¥¤¤Ê¤è¤¦¤ËÊ£ÁÇ¿ô¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
291 @section test_case Test Case
293 @section test_case ¥Æ¥¹¥È¥±¡¼¥¹
297 How do you write and invoke an individual test case when you
300 ¥Õ¥£¥¯¥¹¥Á¥ã¤ò°ì¤Ä½ñ¤¤¤¿¤È¤·¤Æ¡¢
301 ¤É¤¦¤ä¤Ã¤Æ¸Ä¡¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¤¤Æ¼Â¹Ô¤¹¤ì¤ÐÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
305 For example, to test the equality of two complex number,
308 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤¬Åù¤·¤¤¡Ê¤Þ¤¿¤ÏÅù¤·¤¯¤Ê¤¤¡Ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤Ë¤Ï¡¢
313 void test_complex_equals ()
315 CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
316 CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
321 ccunit_addNewTestCase (fixture,
322 "test_complex_equals",
323 "complex equals test",
324 test_complex_equals);
328 One may create and run objects for each test case like this:
331 ¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Æ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò¼Â¹Ô¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
335 CCUnitTestResult* result;
336 result = ccunit_runTestFixture (fixture);
340 When the test fixture is run, that specific test functions
341 will be run. This is not a useful thing to do, however, as
342 no diagnostics will be displayed. One will normally use a
343 @link ExecutingTest TestRunner @endlink (see below) to
346 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤¬¼Â¹Ô¤µ¤ì¤ë¤È¡¢
347 ÆÃÄê¤Î¥Æ¥¹¥È´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
348 ¤³¤ì¤Ï¤¢¤Þ¤êÊØÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡¢
349 ¤Ê¤¼¤Ê¤é¡¢¿ÇÃǤ¬É½¼¨¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¹¡£
350 Ä̾ï¤Ï @link ExecutingTest TestRunner @endlink
351 (@ref test_runner ¸å½Ò)
356 Once you have several tests, organize them into a suite.
358 °ìÅÙ¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¤òºî¤Ã¤¿¤é¡¢
359 ¤½¤ì¤é¤ò¥¹¡¼¥Ä¤ËÀ°Íý¤·¤Þ¤¹¡£
365 @section suite ¥¹¡¼¥Ä
369 How do you set up your tests so that you can run them all at once?
371 °ìÅ٤˥ƥ¹¥È¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë½àÈ÷¤¹¤ë¤Ë¤Ï¡¢
372 ¤É¤Î¤è¤¦¤Ë¤·¤¿¤é¤¤¤¤¤Ç¤·¤ç¤¦¤«¡©
376 CCUnit provides a @link CCUnitTestSuite TestSuite @endlink
377 module that runs any number of TestCases together.
379 CCUnit ¤Ï¤¤¤¯¤Ä¤â¤Î @link CCUnitTestCase TestCases @endlink
381 @link CCUnitTestSuite TestSuite @endlink ¥â¥¸¥å¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
385 You saw, above, how to run test fixture.
387 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤ò¼Â¹Ô¤¹¤ëÊýË¡¤Ï¾å½Ò¤·¤Þ¤·¤¿¡£
391 To create a suite of two or more tests, you do the following:
393 Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤ò´Þ¤à°ì¤Ä¤Î¥¹¡¼¥Ä¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
397 CCUnitTestSuite* suite;
398 CCUnitTestFixture* fixture;
399 CCUnitTestResult* result;
400 suite = ccunit_newTestSuite ("Complex test suite");
401 fixture = ccunit_newTestFixture ("Complex Tests",
402 CCUNIT_NEWTESTFUNC(setUp_complex_test),
403 CCUNIT_NEWTESTFUNC(tearDown_complex_test));
404 ccunit_addNewTestCase (fixture, "test_complex_equals", "complex equals test",
405 test_complex_equals);
406 ccunit_addNewTestCase (fixture, "test_complex_add", "complex add test",
408 ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
410 ccunit_addTestFixture (suite, fixtuer);
411 result = ccunit_runTestSuite (suite, NULL);
415 @link CCUnitTestSuite TestSuites @endlink don't only have to
416 contain @link CCUnitTestFixture TestFixtures @endlink. They
417 can contain any object that implements the @link CCUnitTest
418 Test @endlink interface. For example, you can create a
419 @link CCUnitTestSuite TestSuite @endlink
420 (<code>complex_add_sub_suite ()</code>) in your code and I
421 can create one in mine
422 (<code>complex_mul_div_suite ()</code>), and we can run them together by
423 creating a @link CCUnitTestSuite TestSuite @endlink that
426 @link CCUnitTestSuite TestSuites @endlink ¤Ï
427 @link CCUnitTestFixture TestFixtures @endlink
428 ¤ò´Þ¤à¤À¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
429 @link CCUnitTestSuite TestSuites @endlink ¼«¿È¤ò´Þ¤à¤³¤È¤â¤Ç¤¤Þ¤¹¡£
431 ¤½¤ì¤é¤Ï @link CCUnitTest Test @endlink
432 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤¹¤ë¤É¤ó¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤â´Þ¤á¤é¤ì¤Þ¤¹¡£
434 Î㤨¤Ð¡¢¤¢¤Ê¤¿¤Ï¤¢¤Ê¤¿¤Î¥³¡¼¥É¤Ë @link CCUnitTestSuite TestSuite @endlink
435 (<code>complex_add_sub_suite ()</code>)
438 (<code>complex_mul_div_suite ()</code>)
439 ¤òºî¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡¢
440 ¤½¤·¤Æ»äã¤ÏξÊý¤È¤â¤ò´Þ¤ó¤Ç¤¤¤ë @link CCUnitTestSuite TestSuite @endlink
441 ¤òºî¤Ã¤Æ°ì½ï¤Ë¤½¤ì¤¾¤ì¤Î¥¹¡¼¥Ä¤òÆ°¤«¤¹¤³¤È¤¬¤Ç¤¤ë¤Î¤Ç¤¹¡£
445 CCUnitTestSuite* suite;
446 CCUnitTestResult* result;
447 suite = ccunit_newTestSuite ("Complex add/sub/mul/div test suite");
448 ccunit_addTestSuite (suite, complex_add_sub_suite ());
449 ccunit_addTestSuite (suite, complex_mul_div_suite ());
450 result = ccunit_runTestSuite(suite, NULL);
455 @section test_runner TestRunner
457 @section test_runner ¥Æ¥¹¥È¥é¥ó¥Ê¡¼
461 How do you run your tests and collect their results?
463 ¤É¤¦¤ä¤Ã¤Æ¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢¤½¤Î·ë²Ì¤ò½¸¤á¤¿¤éÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
467 Once you have a test suite, you'll want to run it. %CCUnit
468 provides tools to define the suite to be run and to display
469 its results. You make your suite accessible to a @link
470 CreatingTestSuite ccunit_makeSuite @endlink tool that generate a
471 creating test suite code.
473 °ì¤Ä¥Æ¥¹¥È¥¹¡¼¥Ä¤ò½ñ¤¤¤¿¤é¡¢
474 ¤½¤ì¤ò¼Â¹Ô¤·¤¿¤¤¤Ç¤·¤ç¤¦¡£
475 CCUnit ¤Ï¥¹¡¼¥Ä¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÄêµÁ¤·¡¢
476 ·ë²Ì¤òɽ¼¨¤¹¤ë¤¿¤á¤Î¥Ä¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
477 ¥¹¡¼¥Ä¤ò @link CreatingTestSuite ccunit_makeSuite @endlink
478 ¥Ä¡¼¥ë¤ËÆþÎϤǤ¤ë¤è¤¦¤Ê·Á¼°¤Ç½ñ¤¯¤³¤È¤Ç¡¢
479 ¥Æ¥¹¥È¥¹¡¼¥Ä¤òºîÀ®¤¹¤ë¥³¡¼¥É¤ò¼«Æ°Åª¤ËÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
483 For example, to make a ComplexTest suite available to a
484 @link CreatingTestSuite ccunit_makeSuite @endlink,
485 excute the following tool to
488 Î㤨¤Ð¡¢ComplexTest ¥¹¡¼¥Ä¤ò
489 @link CreatingTestSuite ccunit_makeSuite @endlink
490 ¤ò»È¤Ã¤Æ»ÈÍѤǤ¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
491 °Ê²¼¤Î¥Ä¡¼¥ë¤ò testComplex.c ¤Ë¼Â¹Ô¤·¤Þ¤¹¡£
495 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
498 @anchor test_runner_code
501 <code>complex_suite</code> ¤È¤¤¤¦´Ø¿ô¤¬ÄêµÁ¤µ¤ì¤¿¡¢
502 <code>suiteComplex.c</code> ¤È¤¤¤¦¥Õ¥¡¥¤¥ë¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£
506 To use the TestRunner, include the header files for the tests in runTest.c:
508 TestRunner ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢
509 Î㤨¤Ð runTest.c ¤Ç¥Æ¥¹¥È¤Î¤¿¤á¤Î¥Õ¥¡¥¤¥ë¤Î¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Þ¤¹¡£
513 #include <ccunit/CCUnitTestRunner.h>
514 #include <ccunit/CCUnitTestSuite.h>
519 @link ccunit_runTestRunner()
520 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
521 in the <code>main()</code> function:
523 ¤½¤·¤Æ@link ccunit_runTestRunner()
524 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
525 ¤ò<code>main()</code>´Ø¿ô¤Ç¼Â¹Ô¤·¤Þ¤¹¡£
529 extern CCUnitTestSuite* complex_suite(const char* name);
531 int main( int argc, char **argv)
533 CCUnitTestRunner* runner;
534 CCUnitTestSuite* suite;
535 runner = ccunit_newTestRunner (stdout);
536 suite = complex_suite ("complex test suite");
537 return ccunit_runTestRunner (runner, suite);
542 The @link ExecutingTest TestRunner @endlink will run the tests.
543 If all the tests pass, you'll get an informative message.
544 If any fail, you'll get the following information:
546 @link ExecutingTest TestRunner @endlink ¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¤Þ¤¹¡£
547 ¤â¤·¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¤¬¥Ñ¥¹¤¹¤ì¤Ð¡¢¤½¤Î¾ðÊó¤Î¥á¥Ã¥»¡¼¥¸¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
548 ¤â¤·¤É¤ì¤«¤¬¼ºÇÔ¤¹¤ì¤Ð¡¢¤½¤ì¤Ë¤Ä¤¤¤Æ°Ê²¼¤Î¤è¤¦¤Ê¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
553 The name of the source file that contains the test
555 ¥Æ¥¹¥È¤ò´Þ¤ó¤Ç¤¤¤ë¥½¡¼¥¹¥Õ¥¡¥¤¥ë̾
559 The line number where the failure occurred
565 The name of the test case that failed
567 ¼ºÇÔ¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹¤Î̾Á°
571 All of the text inside the call to
572 <code>CCUNIT_ASSERT ()</code> which detected the failure
574 ¼ºÇÔ¤ò¸¡ÃΤ·¤¿<code>CCUNIT_ASSERT ()</code>¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Î¾ò·ï¤Îʸ»úÎó¡£
580 @section helper_macros Helper Tool
582 @section helper_macros ¥Ø¥ë¥Ñ¡¼¥Ä¡¼¥ë
586 As you might have noticed, implementing the <code>suite
587 ()</code> function of fixture is a repetitive and error
588 prone task. A @ref CreatingTestSuite set of functions and
589 command have been created to automatically implements the
590 <code>suite()</code> function.
592 ¤ªµ¤¤Å¤¤Î¤è¤¦¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î <code>suite ()</code>´Ø¿ô¤ò¼ÂÁõ¤¹¤ë¤Î¤Ï¡¢
593 È¿ÉüŪ¤Ç´Ö°ã¤¤¤ä¤¹¤¤ºî¶È¤Ç¤¹¡£
594 @ref CreatingTestSuite ¤Î´Ø¿ô¤Î¥»¥Ã¥È¤È¥³¥Þ¥ó¥É¤Ï<code>suite ()</code>
595 ´Ø¿ô¤Î¼ÂÁõ¤ò¼«Æ°Åª¤ËºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
598 @dontinclude complex/testComplex.c
601 The following code is a rewrite of ComplexTest using those command:
603 °Ê²¼¤Î¥³¡¼¥É¤Ï¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤¬»È¤¦¤è¤¦¤ËComplexTest¤ò½ñ´¹¤¨¤¿¤â¤Î¤Ç¤¹¡£
607 #include <ccunit/CCUnitAssert.h>
611 First, you declare the fixture, passing the test fixture
612 name to the javaDoc style comment, which consist of a
613 C-style comment block starting with two <tt>*</tt>'s:
615 ºÇ½é¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤òÀë¸À¤·¤Þ¤¹¡£
616 ¤³¤ì¤ÏjavaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥ÈÆâ¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤Î̾Á°¤òµ½Ò¤·¤Þ¤¹¡£
617 javaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¤È¤Ï C ¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î³«»Ï¤¬
618 Æó¤Ä¤Î¥¢¥¹¥¿¥ê¥¹¥¯ <tt>**</tt> ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
622 //** test case: complex number test *\/
626 The function to make @link CCUnitTestSuite TestSuite
627 @endlink is <code>ccunit_suite</code>.
628 But, it can be changed to another
629 name by the <code>-f</code> option of the
630 <code>ccunit_makeSuite</code> command, too.
631 Then, you define each test case of the fixture with prefix
632 <code>test</code>, <code>setUp</code>,
633 <code>tearDown</code>:
636 @link CCUnitTestSuite TestSuite @endlink
637 ¤òºîÀ®¤¹¤ë´Ø¿ô¤Ï <code>ccunit_suite</code> ¤Ç¤¹¡¢
638 ¤·¤«¤· <code>ccunit_makeSuite</code> ¥³¥Þ¥ó¥É¤Î
639 <code>-f</code> ¥ª¥×¥·¥ç¥ó¤ÇÊ̤Î̾Á°¤ËÊѤ¨¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
640 ¤½¤·¤Æ¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ë¤Ï¤½¤ì¤¾¤ì̾Á°¤ÎÀèƬ¤Ë¡¢
641 <code>test</code>, <code>setUp</code>,
642 <code>tearDown</code> ¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
648 static complex_t* s10_1;
649 static complex_t* s1_1;
650 static complex_t* s11_2;
652 void setUp_complex_test ()
654 s10_1 = complex_new (10, 1);
655 s1_1 = complex_new (1, 1);
656 s11_2 = complex_new (11, 2);
659 void tearDown_complex_test ()
661 complex_delete (s10_1);
662 complex_delete (s1_1);
663 complex_delete (s11_2);
667 void test_complex_equals ()
669 CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
670 CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
674 void test_complex_add ()
676 complex_t c10_1 = { 10.0, 1.0 };
677 complex_t c1_1 = { 1.0, 1.0 };
679 complex_t c11_2 = { 11.0, 2.0 };
680 CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
684 void test_complex_sub ()
686 complex_t c9_0 = { 9, 0 };
688 CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
689 complex_sub (&result, s10_1, s1_1),
695 Finally, you end the fixture declaration:
697 ºÇ¸å¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î½ª¤ê¤òÀë¸À¤·¤Þ¤¹¡£
701 //** end test case *\/
705 To generate creating suite function code, run
706 <code>ccunit_makeSuite</code> tool.
708 ¥¹¡¼¥ÄºîÀ®´Ø¿ô¤Î¥³¡¼¥É¤òÀ¸À®¤¹¤ë¤Ë¤Ï¡¢
709 <code>ccunit_makeSuite</code>¥Ä¡¼¥ë¤ò¼Â¹Ô¤·¤Þ¤¹¡£
713 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
716 #include <ccunit/CCUnitTestSuite.h>
717 #include <ccunit/CCUnitTestFixture.h>
718 #include <ccunit/CCUnitTestCase.h>
720 //* test fixture: complex number test *\/
721 //* setUp_complex_test *\/
722 extern void setUp_complex_test ();
723 //* tearDown_complex_test *\/
724 extern void tearDown_complex_test ();
725 //* test_complex_equals *\/
726 extern void test_complex_equals ();
727 //* test_complex_add *\/
728 extern void test_complex_add ();
729 //* test_complex_sub *\/
730 extern void test_complex_sub ();
732 static CCUnitTestFunc fx_001_cases[] = {
734 "test_complex_equals",
753 static CCUnitTestFixtureDfn fx_001 = {
754 { ccunitTypeFixture },
755 "complex number test",
757 "setUp_complex_test",
758 "setUp_complex_test",
762 "tearDown_complex_test",
763 "tearDown_complex_test",
764 tearDown_complex_test
769 static CCUnitTestDfn* suite_001_test[] = {
774 static CCUnitTestSuiteDfn suite_001 = {
781 CCUnitTestSuite* complex_suite (const char* name)
783 if (!suite_001.name[0])
784 suite_001.name = name;
785 return ccunit_newTestSuiteFromDfn (&suite_001);
791 @section post_build_check Post-build check
793 @section post_build_check ¥Ó¥ë¥É¸å¤Î¥Á¥§¥Ã¥¯
797 Now that we have our unit tests running, how about
798 integrating unit testing to our build process ?
800 ¤µ¤¢¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë½àÈ÷¤¬¤Ç¤¤Þ¤·¤¿¡£
801 ¤Ç¤Ï¥Ó¥ë¥É¥×¥í¥»¥¹¤Ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤òÅý¹ç¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤é¤¤
806 To do that, the application must returns a value different than 0 to indicate that
809 ¤½¤¦¤¹¤ë¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤³¤È¤ò¼¨¤¹
810 £°°Ê³°¤ÎÃͤòÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
814 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink returns
815 a integer indicating if the run was successful.
817 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink
818 ¤Ï¡¢¼Â¹Ô¤¬À®¸ù¤·¤¿¤«¤É¤¦¤«¤ò¼¨¤¹À°¿ô¤òÊÖ¤·¤Þ¤¹¡£
822 Updating our main programm, we obtains:
824 ¼¡¤Î¤è¤¦¤Ë¥á¥¤¥ó¥×¥í¥°¥é¥à¤ò¹¹¿·¤·¤Þ¤¹¡£
828 #include <ccunit/CCUnitTestRunner.h>
830 int main (int argc, char** argv)
832 CCUnitTestRunner* runner;
833 CCUnitTestSuite* suite;
835 runner = ccunit_newTestRunner (stdout);
836 suite = ccunit_suite ();
837 wasSucessful = ccunit_runTestRunner (runner, suite);
843 Now, you need to run your application after compilation.
844 The sample program made in the above is in the examples/complex directory.
846 ¤½¤ì¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤¿¸å¤Ë¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
847 °Ê¾å¤ÇºîÀ®¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£