OSDN Git Service

insert figures of test case and fixture
[ccunit/ccunit.git] / doc / cookbook.dox
1 /* -*- Indented-Text -*- */
2 /* Copyright (C) 2003, 2010 TSUTSUMI Kikuo.
3    This file is part of the CCUnit Library.
4
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.
9
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.
14
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/>
18 */
19 /* $Id$ */
20 /**
21 @english
22 @page cookbook CCUnit Cookbook
23 @japanese
24 @page cookbook CCUnit ¥¯¥Ã¥¯¥Ö¥Ã¥¯
25 @endif
26
27 @~english
28 Here is a short cookbook to help you get started.
29 @~japanese
30 ¤³¤ì¤Ï CCUnit ¤ò»È¤¤»Ï¤á¤ë¤Ë¤¢¤¿¤Ã¤Æ¡¢
31 Íý²ò¤Î½õ¤±¤È¤Ê¤ë¤è¤¦¤Êû¤¤¥¯¥Ã¥¯¥Ö¥Ã¥¯¤Ç¤¹¡£
32 @~
33 @english
34 [see also <a href="../ja/cookbook.html">Japanese</a> documents]
35 @japanese
36 [see also <a href="../../cookbook.html">English</a> documents]
37 @endif
38
39 @english
40 @section simple_test_case Simple Test Case
41 @japanese
42 @section simple_test_case ¥·¥ó¥×¥ë¤Ê¥Æ¥¹¥È¥±¡¼¥¹
43 @endif
44
45 @~english
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
49 your code.
50 @~japanese
51 CCUnit¤ò»È¤Ã¤¿¥Æ¥¹¥È¤Ï¼«Æ°Åª¤Ë¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
52 CCUnit ¤Î¥Æ¥¹¥È¤Ï´Êñ¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¤³¤È¤¬¤Ç¤­¡¢
53 °ìÅ٥ƥ¹¥È¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
54 ¤¤¤Ä¤Ç¤â¥×¥í¥°¥é¥à¤ÎÉʼÁ¤ò¿®Íê¤Ç¤­¤ë¤â¤Î¤ËÊݤĤ³¤È¤¬¤Ç¤­¤ë¤Ç¤·¤ç¤¦¡£
55 @~
56
57 @~english
58 To make a simple test, here is what you do:
59 @~japanese
60 ñ½ã¤Ê¥Æ¥¹¥È¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
61 @~
62
63 <ol>
64 <li> @~english Create a test function
65      @~japanese ¥Æ¥¹¥È´Ø¿ô¤òºî¤ë
66      @~
67 </li>
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      ¤â¤·¥Æ¥¹¥È¤¬À®¸ù¤¹¤ë¤Ê¤é¿¿¤òÊÖ¤¹¤è¤¦¤Ê¿¿µ¶ÃͤòÅϤ·¤Þ¤¹@~
74
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      ¤Ë¥ê¥¹¥È¤·¤Æ¤¢¤ê¤Þ¤¹¡£
80      @~
81 </li>
82 </ol>
83
84 @~english
85 For example, to test that the sum of two complex number
86 which is the sum of the values of two complex numbers,
87 write:
88 @~japanese
89 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤Î¹ç·×¤¬¡¢
90 Æó¤Ä¤ÎÊ£ÁÇ¿ô¤ÎÃͤò²Ã»»¤·¤¿ÃͤÈƱ¤¸¤Ç¤¢¤ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤È¤·¤Þ¤¹¡£
91 ¡Ê¤³¤³¤Ç»ÈÍѤ¹¤ë¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï
92 examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£¡Ë
93 @~
94
95 @~english
96 This sample program is in the examples/complex directory.
97 @~
98
99 @code
100 void test_complex_add ()
101 {
102   complex_t c10_1 = { 10.0, 1.0 };
103   complex_t c1_1 = { 1.0, 1.0 };
104   complex_t result;
105   complex_t c11_2 = { 11.0, 2.0 };
106   CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
107 }
108 @endcode
109
110 @~english
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.
114 @~japanese
115 ¤³¤ì¤ÏÂçÊÑñ½ã¤Ê¥Æ¥¹¥È¤Ç¤¹¡£
116 Ä̾Ʊ¤¸¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ÇÁö¤é¤»¤ë¤¿¤á¤Ë¡¢
117 ¤¿¤¯¤µ¤ó¤Î¾®¤µ¤Ê¥Æ¥¹¥È¥±¡¼¥¹¤òºî¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤Ç¤·¤ç¤¦¡£
118 ¤½¤³¤Ç¥Õ¥£¥¯¥¹¥Á¥ã¡ÊÈ÷Éʡˤò»È¤¤¤Þ¤¹¡£
119 @~
120
121 @english
122 @section fixture Fixture
123 @japanese
124 @section fixture ¥Õ¥£¥¯¥¹¥Á¥ã
125 @endif
126
127 @~english
128 What if you have two or more tests that operate on the same
129 similar set of objects?
130 @~japanese
131 ¤â¤·Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤¬¤¢¤ë¤Ê¤é¡¢
132 Ʊ¤¸Îà»÷¤Î¥Ç¡¼¥¿¤Î½¸¤Þ¤ê¤ÇÁàºî¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¤«¡£
133 @~
134
135 @dot
136 digraph TestCase {
137   node [ fontsize=9, fontname=Helvetica ]
138   edge [ fontsize=9, dir=back, arrowtail=vee ]
139   gv [ label="Global variables", shape=box ]
140   { rank=same;
141     tc1 [ label="Test case 1" ];
142     tcn [ label="Test case n" ];
143   }
144   gv -> tc1;
145   gv -> tcn;
146   tc1 -> tcn [ style=dotted, arrowtail=none ];
147 }
148 @enddot
149
150 @~english
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.
156 @~japanese
157 ¥Æ¥¹¥È¤Ï¼þÃΤΥǡ¼¥¿¤Î½¸¤Þ¤ê¤òÇطʤˤ·¤Æ¼Â¹Ô¤µ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
158 ¤³¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡£
159 ¥Æ¥¹¥È¤ò½ñ¤¤¤Æ¤¤¤ë¤È¡¢
160 ¼ÂºÝ¤Î¥Æ¥¹¥È¤¹¤ëÃͤò¥Õ¥£¥¯¥¹¥Á¥ã¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¥³¡¼¥É¤ò½ñ¤¯Êý¤Ë¡¢
161 ¤è¤ê»þ´Ö¤ò¤«¤±¤Æ¤¤¤ë¤³¤È¤Ëµ¤¤Å¤¯¤³¤È¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£
162 @~
163
164 @~english
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
168 different results.
169 @~japanese
170 ¿¤¯¤Î¾ì¹ç¡¢¤¤¤¯¤Ä¤«¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¤Î¤¿¤á¤ËƱ¤¸¥Õ¥£¥¯¥¹¥Á¥ã¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
171 ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ï¾¯¤·°Û¤Ê¤Ã¤¿¥á¥Ã¥»¡¼¥¸¡¢
172 ¤¢¤ë¤¤¤Ï¥Ñ¥é¥á¡¼¥¿¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤ËÁ÷¤ê¡¢
173 °Û¤Ê¤ë·ë²Ì¤òÄ´¤Ù¤ë¤³¤È¤Ë¤Ê¤ë¤Ç¤·¤ç¤¦¡£
174 @~
175
176 @~english
177 When you have a common fixture, here is what you do:
178 @~japanese
179 ¤â¤·¶¦Ä̤¹¤ë¥Õ¥£¥¯¥¹¥Á¥ã¤¬¤¢¤ì¤Ð¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤¹¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
180 @~
181
182 <ol>
183 <li>@~english
184     Create a @link CCUnitTestCase TestCase @endlink object
185     @~japanese
186     @link CCUnitTestCase TestCase @endlink ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
187     ¡Ê°Ê¹ß¡¢¹½Â¤ÂΤ˳ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò¡¢¥ª¥Ö¥¸¥§¥¯¥È¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡Ë
188     @~
189 </li>
190 <li>@~english
191     Add an global variable for each part of the fixture
192     @~japanese
193     ¥Õ¥£¥¯¥¹¥Á¥ã¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ËÂç°èÊÑ¿ô¤òÄɲä·¤Þ¤¹
194     @~
195 </li>
196 <li>@~english
197     Write <code>setUp()</code> function to initialize the valiables
198     @~japanese
199     <code>setUp()</code> ´Ø¿ô¤ò½ñ¤¤¤ÆÊÑ¿ô¤ò½é´ü²½¤·¤Þ¤¹
200     @~
201 </li>
202 <li>@~english
203     Write <code>tearDown()</code> to release any permanent
204     resources you allocated in <code>setUp</code>
205     @~japanese
206     <code>tearDown()</code> ´Ø¿ô¤ò½ñ¤¤¤Æ<code>setUp</code>
207     ¤Ç³ä¤êÅö¤Æ¤¿±Ê³Ū¥ê¥½¡¼¥¹¤ò²òÊü¤·¤Þ¤¹
208     @~
209 </li>
210 <li>@~english
211     Create a @link CCUnitTestFixture TestFixture @endlink object
212     @~japanese
213     @link CCUnitTestFixture TestFixture @endlink
214     ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
215     @~
216 </li>
217 <li>@~english
218     Add @link CCUnitTestCase TestCase @endlink objects to fixture object
219     @~japanese
220     @link CCUnitTestCase TestCase @endlink ¥ª¥Ö¥¸¥§¥¯¥È¤ò
221     @link CCUnitTestFixture TestFixture @endlink
222     ¥Õ¥£¥¯¥¹¥Á¥ã¥ª¥Ö¥¸¥§¥¯¥È¤ËÅÐÏ¿¤·¤Þ¤¹¡£
223     @~
224 </li>
225 </ol>
226
227 @dot
228 digraph TestFixture {
229   node [ fontsize=9, fontname=Helvetica ]
230   edge [ fontsize=9, dir=back, arrowtail=vee ]
231   gv [ label="Global variables", shape=box ]
232   { rank=same;
233     su [ label="setUp ()" ];
234     tc [ label="TestCase" ];
235     td [ label="tearDown ()" ];
236   }
237   gv -> su [ label="initialize" ];
238   gv -> tc [ label="access" ];
239   gv -> td [ label="clean-up" ];
240 }
241 @enddot
242
243 @~english
244 For example, to write several test cases, first create a
245 fixture:
246 @~japanese
247 Î㤨¤Ð¡¢¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¾ì¹ç¡¢
248 ºÇ½é¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Þ¤¹¡£
249 @~
250
251 @code
252 //** TEST CASE: complex number test *\/
253
254 #include "complex.h"
255
256 static complex_t* s10_1;
257 static complex_t* s1_1;
258 static complex_t* s11_2;
259
260 void setUp_complex_test ()
261 {
262   s10_1 = complex_new (10, 1);
263   s1_1 = complex_new (1, 1);
264   s11_2 = complex_new (11, 2);
265 }
266
267 void tearDown_complex_test ()
268 {
269   complex_delete (s10_1);
270   complex_delete (s1_1);
271   complex_delete (s11_2);
272 }
273
274 ...
275
276   CCUnitTestFixture* fixture;
277   fixture = ccunit_newTestFixture ("complex test",
278                                    CCUNIT_NEWTESTFUNC(setUp_complex_test),
279                                    CCUNIT_NEWTESTFUNC(tearDown_complex_test));
280 @endcode
281
282 @~english
283 Once you have the Fixture in place, you can write as complex
284 Test Cases as you'd like.
285 @~japanese
286 °ìÅÙ·è¤Þ¤Ã¤¿¤È¤³¤í¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
287 ¤¢¤Ê¤¿¤¬¹¥¤­¤Ê¤è¤¦¤ËÊ£ÁÇ¿ô¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
288 @~
289
290 @english
291 @section test_case Test Case
292 @japanese
293 @section test_case ¥Æ¥¹¥È¥±¡¼¥¹
294 @endif
295
296 @~english
297 How do you write and invoke an individual test case when you
298 have a Fixture?
299 @~japanese
300 ¥Õ¥£¥¯¥¹¥Á¥ã¤ò°ì¤Ä½ñ¤¤¤¿¤È¤·¤Æ¡¢
301 ¤É¤¦¤ä¤Ã¤Æ¸Ä¡¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¤¤Æ¼Â¹Ô¤¹¤ì¤ÐÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
302 @~
303
304 @~english
305 For example, to test the equality of two complex number,
306 write:
307 @~japanese
308 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤¬Åù¤·¤¤¡Ê¤Þ¤¿¤ÏÅù¤·¤¯¤Ê¤¤¡Ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤Ë¤Ï¡¢
309 ¼¡¤Î¤è¤¦¤Ë½ñ¤­¤Þ¤¹¡£
310 @~
311
312 @code
313 void test_complex_equals ()
314 {
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);
317 }
318
319 ...
320
321   ccunit_addNewTestCase (fixture,
322                          "test_complex_equals",
323                          "complex equals test",
324                          test_complex_equals);
325 @endcode
326
327 @~english
328 One may create and run objects for each test case like this:
329 @~japanese
330 °ì¤Ä¤Ë¤Ï¼¡¤Î¤è¤¦¤Ë¡¢
331 ¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Æ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò¼Â¹Ô¤µ¤»¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
332 @~
333
334 @code
335   CCUnitTestResult* result;
336   result = ccunit_runTestFixture (fixture);
337 @endcode
338
339 @~english
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
344 display the results.
345 @~japanese
346 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤¬¼Â¹Ô¤µ¤ì¤ë¤È¡¢
347 ÆÃÄê¤Î¥Æ¥¹¥È´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
348 ¤³¤ì¤Ï¤¢¤Þ¤êÊØÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡¢
349 ¤Ê¤¼¤Ê¤é¡¢¿ÇÃǤ¬É½¼¨¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¹¡£
350 Ä̾ï¤Ï @link ExecutingTest TestRunner @endlink
351 (@ref test_runner ¸å½Ò)
352 ¤Ç·ë²Ì¤òɽ¼¨¤·¤Þ¤¹¡£
353 @~
354
355 @~english
356 Once you have several tests, organize them into a suite.
357 @~japanese
358 °ìÅÙ¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¤òºî¤Ã¤¿¤é¡¢
359 ¤½¤ì¤é¤ò¥¹¡¼¥Ä¤ËÀ°Íý¤·¤Þ¤¹¡£
360 @~
361
362 @english
363 @section suite Suite
364 @japanese
365 @section suite ¥¹¡¼¥Ä
366 @endif
367
368 @~english
369 How do you set up your tests so that you can run them all at once?
370 @~japanese
371 °ìÅ٤˥ƥ¹¥È¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¤è¤¦¤Ë½àÈ÷¤¹¤ë¤Ë¤Ï¡¢
372 ¤É¤Î¤è¤¦¤Ë¤·¤¿¤é¤¤¤¤¤Ç¤·¤ç¤¦¤«¡©
373 @~
374
375 @~english
376 CCUnit provides a @link CCUnitTestSuite TestSuite @endlink
377 module that runs any number of TestCases together.
378 @~japanese
379 CCUnit ¤Ï¤¤¤¯¤Ä¤â¤Î @link CCUnitTestCase TestCases @endlink
380 ¤ò°ì½ï¤Ë¼Â¹Ô¤¹¤ë
381 @link CCUnitTestSuite TestSuite @endlink ¥â¥¸¥å¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
382 @~
383
384 @~english
385 You saw, above, how to run test fixture.
386 @~japanese
387 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤ò¼Â¹Ô¤¹¤ëÊýË¡¤Ï¾å½Ò¤·¤Þ¤·¤¿¡£
388 @~
389
390 @~english
391 To create a suite of two or more tests, you do the following:
392 @~japanese
393 Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤ò´Þ¤à°ì¤Ä¤Î¥¹¡¼¥Ä¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
394 @~
395
396 @code
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",
407                        test_complex_add);
408 ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
409                        test_complex_sub);
410 ccunit_addTestFixture (suite, fixtuer);
411 result = ccunit_runTestSuite (suite, NULL);
412 @endcode
413
414 @~english
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
424 contains both:
425 @~japanese
426 @link CCUnitTestSuite TestSuites @endlink ¤Ï
427 @link CCUnitTestFixture TestFixtures @endlink
428 ¤ò´Þ¤à¤À¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
429 @link CCUnitTestSuite TestSuites @endlink ¼«¿È¤ò´Þ¤à¤³¤È¤â¤Ç¤­¤Þ¤¹¡£
430 <!--
431 ¤½¤ì¤é¤Ï @link CCUnitTest Test @endlink
432 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤¹¤ë¤É¤ó¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤â´Þ¤á¤é¤ì¤Þ¤¹¡£
433 -->
434 Î㤨¤Ð¡¢¤¢¤Ê¤¿¤Ï¤¢¤Ê¤¿¤Î¥³¡¼¥É¤Ë @link CCUnitTestSuite TestSuite @endlink
435 (<code>complex_add_sub_suite ()</code>)
436 ¤òºî¤ë¤³¤È¤¬¤Ç¤­¡¢
437 ¤½¤·¤Æ»ä¤Ï»ä¤Î¥¹¡¼¥Ä
438 (<code>complex_mul_div_suite ()</code>)
439 ¤òºî¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡¢
440 ¤½¤·¤Æ»äã¤ÏξÊý¤È¤â¤ò´Þ¤ó¤Ç¤¤¤ë @link CCUnitTestSuite TestSuite @endlink
441 ¤òºî¤Ã¤Æ°ì½ï¤Ë¤½¤ì¤¾¤ì¤Î¥¹¡¼¥Ä¤òÆ°¤«¤¹¤³¤È¤¬¤Ç¤­¤ë¤Î¤Ç¤¹¡£
442 @~
443
444 @code
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);
451 @endcode
452
453
454 @english
455 @section test_runner TestRunner
456 @japanese
457 @section test_runner ¥Æ¥¹¥È¥é¥ó¥Ê¡¼
458 @endif
459
460 @~english
461 How do you run your tests and collect their results?
462 @~japanese
463 ¤É¤¦¤ä¤Ã¤Æ¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢¤½¤Î·ë²Ì¤ò½¸¤á¤¿¤éÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
464 @~
465
466 @~english
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.
472 @~japanese
473 °ì¤Ä¥Æ¥¹¥È¥¹¡¼¥Ä¤ò½ñ¤¤¤¿¤é¡¢
474 ¤½¤ì¤ò¼Â¹Ô¤·¤¿¤¤¤Ç¤·¤ç¤¦¡£
475 CCUnit ¤Ï¥¹¡¼¥Ä¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÄêµÁ¤·¡¢
476 ·ë²Ì¤òɽ¼¨¤¹¤ë¤¿¤á¤Î¥Ä¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
477 ¥¹¡¼¥Ä¤ò @link CreatingTestSuite ccunit_makeSuite @endlink
478 ¥Ä¡¼¥ë¤ËÆþÎϤǤ­¤ë¤è¤¦¤Ê·Á¼°¤Ç½ñ¤¯¤³¤È¤Ç¡¢
479 ¥Æ¥¹¥È¥¹¡¼¥Ä¤òºîÀ®¤¹¤ë¥³¡¼¥É¤ò¼«Æ°Åª¤ËÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
480 @~
481
482 @~english
483 For example, to make a ComplexTest suite available to a
484 @link CreatingTestSuite ccunit_makeSuite @endlink,
485 excute the following tool to
486 testComplex.c:
487 @~japanese
488 Î㤨¤Ð¡¢ComplexTest ¥¹¡¼¥Ä¤ò
489 @link CreatingTestSuite ccunit_makeSuite @endlink
490 ¤ò»È¤Ã¤Æ»ÈÍѤǤ­¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
491 °Ê²¼¤Î¥Ä¡¼¥ë¤ò testComplex.c ¤Ë¼Â¹Ô¤·¤Þ¤¹¡£
492 @~
493
494 @code
495 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
496 @endcode
497
498 @anchor test_runner_code
499 @~english
500 @~japanese
501 <code>complex_suite</code> ¤È¤¤¤¦´Ø¿ô¤¬ÄêµÁ¤µ¤ì¤¿¡¢
502 <code>suiteComplex.c</code> ¤È¤¤¤¦¥Õ¥¡¥¤¥ë¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£
503 @~
504
505 @~english
506 To use the TestRunner, include the header files for the tests in runTest.c:
507 @~japanese
508 TestRunner ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢
509 Î㤨¤Ð runTest.c ¤Ç¥Æ¥¹¥È¤Î¤¿¤á¤Î¥Õ¥¡¥¤¥ë¤Î¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Þ¤¹¡£
510 @~
511
512 @code
513 #include <ccunit/CCUnitTestRunner.h>
514 #include <ccunit/CCUnitTestSuite.h>
515 @endcode
516
517 @~english
518 And call to
519 @link ccunit_runTestRunner()
520 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
521 in the <code>main()</code> function:
522 @~japanese
523 ¤½¤·¤Æ@link ccunit_runTestRunner()
524 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
525 ¤ò<code>main()</code>´Ø¿ô¤Ç¼Â¹Ô¤·¤Þ¤¹¡£
526 @~
527
528 @code
529 extern CCUnitTestSuite* complex_suite(const char* name);
530
531 int main( int argc, char **argv)
532 {
533   CCUnitTestRunner* runner;
534   CCUnitTestSuite* suite;
535   runner = ccunit_newTestRunner (stdout);
536   suite = complex_suite ("complex test suite");
537   return ccunit_runTestRunner (runner, suite);
538 }
539 @endcode
540
541 @~english
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:
545 @~japanese
546 @link ExecutingTest TestRunner @endlink ¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¤Þ¤¹¡£
547 ¤â¤·¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¤¬¥Ñ¥¹¤¹¤ì¤Ð¡¢¤½¤Î¾ðÊó¤Î¥á¥Ã¥»¡¼¥¸¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
548 ¤â¤·¤É¤ì¤«¤¬¼ºÇÔ¤¹¤ì¤Ð¡¢¤½¤ì¤Ë¤Ä¤¤¤Æ°Ê²¼¤Î¤è¤¦¤Ê¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
549 @~
550
551 <ul>
552 <li>@~english
553     The name of the source file that contains the test
554     @~japanese
555     ¥Æ¥¹¥È¤ò´Þ¤ó¤Ç¤¤¤ë¥½¡¼¥¹¥Õ¥¡¥¤¥ë̾
556     @~
557 </li>
558 <li>@~english
559     The line number where the failure occurred
560     @~japanese
561     ¼ºÇÔ¤¬µ¯¤³¤Ã¤¿¹ÔÈÖ¹æ
562     @~
563 </li>
564 <li>@~english
565     The name of the test case that failed
566     @~japanese
567     ¼ºÇÔ¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹¤Î̾Á°
568     @~
569 </li>
570 <li>@~english
571     All of the text inside the call to
572     <code>CCUNIT_ASSERT ()</code> which detected the failure
573     @~japanese
574     ¼ºÇÔ¤ò¸¡ÃΤ·¤¿<code>CCUNIT_ASSERT ()</code>¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Î¾ò·ï¤Îʸ»úÎó¡£
575     @~
576 </li>
577 </ul>
578
579 @english
580 @section helper_macros Helper Tool
581 @japanese
582 @section helper_macros ¥Ø¥ë¥Ñ¡¼¥Ä¡¼¥ë
583 @endif
584
585 @~english
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.
591 @~japanese
592 ¤ªµ¤¤Å¤­¤Î¤è¤¦¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î <code>suite ()</code>´Ø¿ô¤ò¼ÂÁõ¤¹¤ë¤Î¤Ï¡¢
593 È¿ÉüŪ¤Ç´Ö°ã¤¤¤ä¤¹¤¤ºî¶È¤Ç¤¹¡£
594 @ref CreatingTestSuite ¤Î´Ø¿ô¤Î¥»¥Ã¥È¤È¥³¥Þ¥ó¥É¤Ï<code>suite ()</code>
595 ´Ø¿ô¤Î¼ÂÁõ¤ò¼«Æ°Åª¤ËºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
596 @~
597
598 @dontinclude complex/testComplex.c
599
600 @~english
601 The following code is a rewrite of ComplexTest using those command:
602 @~japanese
603 °Ê²¼¤Î¥³¡¼¥É¤Ï¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤¬»È¤¦¤è¤¦¤ËComplexTest¤ò½ñ´¹¤¨¤¿¤â¤Î¤Ç¤¹¡£
604 @~
605
606 @code
607 #include <ccunit/CCUnitAssert.h>
608 @endcode
609
610 @~english
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:
614 @~japanese
615 ºÇ½é¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤òÀë¸À¤·¤Þ¤¹¡£
616 ¤³¤ì¤ÏjavaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥ÈÆâ¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤Î̾Á°¤òµ­½Ò¤·¤Þ¤¹¡£
617 javaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¤È¤Ï C ¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î³«»Ï¤¬
618 Æó¤Ä¤Î¥¢¥¹¥¿¥ê¥¹¥¯ <tt>**</tt> ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
619 @~
620
621 @code
622 //** test case: complex number test *\/
623 @endcode
624
625 @~english
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>:
634
635 @~japanese
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> ¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
643 @~
644
645 @code
646 #include <complex.h>
647
648 static complex_t* s10_1;
649 static complex_t* s1_1;
650 static complex_t* s11_2;
651
652 void setUp_complex_test ()
653 {
654   s10_1 = complex_new (10, 1);
655   s1_1 = complex_new (1, 1);
656   s11_2 = complex_new (11, 2);
657 }
658
659 void tearDown_complex_test ()
660 {
661   complex_delete (s10_1);
662   complex_delete (s1_1);
663   complex_delete (s11_2);
664 }
665
666 //** test equals *\/
667 void test_complex_equals ()
668 {
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);
671 }
672
673 //** test add *\/
674 void test_complex_add ()
675 {
676   complex_t c10_1 = { 10.0, 1.0 };
677   complex_t c1_1 = { 1.0, 1.0 };
678   complex_t result;
679   complex_t c11_2 = { 11.0, 2.0 };
680   CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
681 }
682
683 //** test sub *\/
684 void test_complex_sub ()
685 {
686   complex_t c9_0 = { 9, 0 };
687   complex_t result;
688   CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
689                           complex_sub (&result, s10_1, s1_1),
690                           complex_to_string);
691 }
692 @endcode
693
694 @~english
695 Finally, you end the fixture declaration:
696 @~japanese
697 ºÇ¸å¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î½ª¤ê¤òÀë¸À¤·¤Þ¤¹¡£
698 @~
699
700 @code
701 //** end test case *\/
702 @endcode
703
704 @~english
705 To generate creating suite function code, run
706 <code>ccunit_makeSuite</code> tool.
707 @~japanese
708 ¥¹¡¼¥ÄºîÀ®´Ø¿ô¤Î¥³¡¼¥É¤òÀ¸À®¤¹¤ë¤Ë¤Ï¡¢
709 <code>ccunit_makeSuite</code>¥Ä¡¼¥ë¤ò¼Â¹Ô¤·¤Þ¤¹¡£
710 @~
711
712 @code
713 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
714 $ cat suiteComplex.c
715
716 #include <ccunit/CCUnitTestSuite.h>
717 #include <ccunit/CCUnitTestFixture.h>
718 #include <ccunit/CCUnitTestCase.h>
719
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 ();
731
732 static CCUnitTestFunc fx_001_cases[] = {
733   {
734     "test_complex_equals",
735     "test equals",
736     test_complex_equals
737   },
738   {
739     "test_complex_add",
740     "test add",
741     test_complex_add
742   },
743   {
744     "test_complex_sub",
745     "test sub",
746     test_complex_sub
747   },
748   {
749     NULL, NULL, NULL
750   },
751 };
752
753 static CCUnitTestFixtureDfn fx_001 = {
754   { ccunitTypeFixture },
755   "complex number test",
756   {
757     "setUp_complex_test",
758     "setUp_complex_test",
759     setUp_complex_test
760   },
761   {
762     "tearDown_complex_test",
763     "tearDown_complex_test",
764     tearDown_complex_test
765   },
766   fx_001_cases,
767 };
768
769 static CCUnitTestDfn* suite_001_test[] = {
770     &fx_001.test,
771     NULL,
772 };
773
774 static CCUnitTestSuiteDfn suite_001 = {
775   { ccunitTypeSuite },
776   "",
777   suite_001_test
778 };
779
780
781 CCUnitTestSuite* complex_suite (const char* name)
782 {
783   if (!suite_001.name[0])
784     suite_001.name = name;
785   return ccunit_newTestSuiteFromDfn (&suite_001);
786 }
787 $
788 @endcode
789
790 @english
791 @section post_build_check Post-build check
792 @japanese
793 @section post_build_check ¥Ó¥ë¥É¸å¤Î¥Á¥§¥Ã¥¯
794 @endif
795
796 @~english
797 Now that we have our unit tests running, how about
798 integrating unit testing to our build process ?
799 @~japanese
800 ¤µ¤¢¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë½àÈ÷¤¬¤Ç¤­¤Þ¤·¤¿¡£
801 ¤Ç¤Ï¥Ó¥ë¥É¥×¥í¥»¥¹¤Ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤òÅý¹ç¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤é¤¤
802 ¤¤¤Ç¤·¤ç¤¦¡£
803 @~
804
805 @~english
806 To do that, the application must returns a value different than 0 to indicate that
807 there was an error.
808 @~japanese
809 ¤½¤¦¤¹¤ë¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤³¤È¤ò¼¨¤¹
810 £°°Ê³°¤ÎÃͤòÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
811 @~
812
813 @~english
814 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink returns
815 a integer indicating if the run was successful.
816 @~japanese
817 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink
818 ¤Ï¡¢¼Â¹Ô¤¬À®¸ù¤·¤¿¤«¤É¤¦¤«¤ò¼¨¤¹À°¿ô¤òÊÖ¤·¤Þ¤¹¡£
819 @~
820
821 @~english
822 Updating our main programm, we obtains:
823 @~japanese
824 ¼¡¤Î¤è¤¦¤Ë¥á¥¤¥ó¥×¥í¥°¥é¥à¤ò¹¹¿·¤·¤Þ¤¹¡£
825 @~
826
827 @code
828 #include <ccunit/CCUnitTestRunner.h>
829
830 int main (int argc, char** argv)
831 {
832   CCUnitTestRunner* runner;
833   CCUnitTestSuite* suite;
834   int wasSucessful;
835   runner = ccunit_newTestRunner (stdout);
836   suite = ccunit_suite ();
837   wasSucessful = ccunit_runTestRunner (runner, suite);
838   return wasSucessful;
839 }
840 @endcode
841
842 @~english
843 Now, you need to run your application after compilation.
844 The sample program made in the above is in the examples/complex directory.
845 @~japanese
846 ¤½¤ì¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤¿¸å¤Ë¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
847 °Ê¾å¤ÇºîÀ®¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£
848 @~
849
850 */