OSDN Git Service

reexamined format command and add details
[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 @~english
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.
141 @~japanese
142 ¥Æ¥¹¥È¤Ï¼þÃΤΥǡ¼¥¿¤Î¥»¥Ã¥È¤òÇطʤˤ·¤Æ¼Â¹Ô¤µ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
143 ¤³¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡£
144 ¥Æ¥¹¥È¤ò½ñ¤¤¤Æ¤¤¤ë¤È¡¢
145 ¼ÂºÝ¤Î¥Æ¥¹¥È¤¹¤ëÃͤò¥Õ¥£¥¯¥¹¥Á¥ã¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¥³¡¼¥É¤ò½ñ¤¯Êý¤Ë¡¢
146 ¤è¤ê»þ´Ö¤ò¤«¤±¤Æ¤¤¤ë¤³¤È¤Ëµ¤¤Å¤¯¤³¤È¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£
147 @~
148
149 @~english
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
153 different results.
154 @~japanese
155 ¿¤¯¤Î¾ì¹ç¡¢¤¤¤¯¤Ä¤«¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¤Î¤¿¤á¤ËƱ¤¸¥Õ¥£¥¯¥¹¥Á¥ã¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
156 ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ï¾¯¤·°Û¤Ê¤Ã¤¿¥á¥Ã¥»¡¼¥¸¡¢
157 ¤¢¤ë¤¤¤Ï¥Ñ¥é¥á¡¼¥¿¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤ËÁ÷¤ê¡¢
158 °Û¤Ê¤ë·ë²Ì¤òÄ´¤Ù¤Þ¤¹¡£
159 @~
160
161 @~english
162 When you have a common fixture, here is what you do:
163 @~japanese
164 ¤â¤·¶¦Ä̤¹¤ë¥Õ¥£¥¯¥¹¥Á¥ã¤¬¤¢¤ì¤Ð¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤¹¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
165 @~
166
167 <ol>
168 <li>@~english
169     Create a @link CCUnitTestCase TestCase @endlink object
170     @~japanese
171     @link CCUnitTestCase TestCase @endlink ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
172     ¡Ê°Ê¹ß¡¢¹½Â¤ÂΤ˳ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò¡¢¥ª¥Ö¥¸¥§¥¯¥È¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡Ë
173     @~
174 </li>
175 <li>@~english
176     Add an global variable for each part of the fixture
177     @~japanese
178     ¥Õ¥£¥¯¥¹¥Á¥ã¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ËÂç°èÊÑ¿ô¤òÄɲä·¤Þ¤¹
179     @~
180 </li>
181 <li>@~english
182     Write <code>setUp()</code> function to initialize the valiables
183     @~japanese
184     <code>setUp()</code> ´Ø¿ô¤ò½ñ¤¤¤ÆÊÑ¿ô¤ò½é´ü²½¤·¤Þ¤¹
185     @~
186 </li>
187 <li>@~english
188     Write <code>tearDown()</code> to release any permanent
189     resources you allocated in <code>setUp</code>
190     @~japanese
191     <code>tearDown()</code> ´Ø¿ô¤ò½ñ¤¤¤Æ<code>setUp</code>
192     ¤Ç³ä¤êÅö¤Æ¤¿±Ê³Ū¥ê¥½¡¼¥¹¤ò²òÊü¤·¤Þ¤¹
193     @~
194 </li>
195 <li>@~english
196     Create a @link CCUnitTestFixture TestFixture @endlink object
197     @~japanese
198     @link CCUnitTestFixture CCUnitTestFixture @endlink
199     ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
200     @~
201 </li>
202 <li>@~english
203     Add @link CCUnitTestCase TestCase @endlink objects to fixture object
204     @~japanese
205     @link CCUnitTestCase TestCase @endlink ¥ª¥Ö¥¸¥§¥¯¥È¤ò
206     @link CCUnitTestFixture TestFixture @endlink
207     ¥Õ¥£¥¯¥¹¥Á¥ã¥ª¥Ö¥¸¥§¥¯¥È¤ËÅÐÏ¿¤·¤Þ¤¹¡£
208     @~
209 </li>
210 </ol>
211
212 @~english
213 For example, to write several test cases, first create a
214 fixture:
215 @~japanese
216 Î㤨¤Ð¡¢¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¾ì¹ç¡¢
217 ºÇ½é¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Þ¤¹¡£
218 @~
219
220 @code
221 //** TEST CASE: complex number test *\/
222
223 #include "complex.h"
224
225 static complex_t* s10_1;
226 static complex_t* s1_1;
227 static complex_t* s11_2;
228
229 void setUp_complex_test ()
230 {
231   s10_1 = complex_new (10, 1);
232   s1_1 = complex_new (1, 1);
233   s11_2 = complex_new (11, 2);
234 }
235
236 void tearDown_complex_test ()
237 {
238   complex_delete (s10_1);
239   complex_delete (s1_1);
240   complex_delete (s11_2);
241 }
242
243 ...
244
245   CCUnitTestFixture* fixture;
246   fixture = ccunit_newTestFixture ("complex test",
247                                    CCUNIT_NEWTESTFUNC(setUp_complex_test),
248                                    CCUNIT_NEWTESTFUNC(tearDown_complex_test));
249 @endcode
250
251 @~english
252 Once you have the Fixture in place, you can write as complex
253 Test Cases as you'd like.
254 @~japanese
255 °ìÅÙ·è¤Þ¤Ã¤¿¤È¤³¤í¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
256 ¤¢¤Ê¤¿¤¬¹¥¤­¤Ê¤è¤¦¤ËÊ£ÁÇ¿ô¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
257 @~
258
259 @english
260 @section test_case Test Case
261 @japanese
262 @section test_case ¥Æ¥¹¥È¥±¡¼¥¹
263 @endif
264
265 @~english
266 How do you write and invoke an individual test case when you
267 have a Fixture?
268 @~japanese
269 ¥Õ¥£¥¯¥¹¥Á¥ã¤ò°ì¤Ä½ñ¤¤¤¿¤È¤·¤Æ¡¢
270 ¤É¤¦¤ä¤Ã¤Æ¸Ä¡¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¤¤Æ¼Â¹Ô¤¹¤ì¤ÐÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
271 @~
272
273 @~english
274 For example, to test the equality of two complex number,
275 write:
276 @~japanese
277 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤¬Åù¤·¤¤¡Ê¤Þ¤¿¤ÏÅù¤·¤¯¤Ê¤¤¡Ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤Ë¤Ï¡¢
278 ¼¡¤Î¤è¤¦¤Ë½ñ¤­¤Þ¤¹¡£
279 @~
280
281 @code
282 void test_complex_equals ()
283 {
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);
286 }
287
288 ...
289
290   ccunit_addNewTestCase (fixture,
291                          "test_complex_equals",
292                          "complex equals test",
293                          test_complex_equals);
294 @endcode
295
296 @~english
297 One may create and run objects for each test case like this:
298 @~japanese
299 °ì¤Ä¤Ë¤Ï¼¡¤Î¤è¤¦¤Ë¡¢
300 ¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Æ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò¼Â¹Ô¤µ¤»¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
301 @~
302
303 @code
304   CCUnitTestResult* result;
305   result = ccunit_runTestFixture (fixture);
306 @endcode
307
308 @~english
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
313 display the results.
314 @~japanese
315 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤¬¼Â¹Ô¤µ¤ì¤ë¤È¡¢
316 ÆÃÄê¤Î¥Æ¥¹¥È´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
317 ¤³¤ì¤Ï¤¢¤Þ¤êÊØÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡¢
318 ¤Ê¤¼¤Ê¤é¡¢¿ÇÃǤ¬É½¼¨¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¹¡£
319 Ä̾ï¤Ï @link ExecutingTest TestRunner @endlink
320 (@ref test_runner ¸å½Ò)
321 ¤Ç·ë²Ì¤òɽ¼¨¤·¤Þ¤¹¡£
322 @~
323
324 @~english
325 Once you have several tests, organize them into a suite.
326 @~japanese
327 °ìÅÙ¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¤òºî¤Ã¤¿¤é¡¢
328 ¤½¤ì¤é¤ò¥¹¡¼¥Ä¤ËÀ°Íý¤·¤Þ¤¹¡£
329 @~
330
331 @english
332 @section suite Suite
333 @japanese
334 @section suite ¥¹¡¼¥Ä
335 @endif
336
337 @~english
338 How do you set up your tests so that you can run them all at once?
339 @~japanese
340 °ìÅ٤˥ƥ¹¥È¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¤è¤¦¤Ë½àÈ÷¤¹¤ë¤Ë¤Ï¡¢
341 ¤É¤Î¤è¤¦¤Ë¤·¤¿¤é¤¤¤¤¤Ç¤·¤ç¤¦¤«¡©
342 @~
343
344 @~english
345 CCUnit provides a @link CCUnitTestSuite TestSuite @endlink
346 module that runs any number of TestCases together.
347 @~japanese
348 CCUnit ¤Ï¤¤¤¯¤Ä¤â¤Î @link CCUnitTestCase TestCases @endlink
349 ¤ò°ì½ï¤Ë¼Â¹Ô¤¹¤ë
350 @link CCUnitTestSuite TestSuite @endlink ¥â¥¸¥å¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
351 @~
352
353 @~english
354 You saw, above, how to run test fixture.
355 @~japanese
356 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤ò¼Â¹Ô¤¹¤ëÊýË¡¤Ï¾å½Ò¤·¤Þ¤·¤¿¡£
357 @~
358
359 @~english
360 To create a suite of two or more tests, you do the following:
361 @~japanese
362 Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤ò´Þ¤à°ì¤Ä¤Î¥¹¡¼¥Ä¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
363 @~
364
365 @code
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",
376                        test_complex_add);
377 ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
378                        test_complex_sub);
379 ccunit_addTestFixture (suite, fixtuer);
380 result = ccunit_runTestSuite (suite, NULL);
381 @endcode
382
383 @~english
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
393 contains both:
394 @~japanese
395 @link CCUnitTestSuite TestSuites @endlink ¤Ï
396 @link CCUnitTestFixture TestFixtures @endlink
397 ¤ò´Þ¤à¤À¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
398 @link CCUnitTestSuite TestSuites @endlink ¼«¿È¤ò´Þ¤à¤³¤È¤â¤Ç¤­¤Þ¤¹¡£
399 <!--
400 ¤½¤ì¤é¤Ï @link CCUnitTest Test @endlink
401 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤¹¤ë¤É¤ó¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤â´Þ¤á¤é¤ì¤Þ¤¹¡£
402 -->
403 Î㤨¤Ð¡¢¤¢¤Ê¤¿¤Ï¤¢¤Ê¤¿¤Î¥³¡¼¥É¤Ë @link CCUnitTestSuite TestSuite @endlink
404 (<code>complex_add_sub_suite ()</code>)
405 ¤òºî¤ë¤³¤È¤¬¤Ç¤­¡¢
406 ¤½¤·¤Æ»ä¤Ï»ä¤Î¥¹¡¼¥Ä
407 (<code>complex_mul_div_suite ()</code>)
408 ¤òºî¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡¢
409 ¤½¤·¤Æ»äã¤ÏξÊý¤È¤â¤ò´Þ¤ó¤Ç¤¤¤ë @link CCUnitTestSuite TestSuite @endlink
410 ¤òºî¤Ã¤Æ°ì½ï¤Ë¤½¤ì¤¾¤ì¤Î¥¹¡¼¥Ä¤òÆ°¤«¤¹¤³¤È¤¬¤Ç¤­¤ë¤Î¤Ç¤¹¡£
411 @~
412
413 @code
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);
420 @endcode
421
422
423 @english
424 @section test_runner TestRunner
425 @japanese
426 @section test_runner ¥Æ¥¹¥È¥é¥ó¥Ê¡¼
427 @endif
428
429 @~english
430 How do you run your tests and collect their results?
431 @~japanese
432 ¤É¤¦¤ä¤Ã¤Æ¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢¤½¤Î·ë²Ì¤ò½¸¤á¤¿¤éÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
433 @~
434
435 @~english
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.
441 @~japanese
442 °ì¤Ä¥Æ¥¹¥È¥¹¡¼¥Ä¤ò½ñ¤¤¤¿¤é¡¢
443 ¤½¤ì¤ò¼Â¹Ô¤·¤¿¤¤¤Ç¤·¤ç¤¦¡£
444 CCUnit ¤Ï¥¹¡¼¥Ä¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÄêµÁ¤·¡¢
445 ·ë²Ì¤òɽ¼¨¤¹¤ë¤¿¤á¤Î¥Ä¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
446 ¥¹¡¼¥Ä¤ò @link CreatingTestSuite ccunit_makeSuite @endlink
447 ¥Ä¡¼¥ë¤ËÆþÎϤǤ­¤ë¤è¤¦¤Ê·Á¼°¤Ç½ñ¤¯¤³¤È¤Ç¡¢
448 ¥Æ¥¹¥È¥¹¡¼¥Ä¤òºîÀ®¤¹¤ë¥³¡¼¥É¤ò¼«Æ°Åª¤ËÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
449 @~
450
451 @~english
452 For example, to make a ComplexTest suite available to a
453 @link CreatingTestSuite ccunit_makeSuite @endlink,
454 excute the following tool to
455 testComplex.c:
456 @~japanese
457 Î㤨¤Ð¡¢ComplexTest ¥¹¡¼¥Ä¤ò
458 @link CreatingTestSuite ccunit_makeSuite @endlink
459 ¤ò»È¤Ã¤Æ»ÈÍѤǤ­¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
460 °Ê²¼¤Î¥Ä¡¼¥ë¤ò testComplex.c ¤Ë¼Â¹Ô¤·¤Þ¤¹¡£
461 @~
462
463 @code
464 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
465 @endcode
466
467 @anchor test_runner_code
468 @~english
469 @~japanese
470 <code>complex_suite</code> ¤È¤¤¤¦´Ø¿ô¤¬ÄêµÁ¤µ¤ì¤¿¡¢
471 <code>suiteComplex.c</code> ¤È¤¤¤¦¥Õ¥¡¥¤¥ë¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£
472 @~
473
474 @~english
475 To use the TestRunner, include the header files for the tests in runTest.c:
476 @~japanese
477 TestRunner ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢
478 Î㤨¤Ð runTest.c ¤Ç¥Æ¥¹¥È¤Î¤¿¤á¤Î¥Õ¥¡¥¤¥ë¤Î¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Þ¤¹¡£
479 @~
480
481 @code
482 #include <ccunit/CCUnitTestRunner.h>
483 #include <ccunit/CCUnitTestSuite.h>
484 @endcode
485
486 @~english
487 And call to
488 @link ccunit_runTestRunner()
489 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
490 in the <code>main()</code> function:
491 @~japanese
492 ¤½¤·¤Æ@link ccunit_runTestRunner()
493 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
494 ¤ò<code>main()</code>´Ø¿ô¤Ç¼Â¹Ô¤·¤Þ¤¹¡£
495 @~
496
497 @code
498 extern CCUnitTestSuite* complex_suite(const char* name);
499
500 int main( int argc, char **argv)
501 {
502   CCUnitTestRunner* runner;
503   CCUnitTestSuite* suite;
504   runner = ccunit_newTestRunner (stdout);
505   suite = complex_suite ("complex test suite");
506   return ccunit_runTestRunner (runner, suite);
507 }
508 @endcode
509
510 @~english
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:
514 @~japanese
515 @link ExecutingTest TestRunner @endlink ¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¤Þ¤¹¡£
516 ¤â¤·¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¤¬¥Ñ¥¹¤¹¤ì¤Ð¡¢¤½¤Î¾ðÊó¤Î¥á¥Ã¥»¡¼¥¸¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
517 ¤â¤·¤É¤ì¤«¤¬¼ºÇÔ¤¹¤ì¤Ð¡¢¤½¤ì¤Ë¤Ä¤¤¤Æ°Ê²¼¤Î¤è¤¦¤Ê¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
518 @~
519
520 <ul>
521 <li>@~english
522     The name of the source file that contains the test
523     @~japanese
524     ¥Æ¥¹¥È¤ò´Þ¤ó¤Ç¤¤¤ë¥½¡¼¥¹¥Õ¥¡¥¤¥ë̾
525     @~
526 </li>
527 <li>@~english
528     The line number where the failure occurred
529     @~japanese
530     ¼ºÇÔ¤¬µ¯¤³¤Ã¤¿¹ÔÈÖ¹æ
531     @~
532 </li>
533 <li>@~english
534     The name of the test case that failed
535     @~japanese
536     ¼ºÇÔ¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹¤Î̾Á°
537     @~
538 </li>
539 <li>@~english
540     All of the text inside the call to
541     <code>CCUNIT_ASSERT ()</code> which detected the failure
542     @~japanese
543     ¼ºÇÔ¤ò¸¡ÃΤ·¤¿<code>CCUNIT_ASSERT ()</code>¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Î¾ò·ï¤Îʸ»úÎó¡£
544     @~
545 </li>
546 </ul>
547
548 @english
549 @section helper_macros Helper Tool
550 @japanese
551 @section helper_macros ¥Ø¥ë¥Ñ¡¼¥Ä¡¼¥ë
552 @endif
553
554 @~english
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.
560 @~japanese
561 ¤ªµ¤¤Å¤­¤Î¤è¤¦¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î <code>suite ()</code>´Ø¿ô¤ò¼ÂÁõ¤¹¤ë¤Î¤Ï¡¢
562 È¿ÉüŪ¤Ç´Ö°ã¤¤¤ä¤¹¤¤ºî¶È¤Ç¤¹¡£
563 @ref CreatingTestSuite ¤Î´Ø¿ô¤Î¥»¥Ã¥È¤È¥³¥Þ¥ó¥É¤Ï<code>suite ()</code>
564 ´Ø¿ô¤Î¼ÂÁõ¤ò¼«Æ°Åª¤ËºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
565 @~
566
567 @dontinclude complex/testComplex.c
568
569 @~english
570 The following code is a rewrite of ComplexTest using those command:
571 @~japanese
572 °Ê²¼¤Î¥³¡¼¥É¤Ï¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤¬»È¤¦¤è¤¦¤ËComplexTest¤ò½ñ´¹¤¨¤¿¤â¤Î¤Ç¤¹¡£
573 @~
574
575 @code
576 #include <ccunit/CCUnitAssert.h>
577 @endcode
578
579 @~english
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:
583 @~japanese
584 ºÇ½é¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤òÀë¸À¤·¤Þ¤¹¡£
585 ¤³¤ì¤ÏjavaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥ÈÆâ¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤Î̾Á°¤òµ­½Ò¤·¤Þ¤¹¡£
586 javaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¤È¤Ï C ¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î³«»Ï¤¬
587 Æó¤Ä¤Î¥¢¥¹¥¿¥ê¥¹¥¯ <tt>**</tt> ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
588 @~
589
590 @code
591 //** test case: complex number test *\/
592 @endcode
593
594 @~english
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>:
603
604 @~japanese
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> ¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
612 @~
613
614 @code
615 #include <complex.h>
616
617 static complex_t* s10_1;
618 static complex_t* s1_1;
619 static complex_t* s11_2;
620
621 void setUp_complex_test ()
622 {
623   s10_1 = complex_new (10, 1);
624   s1_1 = complex_new (1, 1);
625   s11_2 = complex_new (11, 2);
626 }
627
628 void tearDown_complex_test ()
629 {
630   complex_delete (s10_1);
631   complex_delete (s1_1);
632   complex_delete (s11_2);
633 }
634
635 //** test equals *\/
636 void test_complex_equals ()
637 {
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);
640 }
641
642 //** test add *\/
643 void test_complex_add ()
644 {
645   complex_t c10_1 = { 10.0, 1.0 };
646   complex_t c1_1 = { 1.0, 1.0 };
647   complex_t result;
648   complex_t c11_2 = { 11.0, 2.0 };
649   CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
650 }
651
652 //** test sub *\/
653 void test_complex_sub ()
654 {
655   complex_t c9_0 = { 9, 0 };
656   complex_t result;
657   CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
658                           complex_sub (&result, s10_1, s1_1),
659                           complex_to_string);
660 }
661 @endcode
662
663 @~english
664 Finally, you end the fixture declaration:
665 @~japanese
666 ºÇ¸å¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î½ª¤ê¤òÀë¸À¤·¤Þ¤¹¡£
667 @~
668
669 @code
670 //** end test case *\/
671 @endcode
672
673 @~english
674 To generate creating suite function code, run
675 <code>ccunit_makeSuite</code> tool.
676 @~japanese
677 ¥¹¡¼¥ÄºîÀ®´Ø¿ô¤Î¥³¡¼¥É¤òÀ¸À®¤¹¤ë¤Ë¤Ï¡¢
678 <code>ccunit_makeSuite</code>¥Ä¡¼¥ë¤ò¼Â¹Ô¤·¤Þ¤¹¡£
679 @~
680
681 @code
682 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
683 $ cat suiteComplex.c
684
685 #include <ccunit/CCUnitTestSuite.h>
686 #include <ccunit/CCUnitTestFixture.h>
687 #include <ccunit/CCUnitTestCase.h>
688
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 ();
700
701 static CCUnitTestFunc fx_001_cases[] = {
702   {
703     "test_complex_equals",
704     "test equals",
705     test_complex_equals
706   },
707   {
708     "test_complex_add",
709     "test add",
710     test_complex_add
711   },
712   {
713     "test_complex_sub",
714     "test sub",
715     test_complex_sub
716   },
717   {
718     NULL, NULL, NULL
719   },
720 };
721
722 static CCUnitTestFixtureDfn fx_001 = {
723   { ccunitTypeFixture },
724   "complex number test",
725   {
726     "setUp_complex_test",
727     "setUp_complex_test",
728     setUp_complex_test
729   },
730   {
731     "tearDown_complex_test",
732     "tearDown_complex_test",
733     tearDown_complex_test
734   },
735   fx_001_cases,
736 };
737
738 static CCUnitTestDfn* suite_001_test[] = {
739     &fx_001.test,
740     NULL,
741 };
742
743 static CCUnitTestSuiteDfn suite_001 = {
744   { ccunitTypeSuite },
745   "",
746   suite_001_test
747 };
748
749
750 CCUnitTestSuite* complex_suite (const char* name)
751 {
752   if (!suite_001.name[0])
753     suite_001.name = name;
754   return ccunit_newTestSuiteFromDfn (&suite_001);
755 }
756 $
757 @endcode
758
759 @english
760 @section post_build_check Post-build check
761 @japanese
762 @section post_build_check ¥Ó¥ë¥É¸å¤Î¥Á¥§¥Ã¥¯
763 @endif
764
765 @~english
766 Now that we have our unit tests running, how about
767 integrating unit testing to our build process ?
768 @~japanese
769 ¤µ¤¢¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë½àÈ÷¤¬¤Ç¤­¤Þ¤·¤¿¡£
770 ¤Ç¤Ï¥Ó¥ë¥É¥×¥í¥»¥¹¤Ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤òÅý¹ç¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤é¤¤
771 ¤¤¤Ç¤·¤ç¤¦¡£
772 @~
773
774 @~english
775 To do that, the application must returns a value different than 0 to indicate that
776 there was an error.
777 @~japanese
778 ¤½¤¦¤¹¤ë¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤³¤È¤ò¼¨¤¹
779 £°°Ê³°¤ÎÃͤòÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
780 @~
781
782 @~english
783 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink returns
784 a integer indicating if the run was successful.
785 @~japanese
786 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink
787 ¤Ï¡¢¼Â¹Ô¤¬À®¸ù¤·¤¿¤«¤É¤¦¤«¤ò¼¨¤¹À°¿ô¤òÊÖ¤·¤Þ¤¹¡£
788 @~
789
790 @~english
791 Updating our main programm, we obtains:
792 @~japanese
793 ¼¡¤Î¤è¤¦¤Ë¥á¥¤¥ó¥×¥í¥°¥é¥à¤ò¹¹¿·¤·¤Þ¤¹¡£
794 @~
795
796 @code
797 #include <ccunit/CCUnitTestRunner.h>
798
799 int main (int argc, char** argv)
800 {
801   CCUnitTestRunner* runner;
802   CCUnitTestSuite* suite;
803   int wasSucessful;
804   runner = ccunit_newTestRunner (stdout);
805   suite = ccunit_suite ();
806   wasSucessful = ccunit_runTestRunner (runner, suite);
807   return wasSucessful;
808 }
809 @endcode
810
811 @~english
812 Now, you need to run your application after compilation.
813 The sample program made in the above is in the examples/complex directory.
814 @~japanese
815 ¤½¤ì¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤¿¸å¤Ë¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
816 °Ê¾å¤ÇºîÀ®¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï examples/complex ¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£
817 @~
818
819 */