OSDN Git Service

# fixed typo
[ccunit/ccunit.git] / doc / cookbook.dox
1 /* -*- Indented-Text -*- */
2 /* $Id$ */
3 /**
4 @english
5 @page cookbook CCUnit Cookbook
6 @japanese
7 @page cookbook CCUnit ¥¯¥Ã¥¯¥Ö¥Ã¥¯
8 @endif
9
10 @~english
11 Here is a short cookbook to help you get started.
12 @~japanese
13 ¤³¤ì¤Ï CCUnit ¤ò»È¤¤»Ï¤á¤ë¤Ë¤¢¤¿¤Ã¤Æ¡¢
14 Íý²ò¤Î½õ¤±¤È¤Ê¤ë¤è¤¦¤Êû¤¤¥¯¥Ã¥¯¥Ö¥Ã¥¯¤Ç¤¹¡£
15 @~
16 @english
17 [see also <a href="../ja/cookbook.html">Japanese</a> documents]
18 @japanese
19 [see also <a href="../../cookbook.html">English</a> documents]
20 @endif
21
22 @english
23 @section simple_test_case Simple Test Case
24 @japanese
25 @section simple_test_case ¥·¥ó¥×¥ë¤Ê¥Æ¥¹¥È¥±¡¼¥¹
26 @endif
27
28 @~english
29 Tests in <b>CCUnit</b> can be run automatically.  They are
30 easy to set up and once you have written them, they are
31 always there to help you keep confidence in the quality of
32 your code.
33 @~japanese
34 CCUnit¤ò»È¤Ã¤¿¥Æ¥¹¥È¤Ï¼«Æ°Åª¤Ë¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
35 CCUnit ¤Î¥Æ¥¹¥È¤Ï´Êñ¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¤³¤È¤¬¤Ç¤­¡¢
36 °ìÅ٥ƥ¹¥È¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
37 ¤¤¤Ä¤Ç¤â¥×¥í¥°¥é¥à¤ÎÉʼÁ¤ò¿®Íê¤Ç¤­¤ë¤â¤Î¤ËÊݤĤ³¤È¤¬¤Ç¤­¤ë¤Ç¤·¤ç¤¦¡£
38 @~
39
40 @~english
41 To make a simple test, here is what you do:
42 @~japanese
43 ñ½ã¤Ê¥Æ¥¹¥È¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
44 @~
45
46 <ol>
47 <li> @~english Create a test function
48      @~japanese ¥Æ¥¹¥È´Ø¿ô¤òºî¤ë</li>
49 <li> @~english When you want to check a value, call
50      @~japanese Ãͤò¥Á¥§¥Ã¥¯¤·¤¿¤¤¾ì¹ç¤Ï¡¢@~
51      @link CCUNIT_ASSERT() CCUNIT_ASSERT(bool) @endlink
52      @~english and pass a bool that is true if the test succeeds
53      @~japanese ¤ò¸Æ¤Ó½Ð¤·¤Æ¡¢
54      ¤â¤·¥Æ¥¹¥È¤¬À®¸ù¤¹¤ë¤Î¤Ç¤¢¤ì¤Ð¿¿¤òÊÖ¤¹¤è¤¦¤Ê¿¿µ¶ÃͤòÅϤ·¤Þ¤¹</li>
55 </ol>
56
57 @~english
58 For example, to test that the sum of two complex number
59 which is the sum of the values of two complex numbers,
60 write:
61 @~japanese
62 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤Î¹ç·×¤¬¡¢
63 Æó¤Ä¤ÎÊ£ÁÇ¿ô¤ÎÃͤò²Ã»»¤·¤¿ÃͤÈƱ¤¸¤Ç¤¢¤ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤È¤·¤Þ¤¹¡£
64 @~
65
66 @code
67 void test_complex_add ()
68 {
69   complex_t c10_1 = { 10.0, 1.0 };
70   complex_t c1_1 = { 1.0, 1.0 };
71   complex_t result;
72   complex_t c11_2 = { 11.0, 2.0 };
73   CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, c10_1, c1_1)));
74 }
75 @endcode
76
77 @~english
78 That was a very simple test. Ordinarily, you'll have many
79 little test cases that you'll want to run on the same set of
80 objects. To do this, use a fixture.
81 @~japanese
82 ¤³¤ì¤ÏÂçÊÑñ½ã¤Ê¥Æ¥¹¥È¤Ç¤¹¡£
83 Ä̾Ʊ¤¸¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ÇÁö¤é¤»¤ë¤¿¤á¤Ë¡¢
84 ¤¿¤¯¤µ¤ó¤Î¾®¤µ¤Ê¥Æ¥¹¥È¥±¡¼¥¹¤òºî¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤Ç¤·¤ç¤¦¡£
85 ¤½¤¦¤¹¤ë¤Ë¤Ï¥Õ¥£¥¯¥¹¥Á¥ã¡ÊÈ÷Éʡˤò»È¤¤¤Þ¤¹¡£
86 @~
87
88 @english
89 @section fixture Fixture
90 @japanese
91 @section fixture ¥Õ¥£¥¯¥¹¥Á¥ã
92 @endif
93
94 @~english
95 What if you have two or more tests that operate on the same
96 similar set of objects?
97 @~japanese
98 ¤â¤·Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤¬¤¢¤ë¤Ê¤é¡¢
99 Ʊ¤¸Îà»÷¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ÇÁàºî¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¤«¡£
100 @~
101
102 @~english
103 Tests need to run against the background of a known set of
104 objects. This set of objects is called a test fixture. When
105 you are writing tests you will often find that you spend
106 more time writing the code to set up the fixture than you do
107 in actually testing values.
108 @~japanese
109 ¥Æ¥¹¥È¤Ï¼þÃΤΥǡ¼¥¿¤Î¥»¥Ã¥È¤òÇطʤˤ·¤Æ¼Â¹Ô¤µ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
110 ¤³¤Î¥Ç¡¼¥¿¤Î¥»¥Ã¥È¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡£
111 ¥Æ¥¹¥È¤ò½ñ¤¤¤Æ¤¤¤ë¤È¡¢
112 ¼ÂºÝ¤Î¥Æ¥¹¥È¤¹¤ëÃͤò¥Õ¥£¥¯¥¹¥Á¥ã¤Ë¥»¥Ã¥È¥¢¥Ã¥×¤¹¤ë¥³¡¼¥É¤ò½ñ¤¯Êý¤Ë¡¢
113 ¤â¤Ã¤È»þ´Ö¤ò¤«¤±¤Æ¤¤¤ë¤³¤È¤Ëµ¤¤Å¤¯¤³¤È¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£
114 @~
115
116 @~english
117 Often, you will be able to use the same fixture for several
118 different tests. Each case will send slightly different
119 messages or parameters to the fixture and will check for
120 different results.
121 @~japanese
122 ¿¤¯¤Î¾ì¹ç¡¢¤¤¤¯¤Ä¤«¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¤Î¤¿¤á¤ËƱ¤¸¥Õ¥£¥¯¥¹¥Á¥ã¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
123 ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ï¾¯¤·°Û¤Ê¤Ã¤¿¥á¥Ã¥»¡¼¥¸¡¢
124 ¤¢¤ë¤¤¤Ï¥Ñ¥é¥á¡¼¥¿¤ò¥Õ¥£¥¯¥¹¥Á¥ã¤ËÁ÷¤ê¡¢
125 °Û¤Ê¤ë·ë²Ì¤òÄ´¤Ù¤Þ¤¹¡£
126 @~
127
128 @~english
129 When you have a common fixture, here is what you do:
130 @~japanese
131 ¤â¤·¶¦Ä̤¹¤ë¥Õ¥£¥¯¥¹¥Á¥ã¤¬¤¢¤ì¤Ð¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤¹¤ë¤³¤È¤Ë¤Ê¤ê
132 ¤Þ¤¹¡£
133 @~
134
135 <ol>
136 <li>@~english
137     Create a @link CCUnitTestCase TestCase @endlink object
138     @~japanese
139     @link CCUnitTestCase TestCase @endlink ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
140     ¡Ê°Ê¹ß¡¢¹½Â¤ÂΤ˳ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò¡¢¥ª¥Ö¥¸¥§¥¯¥È¤È¸Æ¤Ö¤³¤È¤Ë¤·¤Þ¤¹¡Ë
141 </li>
142 <li>@~english
143     Add an global variable for each part of the fixture
144     @~japanese
145     ¥Õ¥£¥¯¥¹¥Á¥ã¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ËÂç°èÊÑ¿ô¤òÄɲä·¤Þ¤¹
146 </li>
147 <li>@~english
148     Write <code>setUp()</code> function to initialize the valiables
149     @~japanese
150     <code>setUp()</code> ´Ø¿ô¤ò½ñ¤¤¤ÆÊÑ¿ô¤ò½é´ü²½¤·¤Þ¤¹
151 </li>
152 <li>@~english
153     Write <code>tearDown()</code> to release any permanent
154     resources you allocated in <code>setUp</code>
155     @~japanese
156     <code>tearDown()</code> ´Ø¿ô¤ò½ñ¤¤¤Æ<code>setUp</code>
157     ¤Ç³ä¤êÅö¤Æ¤¿±Ê³Ū¥ê¥½¡¼¥¹¤ò²òÊü¤·¤Þ¤¹
158 </li>
159 <li>@~english
160     Create a @link CCUnitTestFixture TestFixture @endlink object
161     @~japanese
162     @link CCUnitTestFixture CCUnitTestFixture @endlink
163     ¹½Â¤ÂΤ˥á¥â¥ê¤ò³ä¤êÅö¤Æ¤Þ¤¹
164 </li>
165 <li>@~english
166     Add @link CCUnitTestCase TestCase @endlink objects to fixture object
167     @~japanese
168     @link CCUnitTestCase TestCase @endlink ¥ª¥Ö¥¸¥§¥¯¥È¤ò
169     @link CCUnitTestFixture TestFixture @endlink
170     ¥Õ¥£¥¯¥¹¥Á¥ã¥ª¥Ö¥¸¥§¥¯¥È¤ËÅÐÏ¿¤·¤Þ¤¹¡£
171 </li>
172 </ol>
173
174 @~english
175 For example, to write several test cases, first create a
176 fixture:
177 @~japanese
178 Î㤨¤Ð¡¢¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¾ì¹ç¡¢
179 ºÇ½é¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Þ¤¹¡£
180 @~
181
182 @code
183 //** TEST CASE: complex number test *\/
184
185 #include "complex.h"
186
187 static complex_t* s10_1;
188 static complex_t* s1_1;
189 static complex_t* s11_2;
190
191 void setUp_ComplexTest ()
192 {
193   s10_1 = complex_new (10, 1);
194   s1_1 = complex_new (1, 1);
195   s11_2 = complex_new (11, 2);
196 }
197
198 void tearDown_ComplexTest ()
199 {
200   complex_delete (s10_1);
201   complex_delete (s1_1);
202   complex_delete (s11_2);
203 }
204
205 ...
206
207   CCUnitTestFixture* fixture;
208   fixture = ccunit_newTestFixture ("ComplexTest",
209                                    CCUNIT_NEWTESTFUNC(setUp_ComplexTest),
210                                    CCUNIT_NEWTESTFUNC(tearDown_ComplexTest));
211 @endcode
212
213 @~english
214 Once you have the Fixture in place, you can write as complex
215 Test Cases as you'd like.
216 @~japanese
217 °ìÅÙ·è¤Þ¤Ã¤¿¤È¤³¤í¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤ò½ñ¤¤¤Æ¤·¤Þ¤¨¤Ð¡¢
218 ¤¢¤Ê¤¿¤¬¹¥¤­¤Ê¤è¤¦¤ËÊ£ÁÇ¿ô¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¯¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
219 @~
220
221 @english
222 @section test_case Test Case
223 @japanese
224 @section test_case ¥Æ¥¹¥È¥±¡¼¥¹
225 @endif
226
227 @~english
228 How do you write and invoke an individual test case when you
229 have a Fixture?
230 @~japanese
231 ¥Õ¥£¥¯¥¹¥Á¥ã¤ò°ì¤Ä½ñ¤¤¤¿¤È¤·¤Æ¡¢
232 ¤É¤¦¤ä¤Ã¤Æ¸Ä¡¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò½ñ¤¤¤Æ¼Â¹Ô¤¹¤ì¤ÐÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
233 @~
234
235 @~english
236 For example, to test the equality of two complex number,
237 write:
238 @~japanese
239 Î㤨¤Ð¡¢Æó¤Ä¤ÎÊ£ÁÇ¿ô¤¬Åù¤·¤¤¡Ê¤Þ¤¿¤ÏÅù¤·¤¯¤Ê¤¤¡Ë¤³¤È¤ò¥Æ¥¹¥È¤¹¤ë¤Ë¤Ï¡¢
240 ¼¡¤Î¤è¤¦¤Ë½ñ¤­¤Þ¤¹¡£
241 @~
242
243 @code
244 void test_complex_equals ()
245 {
246   CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
247   CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
248 }
249
250 ...
251
252   ccunit_addNewTestCase (fixture,
253                          "test_complex_equals",
254                          "complex equals test",
255                          test_complex_equals);
256 @endcode
257
258 @~english
259 One may create and run objects for each test case like this:
260 @~japanese
261 °ì¤Ä¤Ë¤Ï¼¡¤Î¤è¤¦¤Ë¡¢
262 ¥Õ¥£¥¯¥¹¥Á¥ã¤òºîÀ®¤·¤Æ¤½¤ì¤¾¤ì¤Î¥Æ¥¹¥È¥±¡¼¥¹¤ò¼Â¹Ô¤µ¤»¤ë¤³¤È
263 ¤¬¤Ç¤­¤Þ¤¹¡£
264 @~
265
266 @code
267   CCUnitTestResult* result;
268   result = ccunit_runTestFixture (fixture);
269 @endcode
270
271 @~english
272 When the test fixture is run, that specific test functions
273 will be run.  This is not a useful thing to do, however, as
274 no diagnostics will be displayed.  One will normally use a
275 @link ExecutingTest TestRunner @endlink (see below) to
276 display the results.
277 @~japanese
278 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤¬¼Â¹Ô¤µ¤ì¤ë¤È¡¢
279 ÆÃÄê¤Î¥Æ¥¹¥È´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
280 ¤³¤ì¤Ï¤¢¤Þ¤êÊØÍø¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡¢
281 ¤Ê¤¼¤Ê¤é¡¢¿ÇÃǤ¬É½¼¨¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¹¡£
282 Ä̾ï¤Ï @link ExecutingTest TestRunner @endlink
283 (@ref test_runner ¸å½Ò)
284 ¤Ç·ë²Ì¤òɽ¼¨¤·¤Þ¤¹¡£
285
286 @~english
287 Once you have several tests, organize them into a suite.
288 @~japanese
289 °ìÅÙ¤¤¤¯¤Ä¤«¤Î¥Æ¥¹¥È¤òºî¤Ã¤¿¤é¡¢
290 ¤½¤ì¤é¤ò¥¹¥¤¡¼¥È¤ËÀ°Íý¤·¤Þ¤¹¡£
291
292 @english
293 @section suite Suite
294 @japanese
295 @section suite ¥¹¥¤¡¼¥È
296 @endif
297
298 @~english
299 How do you set up your tests so that you can run them all at once?
300 @~japanese
301 ¤É¤Î¤è¤¦¤Ë¤·¤Æ¡¢°ìÅ٤˥ƥ¹¥È¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¤è¤¦¡¢
302 ½àÈ÷¤·¤¿¤é¤¤¤¤¤Ç¤·¤ç¤¦¤«¡©
303 @~
304
305 @~english
306 %CCUnit provides a @link CCUnitTestSuite TestSuite @endlink
307 module that runs any number of TestCases together.
308 @~japanese
309 CCUnit ¤Ï¤¤¤¯¤Ä¤â¤Î @link CCUnitTestCase TestCases @endlink
310 ¤ò°ì½ï¤Ë¼Â¹Ô¤¹¤ë
311 @link CCUnitTestSuite TestSuite @endlink ¥â¥¸¥å¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
312 @~
313
314 @~english
315 You saw, above, how to run test fixture.
316 @~japanese
317 ¥Æ¥¹¥È¥Õ¥£¥¯¥¹¥Á¥ã¤ò¼Â¹Ô¤¹¤ëÊýË¡¤Ï¾å½Ò¤·¤Þ¤·¤¿¡£
318 @~
319
320 @~english
321 To create a suite of two or more tests, you do the following:
322 @~japanese
323 Æó¤Ä°Ê¾å¤Î¥Æ¥¹¥È¤ò´Þ¤à°ì¤Ä¤Î¥¹¥¤¡¼¥È¤òºî¤ë¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£
324 @~
325
326 @code
327 CCUnitTestSuite* suite;
328 CCUnitTestFixture* fixture;
329 CCUnitTestResult* result;
330 suite = ccunit_newTestSuite ("Complex test suite");
331 fixture = ccunit_newTestFixture ("Complex Tests",
332                                  CCUNIT_NEWTESTFUNC(setUp_complex_test),
333                                  CCUNIT_NEWTESTFUNC(tearDown_complex_test));
334 ccunit_addNewTestCase (fixture, "test_complex_equals", "complex equals test",
335                        test_complex_equals);
336 ccunit_addNewTestCase (fixture, "test_complex_add", "complex add test",
337                        test_complex_add);
338 ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
339                        test_complex_sub);
340 ccunit_addTestFixture (suite, fixtuer);
341 result = ccunit_runTestSuite (suite, NULL);
342 @endcode
343
344 @~english
345 @link CCUnitTestSuite TestSuites @endlink don't only have to
346 contain @link CCUnitTestFixture TestFixtures @endlink.  They
347 can contain any object that implements the @link CCUnitTest
348 Test @endlink interface.  For example, you can create a
349 @link CCUnitTestSuite TestSuite @endlink in your code and I
350 can create one in mine, and we can run them together by
351 creating a @link CCUnitTestSuite TestSuite @endlink that
352 contains both:
353 @~japanese
354 @link CCUnitTestSuite TestSuites @endlink ¤Ï
355 @link CCUnitTestFixture TestFixtures @endlink
356 ¤ò´Þ¤à¤À¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
357 ¤½¤ì¤é¤Ï @link CCUnitTest Test @endlink
358 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤¹¤ë¤É¤ó¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤â´Þ¤á¤é¤ì¤Þ¤¹¡£
359 Î㤨¤Ð¡¢¤¢¤Ê¤¿¤Ï¤¢¤Ê¤¿¤Î¥³¡¼¥É¤Ë@link CCUnitTestSuite TestSuite @endlink
360 ¤òºî¤ë¤³¤È¤¬¤Ç¤­¡¢¤½¤·¤Æ»ä¤Ï»ä¤Î¥¹¥¤¡¼¥È¤òºî¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡¢
361 ¤½¤·¤Æ»äã¤ÏξÊý¤È¤â¤ò´Þ¤ó¤Ç¤¤¤ë @link CCUnitTestSuite TestSuite @endlink
362 ¤òºî¤Ã¤Æ°ì½ï¤Ë¸Ä¡¹¤Î¥¹¥¤¡¼¥È¤òÆ°¤«¤¹¤³¤È¤¬¤Ç¤­¤ë¤Î¤Ç¤¹¡£
363 @~
364
365 @code
366 CCUnitTestSuite* suite;
367 CCUnitTestResult* result;
368 suite = ccunit_newTestSuite ("suite");
369 ccunit_addTestSuite (suite, complex_add_sub_suite ());
370 ccunit_addTestSuite (suite, complex_mul_div_suite ());
371 result = ccunit_runTestSuite(suite, NULL);
372 @endcode
373
374
375 @english
376 @section test_runner TestRunner
377 @japanese
378 @section test_runner ¥Æ¥¹¥È¥é¥ó¥Ê¡¼
379 @endif
380
381 @~english
382 How do you run your tests and collect their results?
383 @~japanese
384 ¤É¤¦¤ä¤Ã¤Æ¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢¤½¤Î·ë²Ì¤ò½¸¤á¤¿¤éÎɤ¤¤Ç¤·¤ç¤¦¤«¡£
385 @~
386
387 @~english
388 Once you have a test suite, you'll want to run it. %CCUnit
389 provides tools to define the suite to be run and to display
390 its results.  You make your suite accessible to a @link
391 CreatingTestSuite ccunit_makeSuite @endlink tool that generate a
392 creating test suite code.
393 @~japanese
394 °ì¤Ä¥Æ¥¹¥È¥¹¥¤¡¼¥È¤ò½ñ¤¤¤¿¤é¡¢
395 ¤½¤ì¤ò¼Â¹Ô¤·¤¿¤¤¤Ç¤·¤ç¤¦¡£
396 CCUnit ¤Ï¥¹¥¤¡¼¥È¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÄêµÁ¤·¡¢
397 ·ë²Ì¤òɽ¼¨¤¹¤ë¤¿¤á¤Î¥Ä¡¼¥ë¤òÄ󶡤·¤Þ¤¹¡£
398 ¥¹¡¼¥Ä¤ò@link CreatingTestSuite ccunit_makeSuite @endlink
399 ¥Ä¡¼¥ë¤ËÆþÎϤǤ­¤ë¤è¤¦¤Ê·Á¼°¤Ç½ñ¤¯¤³¤È¤Ç¡¢
400 ¥Æ¥¹¥È¥¹¡¼¥Ä¤òºîÀ®¤¹¤ë¥³¡¼¥É¤ò¼«Æ°Åª¤ËÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
401 @~
402
403 @~english
404 For example, to make a ComplexTest suite available to a
405 @link CreatingTestSuite ccunit_makeSuite @endlink,
406 excute the following tool to
407 ComplexTest.c:
408 @~japanese
409 Î㤨¤Ð¡¢ComplexTest ¥¹¥¤¡¼¥È¤ò
410 @link CreatingTestSuite ccunit_makeSuite @endlink
411 ¤ò»È¤Ã¤Æ»ÈÍѤǤ­¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
412 °Ê²¼¤Î¥Ä¡¼¥ë¤ò ComplexTest.c ¤Ë¼Â¹Ô¤·¤Þ¤¹¡£
413 @~
414
415 @code
416 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c ComplexTest.c
417 @endcode
418
419 @anchor test_runner_code
420 @~english
421 To use the TestRunner, include the header files for the tests in Main.c:
422 @~japanese
423 TestRunner ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢
424 Main.c¤Ç¥Æ¥¹¥È¤Î¤¿¤á¤Î¥Õ¥¡¥¤¥ë¤Î¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Þ¤¹¡£
425 @~
426
427 @code
428 #include <ccunit/CCUnitTestRunner.h>
429 #include <ccunit/CCUnitTestSuite.h>
430 @endcode
431
432 @~english
433 And call to
434 @link ccunit_runTestRunner()
435 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
436 in the <code>main()</code> function:
437 @~japanese
438 ¤½¤·¤Æ@link ccunit_runTestRunner()
439 ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
440 ¤ò<code>main()</code>´Ø¿ô¤Ç¼Â¹Ô¤·¤Þ¤¹¡£
441 @~
442
443 @code
444 extern CCUnitTestSuite* complex_suite(const char* name);
445
446 int main( int argc, char **argv)
447 {
448   CCUnitTestRunner* runner;
449   CCUnitTestSuite* suite;
450   runner = ccunit_newTestRunner (stdout);
451   suite = complex_suite ("complex test suite");
452   return ccunit_runTestRunner (runner, suite);
453 }
454 @endcode
455
456 @~english
457 The @link ExecutingTest TestRunner @endlink will run the tests.
458 If all the tests pass, you'll get an informative message.
459 If any fail, you'll get the following information:
460 @~japanese
461 @link ExecutingTest TestRunner @endlink ¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¤Þ¤¹¡£
462 ¤â¤·¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¤¬¥Ñ¥¹¤¹¤ì¤Ð¡¢¤½¤Î¾ðÊó¤Î¥á¥Ã¥»¡¼¥¸¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
463 ¤â¤·¤É¤ì¤«¤¬¼ºÇÔ¤¹¤ì¤Ð¡¢¤½¤ì¤Ë¤Ä¤¤¤Æ°Ê²¼¤Î¤è¤¦¤Ê¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
464 @~
465
466 <ul>
467 <li>@~english
468     The name of the source file that contains the test
469     @~japanese
470     ¥Æ¥¹¥È¤ò´Þ¤ó¤Ç¤¤¤ë¥½¡¼¥¹¥Õ¥¡¥¤¥ë̾
471 </li>
472 <li>@~english
473     The line number where the failure occurred
474     @~japanese
475     ¼ºÇÔ¤¬µ¯¤³¤Ã¤¿¹ÔÈÖ¹æ
476 </li>
477 <li>@~english
478     The name of the test case that failed
479     @~japanese
480     ¼ºÇÔ¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹¤Î̾Á°
481 </li>
482 <li>@~english
483     All of the text inside the call to
484     <code>CCUNIT_ASSERT ()</code> which detected the failure
485     @~japanese
486     ¼ºÇÔ¤ò¸¡ÃΤ·¤¿<code>CCUNIT_ASSERT ()</code>¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Î¾ò·ï¤Îʸ»úÎó¡£
487 </li>
488 </ul>
489
490 @english
491 @section helper_macros Helper Tool
492 @japanese
493 @section helper_macros ¥Ø¥ë¥Ñ¡¼¥Ä¡¼¥ë
494 @endif
495
496 @~english
497 As you might have noticed, implementing the <code>suite
498 ()</code> function of fixture is a repetitive and error
499 prone task. A @ref CreatingTestSuite set of functions and
500 command have been created to automatically implements the
501 <code>suite()</code> function.
502 @~japanese
503 ¤ªµ¤¤Å¤­¤Î¤è¤¦¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î <code>suite ()</code>´Ø¿ô¤ò¼ÂÁõ¤¹¤ë¤Î¤Ï¡¢
504 È¿ÉüŪ¤Ç´Ö°ã¤¤¤ä¤¹¤¤ºî¶È¤Ç¤¹¡£
505 @ref CreatingTestSuite ¤Î´Ø¿ô¤Î¥»¥Ã¥È¤È¥³¥Þ¥ó¥É¤Ï<code>suite ()</code>
506 ´Ø¿ô¤Î¼ÂÁõ¤ò¼«Æ°Åª¤ËºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
507 @~
508
509 @dontinclude complex/testComplex.c
510
511 @~english
512 The following code is a rewrite of ComplexTest using those command:
513 @~japanese
514 °Ê²¼¤Î¥³¡¼¥É¤Ï¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤¬»È¤¦¤è¤¦¤ËComplexTest¤ò½ñ´¹¤¨¤¿¤â¤Î¤Ç¤¹¡£
515 @~
516
517 @code
518 #include <cppunit/CCUnitAssert.h>
519 @endcode
520
521 @~english
522 First, you declare the fixture, passing the test fixture
523 name to the javaDoc style comment, which consist of a
524 C-style comment block starting with two <tt>*</tt>'s:
525 @~japanese
526 ºÇ½é¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤òÀë¸À¤·¤Þ¤¹¡£
527 ¤³¤ì¤ÏjavaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥ÈÆâ¤Ë¥Õ¥£¥¯¥¹¥Á¥ã¤Î̾Á°¤òµ­½Ò¤·¤Þ¤¹¡£
528 javaDoc¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¤È¤Ï C ¥¹¥¿¥¤¥ë¤Î¥³¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î³«»Ï¤¬
529 Æó¤Ä¤Î¥¢¥¹¥¿¥ê¥¹¥¯<tt>*</tt>¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
530 @~
531
532 @code
533 //** test case: complex number test *\/
534 @endcode
535
536 @~english
537 The function to make @link CCUnitTestSuite TestSuite
538 @endlink is <code>ccunit_suite</code>.
539 But, it can be changed to another
540 name by the <code>-f</code> option of the
541 <code>ccunit_makeSuite</code> command, too.
542 Then, you define each test case of the fixture with prefix
543 <code>test</code>, <code>setUp</code>,
544 <code>tearDown</code>:
545
546 @~japanese
547 @link CCUnitTestSuite TestSuite @endlink
548 ¤òºîÀ®¤¹¤ë´Ø¿ô¤Ï <code>ccunit_suite</code> ¤Ç¤¹¡¢
549 ¤·¤«¤· <code>ccunit_makeSuite</code> ¥³¥Þ¥ó¥É¤Î
550 <code>-f</code> ¥ª¥×¥·¥ç¥ó¤ÇÊ̤Î̾Á°¤ËÊѤ¨¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£
551 ¤½¤·¤Æ¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î¥Æ¥¹¥È¥±¡¼¥¹¤Ë¤Ï¤½¤ì¤¾¤ì̾Á°¤ÎÀèƬ¤Ë¡¢
552 <code>test</code>, <code>setUp</code>,
553 <code>tearDown</code> ¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
554 @~
555
556 @code
557 #include <complex.h>
558
559 static complex_t* s10_1;
560 static complex_t* s1_1;
561 static complex_t* s11_2;
562
563 void setUp_complex_test ()
564 {
565   s10_1 = complex_new (10, 1);
566   s1_1 = complex_new (1, 1);
567   s11_2 = complex_new (11, 2);
568 }
569
570 void tearDown_complex_test ()
571 {
572   complex_delete (s10_1);
573   complex_delete (s1_1);
574   complex_delete (s11_2);
575 }
576
577 //** test equals *\/
578 void test_complex_equals ()
579 {
580   CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
581   CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
582 }
583
584 //** test add *\/
585 void test_complex_add ()
586 {
587   complex_t c10_1 = { 10.0, 1.0 };
588   complex_t c1_1 = { 1.0, 1.0 };
589   complex_t result;
590   complex_t c11_2 = { 11.0, 2.0 };
591   CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
592 }
593
594 //** test sub *\/
595 void test_complex_sub ()
596 {
597   complex_t c9_0 = { 9, 0 };
598   complex_t result;
599   CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
600                           complex_sub (&result, s10_1, s1_1),
601                           complex_to_string);
602 }
603 @endcode
604
605 @~english
606 Finally, you end the fixture declaration:
607 @~japanese
608 ºÇ¸å¤Ë¡¢¥Õ¥£¥¯¥¹¥Á¥ã¤Î½ª¤ê¤òÀë¸À¤·¤Þ¤¹¡£
609 @~
610
611 @code
612 //** end test case *\/
613 @endcode
614
615 @~english
616 To generate creating suite function code, run
617 <code>ccunit_makeSuite</code> tool.
618 @~japanese
619 ¥¹¥¤¡¼¥ÈºîÀ®´Ø¿ô¤Î¥³¡¼¥É¤òÀ¸À®¤¹¤ë¤Ë¤Ï¡¢
620 <code>ccunit_makeSuite</code>¥Ä¡¼¥ë¤ò¼Â¹Ô¤·¤Þ¤¹¡£
621 @~
622
623 @code
624 $ ccunit_makeSuite -f complex_suite -o suiteComplex.c testComplex.c
625 $ cat suiteComplex.c
626
627 #include <ccunit/CCUnitTestSuite.h>
628 #include <ccunit/CCUnitTestFixture.h>
629 #include <ccunit/CCUnitTestCase.h>
630
631 //* test fixture: complex number test *\/
632 //* setUp_complex_test *\/
633 extern void setUp_complex_test ();
634 //* tearDown_complex_test *\/
635 extern void tearDown_complex_test ();
636 //* test_complex_equals *\/
637 extern void test_complex_equals ();
638 //* test_complex_add *\/
639 extern void test_complex_add ();
640 //* test_complex_sub *\/
641 extern void test_complex_sub ();
642
643 static CCUnitTestFunc fx_001_cases[] = {
644   {
645     "test_complex_equals",
646     "test equals",
647     test_complex_equals
648   },
649   {
650     "test_complex_add",
651     "test add",
652     test_complex_add
653   },
654   {
655     "test_complex_sub",
656     "test sub",
657     test_complex_sub
658   },
659   {
660     NULL, NULL, NULL
661   },
662 };
663
664 static CCUnitTestFixtureDfn fx_001 = {
665   { ccunitTypeFixture },
666   "complex number test",
667   {
668     "setUp_complex_test",
669     "setUp_complex_test",
670     setUp_complex_test
671   },
672   {
673     "tearDown_complex_test",
674     "tearDown_complex_test",
675     tearDown_complex_test
676   },
677   fx_001_cases,
678 };
679
680 static CCUnitTestDfn* suite_001_test[] = {
681     &fx_001.test,
682     NULL,
683 };
684
685 static CCUnitTestSuiteDfn suite_001 = {
686   { ccunitTypeSuite },
687   "",
688   suite_001_test
689 };
690
691
692 CCUnitTestSuite* complex_suite (const char* name)
693 {
694   if (!suite_001.name[0])
695     suite_001.name = name;
696   return ccunit_newTestSuiteFromDfn (&suite_001);
697 }
698 $
699 @endcode
700
701 @english
702 @section post_build_check Post-build check
703 @japanese
704 @section post_build_check ¥Ó¥ë¥É¸å¤Î¥Á¥§¥Ã¥¯
705 @endif
706
707 @~english
708 Now that we have our unit tests running, how about
709 integrating unit testing to our build process ?
710 @~japanese
711 ¤µ¤¢¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë½àÈ÷¤¬¤Ç¤­¤Þ¤·¤¿¡£
712 ¤Ç¤Ï¥Ó¥ë¥É¥×¥í¥»¥¹¤Ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤òÅý¹ç¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤é¤¤
713 ¤¤¤Ç¤·¤ç¤¦¡£
714 @~
715
716 @~english
717 To do that, the application must returns a value different than 0 to indicate that
718 there was an error.
719 @~japanese
720 ¤½¤¦¤¹¤ë¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤³¤È¤ò¼¨¤¹
721 £°°Ê³°¤ÎÃͤòÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
722 @~
723
724 @~english
725 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink returns
726 a integer indicating if the run was successful.
727 @~japanese
728 @link ccunit_runTestRunner() ccunit_runTestRunner() @endlink
729 ¤Ï¡¢¼Â¹Ô¤¬À®¸ù¤·¤¿¤«¤É¤¦¤«¤ò¼¨¤¹À°¿ô¤òÊÖ¤·¤Þ¤¹¡£
730 @~
731
732 @~english
733 Updating our main programm, we obtains:
734 @~japanese
735 ¼¡¤Î¤è¤¦¤Ë¥á¥¤¥ó¥×¥í¥°¥é¥à¤ò¹¹¿·¤·¤Þ¤¹¡£
736 @~
737
738 @code
739 #include <ccunit/CCUnitTestRunner.h>
740
741 int main (int argc, char** argv)
742 {
743   CCUnitTestRunner* runner;
744   CCUnitTestSuite* suite;
745   int wasSucessful;
746   runner = ccunit_newTestRunner (stdout);
747   suite = ccunit_suite ();
748   wasSucessful = ccunit_runTestRunner (runner, suite);
749   return wasSucessful;
750 }
751 @endcode
752
753 @~english
754 Now, you need to run your application after compilation.
755 The sample program made in the above is in the examples/complex directory.
756 @~japanese
757 ¤½¤ì¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤¿¸å¤Ë¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç
758 ¤¦¡£
759 °Ê¾å¤ÇºîÀ®¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï examples/complex ¥Ç¥£¥ì¥¯
760 ¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£
761 @~
762
763 */