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 Ʊ¤¸Îà»÷¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ÇÁàºî¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¤«¡£
136 Tests need to run against the background of a known set of
137 objects. This set of objects is called a test fixture. When
138 you are writing tests you will often find that you spend
139 more time writing the code to set up the fixture than you do
140 in actually testing values.
142 ¥Æ¥¹¥È¤Ï¼þÃΤΥǡ¼¥¿¤Î¥»¥Ã¥È¤òÇطʤˤ·¤Æ¼Â¹Ô¤µ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
143 ¤³¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡£
144 ¥Æ¥¹¥È¤ò½ñ¤¤¤Æ¤¤¤ë¤È¡¢
145 ¼ÂºÝ¤Î¥Æ¥¹¥È¤¹¤ëÃͤò¥Õ¥£¥¯¥¹¥Á¥ã¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¥³¡¼¥É¤ò½ñ¤¯Êý¤Ë¡¢
146 ¤è¤ê»þ´Ö¤ò¤«¤±¤Æ¤¤¤ë¤³¤È¤Ëµ¤¤Å¤¯¤³¤È¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£
150 Often, you will be able to use the same fixture for several
151 different tests. Each case will send slightly different
152 messages or parameters to the fixture and will check for
155 ¿¤¯¤Î¾ì¹ç¡¢¤¤¤¯¤Ä¤«¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¤Î¤¿¤á¤ËƱ¤¸¥Õ¥£¥¯¥¹¥Á¥ã¤ò»È¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
156 ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ï¾¯¤·°Û¤Ê¤Ã¤¿¥á¥Ã¥»¡¼¥¸¡¢
157 ¤¢¤ë¤¤¤Ï¥Ñ¥é¥á¡¼¥¿¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤ËÁ÷¤ê¡¢
158 °Û¤Ê¤ë·ë²Ì¤òÄ´¤Ù¤Þ¤¹¡£
162 When you have a common fixture, here is what you do:
164 ¤â¤·¶¦Ä̤¹¤ë¥Õ¥£¥¯¥¹¥Á¥ã¤¬¤¢¤ì¤Ð¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤¹¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
169 Create a @link CCUnitTestCase TestCase @endlink object
171 @link CCUnitTestCase TestCase @endlink ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
172 ¡Ê°Ê¹ß¡¢¹½Â¤ÂΤ˳ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò¡¢¥ª¥Ö¥¸¥§¥¯¥È¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡Ë
176 Add an global variable for each part of the fixture
178 ¥Õ¥£¥¯¥¹¥Á¥ã¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ËÂç°èÊÑ¿ô¤òÄɲä·¤Þ¤¹
182 Write <code>setUp()</code> function to initialize the valiables
184 <code>setUp()</code> ´Ø¿ô¤ò½ñ¤¤¤ÆÊÑ¿ô¤ò½é´ü²½¤·¤Þ¤¹
188 Write <code>tearDown()</code> to release any permanent
189 resources you allocated in <code>setUp</code>
191 <code>tearDown()</code> ´Ø¿ô¤ò½ñ¤¤¤Æ<code>setUp</code>
192 ¤Ç³ä¤êÅö¤Æ¤¿±Ê³Ū¥ê¥½¡¼¥¹¤ò²òÊü¤·¤Þ¤¹
196 Create a @link CCUnitTestFixture TestFixture @endlink object
198 @link CCUnitTestFixture CCUnitTestFixture @endlink
199 ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
203 Add @link CCUnitTestCase TestCase @endlink objects to fixture object
205 @link CCUnitTestCase TestCase @endlink ¥ª¥Ö¥¸¥§¥¯¥È¤ò
206 @link CCUnitTestFixture TestFixture @endlink
207 ¥Õ¥£¥¯¥¹¥Á¥ã¥ª¥Ö¥¸¥§¥¯¥È¤ËÅÐÏ¿¤·¤Þ¤¹¡£
213 For example, to write several test cases, first create a
216 Î㤨¤Ð¡¢¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¾ì¹ç¡¢
217 ºÇ½é¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Þ¤¹¡£
221 //** TEST CASE: complex number test *\/
225 static complex_t* s10_1;
226 static complex_t* s1_1;
227 static complex_t* s11_2;
229 void setUp_complex_test ()
231 s10_1 = complex_new (10, 1);
232 s1_1 = complex_new (1, 1);
233 s11_2 = complex_new (11, 2);
236 void tearDown_complex_test ()
238 complex_delete (s10_1);
239 complex_delete (s1_1);
240 complex_delete (s11_2);
245 CCUnitTestFixture* fixture;
246 fixture = ccunit_newTestFixture ("complex test",
247 CCUNIT_NEWTESTFUNC(setUp_complex_test),
248 CCUNIT_NEWTESTFUNC(tearDown_complex_test));
252 Once you have the Fixture in place, you can write as complex
253 Test Cases as you'd like.
255 °ìÅÙ·è¤Þ¤Ã¤¿¤È¤³¤í¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
256 ¤¢¤Ê¤¿¤¬¹¥¤¤Ê¤è¤¦¤ËÊ£ÁÇ¿ô¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
260 @section test_case Test Case
262 @section test_case ¥Æ¥¹¥È¥±¡¼¥¹
266 How do you write and invoke an individual test case when you
269 ¥Õ¥£¥¯¥¹¥Á¥ã¤ò°ì¤Ä½ñ¤¤¤¿¤È¤·¤Æ¡¢
270 ¤É¤¦¤ä¤Ã¤Æ¸Ä¡¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¤¤Æ¼Â¹Ô¤¹¤ì¤ÐÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
274 For example, to test the equality of two complex number,
277 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤¬Åù¤·¤¤¡Ê¤Þ¤¿¤ÏÅù¤·¤¯¤Ê¤¤¡Ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤Ë¤Ï¡¢
282 void test_complex_equals ()
284 CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
285 CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
290 ccunit_addNewTestCase (fixture,
291 "test_complex_equals",
292 "complex equals test",
293 test_complex_equals);
297 One may create and run objects for each test case like this:
300 ¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Æ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò¼Â¹Ô¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
304 CCUnitTestResult* result;
305 result = ccunit_runTestFixture (fixture);
309 When the test fixture is run, that specific test functions
310 will be run. This is not a useful thing to do, however, as
311 no diagnostics will be displayed. One will normally use a
312 @link ExecutingTest TestRunner @endlink (see below) to
315 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤¬¼Â¹Ô¤µ¤ì¤ë¤È¡¢
316 ÆÃÄê¤Î¥Æ¥¹¥È´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
317 ¤³¤ì¤Ï¤¢¤Þ¤êÊØÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡¢
318 ¤Ê¤¼¤Ê¤é¡¢¿ÇÃǤ¬É½¼¨¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¹¡£
319 Ä̾ï¤Ï @link ExecutingTest TestRunner @endlink
320 (@ref test_runner ¸å½Ò)
325 Once you have several tests, organize them into a suite.
327 °ìÅÙ¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¤òºî¤Ã¤¿¤é¡¢
328 ¤½¤ì¤é¤ò¥¹¡¼¥Ä¤ËÀ°Íý¤·¤Þ¤¹¡£
334 @section suite ¥¹¡¼¥Ä
338 How do you set up your tests so that you can run them all at once?
340 °ìÅ٤˥ƥ¹¥È¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë½àÈ÷¤¹¤ë¤Ë¤Ï¡¢
341 ¤É¤Î¤è¤¦¤Ë¤·¤¿¤é¤¤¤¤¤Ç¤·¤ç¤¦¤«¡©
345 CCUnit provides a @link CCUnitTestSuite TestSuite @endlink
346 module that runs any number of TestCases together.
348 CCUnit ¤Ï¤¤¤¯¤Ä¤â¤Î @link CCUnitTestCase TestCases @endlink
350 @link CCUnitTestSuite TestSuite @endlink ¥â¥¸¥å¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
354 You saw, above, how to run test fixture.
356 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤ò¼Â¹Ô¤¹¤ëÊýË¡¤Ï¾å½Ò¤·¤Þ¤·¤¿¡£
360 To create a suite of two or more tests, you do the following:
362 Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤ò´Þ¤à°ì¤Ä¤Î¥¹¡¼¥Ä¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
366 CCUnitTestSuite* suite;
367 CCUnitTestFixture* fixture;
368 CCUnitTestResult* result;
369 suite = ccunit_newTestSuite ("Complex test suite");
370 fixture = ccunit_newTestFixture ("Complex Tests",
371 CCUNIT_NEWTESTFUNC(setUp_complex_test),
372 CCUNIT_NEWTESTFUNC(tearDown_complex_test));
373 ccunit_addNewTestCase (fixture, "test_complex_equals", "complex equals test",
374 test_complex_equals);
375 ccunit_addNewTestCase (fixture, "test_complex_add", "complex add test",
377 ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
379 ccunit_addTestFixture (suite, fixtuer);
380 result = ccunit_runTestSuite (suite, NULL);
384 @link CCUnitTestSuite TestSuites @endlink don't only have to
385 contain @link CCUnitTestFixture TestFixtures @endlink. They
386 can contain any object that implements the @link CCUnitTest
387 Test @endlink interface. For example, you can create a
388 @link CCUnitTestSuite TestSuite @endlink
389 (<code>complex_add_sub_suite ()</code>) in your code and I
390 can create one in mine
391 (<code>complex_mul_div_suite ()</code>), and we can run them together by
392 creating a @link CCUnitTestSuite TestSuite @endlink that
395 @link CCUnitTestSuite TestSuites @endlink ¤Ï
396 @link CCUnitTestFixture TestFixtures @endlink
397 ¤ò´Þ¤à¤À¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
398 @link CCUnitTestSuite TestSuites @endlink ¼«¿È¤ò´Þ¤à¤³¤È¤â¤Ç¤¤Þ¤¹¡£
400 ¤½¤ì¤é¤Ï @link CCUnitTest Test @endlink
401 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤¹¤ë¤É¤ó¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤â´Þ¤á¤é¤ì¤Þ¤¹¡£
403 Î㤨¤Ð¡¢¤¢¤Ê¤¿¤Ï¤¢¤Ê¤¿¤Î¥³¡¼¥É¤Ë @link CCUnitTestSuite TestSuite @endlink
404 (<code>complex_add_sub_suite ()</code>)
407 (<code>complex_mul_div_suite ()</code>)
408 ¤òºî¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡¢
409 ¤½¤·¤Æ»äã¤ÏξÊý¤È¤â¤ò´Þ¤ó¤Ç¤¤¤ë @link CCUnitTestSuite TestSuite @endlink
410 ¤òºî¤Ã¤Æ°ì½ï¤Ë¤½¤ì¤¾¤ì¤Î¥¹¡¼¥Ä¤òÆ°¤«¤¹¤³¤È¤¬¤Ç¤¤ë¤Î¤Ç¤¹¡£
414 CCUnitTestSuite* suite;
415 CCUnitTestResult* result;
416 suite = ccunit_newTestSuite ("Complex add/sub/mul/div test suite");
417 ccunit_addTestSuite (suite, complex_add_sub_suite ());
418 ccunit_addTestSuite (suite, complex_mul_div_suite ());
419 result = ccunit_runTestSuite(suite, NULL);
424 @section test_runner TestRunner
426 @section test_runner ¥Æ¥¹¥È¥é¥ó¥Ê¡¼
430 How do you run your tests and collect their results?
432 ¤É¤¦¤ä¤Ã¤Æ¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢¤½¤Î·ë²Ì¤ò½¸¤á¤¿¤éÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
436 Once you have a test suite, you'll want to run it. %CCUnit
437 provides tools to define the suite to be run and to display
438 its results. You make your suite accessible to a @link
439 CreatingTestSuite ccunit_makeSuite @endlink tool that generate a
440 creating test suite code.
442 °ì¤Ä¥Æ¥¹¥È¥¹¡¼¥Ä¤ò½ñ¤¤¤¿¤é¡¢
443 ¤½¤ì¤ò¼Â¹Ô¤·¤¿¤¤¤Ç¤·¤ç¤¦¡£
444 CCUnit ¤Ï¥¹¡¼¥Ä¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÄêµÁ¤·¡¢
445 ·ë²Ì¤òɽ¼¨¤¹¤ë¤¿¤á¤Î¥Ä¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
446 ¥¹¡¼¥Ä¤ò @link CreatingTestSuite ccunit_makeSuite @endlink
447 ¥Ä¡¼¥ë¤ËÆþÎϤǤ¤ë¤è¤¦¤Ê·Á¼°¤Ç½ñ¤¯¤³¤È¤Ç¡¢
448 ¥Æ¥¹¥È¥¹¡¼¥Ä¤òºîÀ®¤¹¤ë¥³¡¼¥É¤ò¼«Æ°Åª¤ËÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
452 For example, to make a ComplexTest suite available to a
453 @link CreatingTestSuite ccunit_makeSuite @endlink,
454 excute the following tool to
457 Î㤨¤Ð¡¢ComplexTest ¥¹¡¼¥Ä¤ò
458 @link CreatingTestSuite ccunit_makeSuite @endlink
459 ¤ò»È¤Ã¤Æ»ÈÍѤǤ¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
460 °Ê²¼¤Î¥Ä¡¼¥ë¤ò testComplex.c ¤Ë¼Â¹Ô¤·¤Þ¤¹¡£
464 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
467 @anchor test_runner_code
470 <code>complex_suite</code> ¤È¤¤¤¦´Ø¿ô¤¬ÄêµÁ¤µ¤ì¤¿¡¢
471 <code>suiteComplex.c</code> ¤È¤¤¤¦¥Õ¥¡¥¤¥ë¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£
475 To use the TestRunner, include the header files for the tests in runTest.c:
477 TestRunner ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢
478 Î㤨¤Ð runTest.c ¤Ç¥Æ¥¹¥È¤Î¤¿¤á¤Î¥Õ¥¡¥¤¥ë¤Î¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Þ¤¹¡£
482 #include <ccunit/CCUnitTestRunner.h>
483 #include <ccunit/CCUnitTestSuite.h>
488 @link ccunit_runTestRunner()
489 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
490 in the <code>main()</code> function:
492 ¤½¤·¤Æ@link ccunit_runTestRunner()
493 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
494 ¤ò<code>main()</code>´Ø¿ô¤Ç¼Â¹Ô¤·¤Þ¤¹¡£
498 extern CCUnitTestSuite* complex_suite(const char* name);
500 int main( int argc, char **argv)
502 CCUnitTestRunner* runner;
503 CCUnitTestSuite* suite;
504 runner = ccunit_newTestRunner (stdout);
505 suite = complex_suite ("complex test suite");
506 return ccunit_runTestRunner (runner, suite);
511 The @link ExecutingTest TestRunner @endlink will run the tests.
512 If all the tests pass, you'll get an informative message.
513 If any fail, you'll get the following information:
515 @link ExecutingTest TestRunner @endlink ¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¤Þ¤¹¡£
516 ¤â¤·¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¤¬¥Ñ¥¹¤¹¤ì¤Ð¡¢¤½¤Î¾ðÊó¤Î¥á¥Ã¥»¡¼¥¸¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
517 ¤â¤·¤É¤ì¤«¤¬¼ºÇÔ¤¹¤ì¤Ð¡¢¤½¤ì¤Ë¤Ä¤¤¤Æ°Ê²¼¤Î¤è¤¦¤Ê¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
522 The name of the source file that contains the test
524 ¥Æ¥¹¥È¤ò´Þ¤ó¤Ç¤¤¤ë¥½¡¼¥¹¥Õ¥¡¥¤¥ë̾
528 The line number where the failure occurred
534 The name of the test case that failed
536 ¼ºÇÔ¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹¤Î̾Á°
540 All of the text inside the call to
541 <code>CCUNIT_ASSERT ()</code> which detected the failure
543 ¼ºÇÔ¤ò¸¡ÃΤ·¤¿<code>CCUNIT_ASSERT ()</code>¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Î¾ò·ï¤Îʸ»úÎó¡£
549 @section helper_macros Helper Tool
551 @section helper_macros ¥Ø¥ë¥Ñ¡¼¥Ä¡¼¥ë
555 As you might have noticed, implementing the <code>suite
556 ()</code> function of fixture is a repetitive and error
557 prone task. A @ref CreatingTestSuite set of functions and
558 command have been created to automatically implements the
559 <code>suite()</code> function.
561 ¤ªµ¤¤Å¤¤Î¤è¤¦¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î <code>suite ()</code>´Ø¿ô¤ò¼ÂÁõ¤¹¤ë¤Î¤Ï¡¢
562 È¿ÉüŪ¤Ç´Ö°ã¤¤¤ä¤¹¤¤ºî¶È¤Ç¤¹¡£
563 @ref CreatingTestSuite ¤Î´Ø¿ô¤Î¥»¥Ã¥È¤È¥³¥Þ¥ó¥É¤Ï<code>suite ()</code>
564 ´Ø¿ô¤Î¼ÂÁõ¤ò¼«Æ°Åª¤ËºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
567 @dontinclude complex/testComplex.c
570 The following code is a rewrite of ComplexTest using those command:
572 °Ê²¼¤Î¥³¡¼¥É¤Ï¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤¬»È¤¦¤è¤¦¤ËComplexTest¤ò½ñ´¹¤¨¤¿¤â¤Î¤Ç¤¹¡£
576 #include <ccunit/CCUnitAssert.h>
580 First, you declare the fixture, passing the test fixture
581 name to the javaDoc style comment, which consist of a
582 C-style comment block starting with two <tt>*</tt>'s:
584 ºÇ½é¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤òÀë¸À¤·¤Þ¤¹¡£
585 ¤³¤ì¤ÏjavaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥ÈÆâ¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤Î̾Á°¤òµ½Ò¤·¤Þ¤¹¡£
586 javaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¤È¤Ï C ¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î³«»Ï¤¬
587 Æó¤Ä¤Î¥¢¥¹¥¿¥ê¥¹¥¯ <tt>**</tt> ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
591 //** test case: complex number test *\/
595 The function to make @link CCUnitTestSuite TestSuite
596 @endlink is <code>ccunit_suite</code>.
597 But, it can be changed to another
598 name by the <code>-f</code> option of the
599 <code>ccunit_makeSuite</code> command, too.
600 Then, you define each test case of the fixture with prefix
601 <code>test</code>, <code>setUp</code>,
602 <code>tearDown</code>:
605 @link CCUnitTestSuite TestSuite @endlink
606 ¤òºîÀ®¤¹¤ë´Ø¿ô¤Ï <code>ccunit_suite</code> ¤Ç¤¹¡¢
607 ¤·¤«¤· <code>ccunit_makeSuite</code> ¥³¥Þ¥ó¥É¤Î
608 <code>-f</code> ¥ª¥×¥·¥ç¥ó¤ÇÊ̤Î̾Á°¤ËÊѤ¨¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
609 ¤½¤·¤Æ¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ë¤Ï¤½¤ì¤¾¤ì̾Á°¤ÎÀèƬ¤Ë¡¢
610 <code>test</code>, <code>setUp</code>,
611 <code>tearDown</code> ¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
617 static complex_t* s10_1;
618 static complex_t* s1_1;
619 static complex_t* s11_2;
621 void setUp_complex_test ()
623 s10_1 = complex_new (10, 1);
624 s1_1 = complex_new (1, 1);
625 s11_2 = complex_new (11, 2);
628 void tearDown_complex_test ()
630 complex_delete (s10_1);
631 complex_delete (s1_1);
632 complex_delete (s11_2);
636 void test_complex_equals ()
638 CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
639 CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
643 void test_complex_add ()
645 complex_t c10_1 = { 10.0, 1.0 };
646 complex_t c1_1 = { 1.0, 1.0 };
648 complex_t c11_2 = { 11.0, 2.0 };
649 CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
653 void test_complex_sub ()
655 complex_t c9_0 = { 9, 0 };
657 CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
658 complex_sub (&result, s10_1, s1_1),
664 Finally, you end the fixture declaration:
666 ºÇ¸å¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î½ª¤ê¤òÀë¸À¤·¤Þ¤¹¡£
670 //** end test case *\/
674 To generate creating suite function code, run
675 <code>ccunit_makeSuite</code> tool.
677 ¥¹¡¼¥ÄºîÀ®´Ø¿ô¤Î¥³¡¼¥É¤òÀ¸À®¤¹¤ë¤Ë¤Ï¡¢
678 <code>ccunit_makeSuite</code>¥Ä¡¼¥ë¤ò¼Â¹Ô¤·¤Þ¤¹¡£
682 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
685 #include <ccunit/CCUnitTestSuite.h>
686 #include <ccunit/CCUnitTestFixture.h>
687 #include <ccunit/CCUnitTestCase.h>
689 //* test fixture: complex number test *\/
690 //* setUp_complex_test *\/
691 extern void setUp_complex_test ();
692 //* tearDown_complex_test *\/
693 extern void tearDown_complex_test ();
694 //* test_complex_equals *\/
695 extern void test_complex_equals ();
696 //* test_complex_add *\/
697 extern void test_complex_add ();
698 //* test_complex_sub *\/
699 extern void test_complex_sub ();
701 static CCUnitTestFunc fx_001_cases[] = {
703 "test_complex_equals",
722 static CCUnitTestFixtureDfn fx_001 = {
723 { ccunitTypeFixture },
724 "complex number test",
726 "setUp_complex_test",
727 "setUp_complex_test",
731 "tearDown_complex_test",
732 "tearDown_complex_test",
733 tearDown_complex_test
738 static CCUnitTestDfn* suite_001_test[] = {
743 static CCUnitTestSuiteDfn suite_001 = {
750 CCUnitTestSuite* complex_suite (const char* name)
752 if (!suite_001.name[0])
753 suite_001.name = name;
754 return ccunit_newTestSuiteFromDfn (&suite_001);
760 @section post_build_check Post-build check
762 @section post_build_check ¥Ó¥ë¥É¸å¤Î¥Á¥§¥Ã¥¯
766 Now that we have our unit tests running, how about
767 integrating unit testing to our build process ?
769 ¤µ¤¢¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë½àÈ÷¤¬¤Ç¤¤Þ¤·¤¿¡£
770 ¤Ç¤Ï¥Ó¥ë¥É¥×¥í¥»¥¹¤Ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤òÅý¹ç¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤é¤¤
775 To do that, the application must returns a value different than 0 to indicate that
778 ¤½¤¦¤¹¤ë¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤³¤È¤ò¼¨¤¹
779 £°°Ê³°¤ÎÃͤòÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
783 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink returns
784 a integer indicating if the run was successful.
786 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink
787 ¤Ï¡¢¼Â¹Ô¤¬À®¸ù¤·¤¿¤«¤É¤¦¤«¤ò¼¨¤¹À°¿ô¤òÊÖ¤·¤Þ¤¹¡£
791 Updating our main programm, we obtains:
793 ¼¡¤Î¤è¤¦¤Ë¥á¥¤¥ó¥×¥í¥°¥é¥à¤ò¹¹¿·¤·¤Þ¤¹¡£
797 #include <ccunit/CCUnitTestRunner.h>
799 int main (int argc, char** argv)
801 CCUnitTestRunner* runner;
802 CCUnitTestSuite* suite;
804 runner = ccunit_newTestRunner (stdout);
805 suite = ccunit_suite ();
806 wasSucessful = ccunit_runTestRunner (runner, suite);
812 Now, you need to run your application after compilation.
813 The sample program made in the above is in the examples/complex directory.
815 ¤½¤ì¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤¿¸å¤Ë¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
816 °Ê¾å¤ÇºîÀ®¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£