CuTest-README.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. Originally obtained from "http://cutest.sourceforge.net/" version 1.4.
  2. HOW TO USE
  3. You can use CuTest to create unit tests to drive your development
  4. in the style of Extreme Programming. You can also add unit tests to
  5. existing code to ensure that it works as you suspect.
  6. Your unit tests are an investment. They let you to change your
  7. code and add new features confidently without worrying about
  8. accidentally breaking earlier features.
  9. LICENSING
  10. Copyright (c) 2003 Asim Jalis
  11. This software is provided 'as-is', without any express or implied
  12. warranty. In no event will the authors be held liable for any damages
  13. arising from the use of this software.
  14. Permission is granted to anyone to use this software for any purpose,
  15. including commercial applications, and to alter it and redistribute it
  16. freely, subject to the following restrictions:
  17. 1. The origin of this software must not be misrepresented; you must not
  18. claim that you wrote the original software. If you use this software in
  19. a product, an acknowledgment in the product documentation would be
  20. appreciated but is not required.
  21. 2. Altered source versions must be plainly marked as such, and must not
  22. be misrepresented as being the original software.
  23. 3. This notice may not be removed or altered from any source
  24. distribution.
  25. GETTING STARTED
  26. To add unit testing to your C code the only files you need are
  27. CuTest.c and CuTest.h.
  28. CuTestTest.c and AllTests.c have been included to provide an
  29. example of how to write unit tests and then how to aggregate them
  30. into suites and into a single AllTests.c file. Suites allow you
  31. to put group tests into logical sets. AllTests.c combines all the
  32. suites and runs them.
  33. You should not have to look inside CuTest.c. Looking in
  34. CuTestTest.c and AllTests.c (for example usage) should be
  35. sufficient.
  36. After downloading the sources, run your compiler to create an
  37. executable called AllTests.exe. For example, if you are using
  38. Windows with the cl.exe compiler you would type:
  39. cl.exe AllTests.c CuTest.c CuTestTest.c
  40. AllTests.exe
  41. This will run all the unit tests associated with CuTest and print
  42. the output on the console. You can replace cl.exe with gcc or
  43. your favorite compiler in the command above.
  44. DETAILED EXAMPLE
  45. Here is a more detailed example. We will work through a simple
  46. test first exercise. The goal is to create a library of string
  47. utilities. First, lets write a function that converts a
  48. null-terminated string to all upper case.
  49. Ensure that CuTest.c and CuTest.h are accessible from your C
  50. project. Next, create a file called StrUtil.c with these
  51. contents:
  52. #include "CuTest.h"
  53. char* StrToUpper(char* str) {
  54. return str;
  55. }
  56. void TestStrToUpper(CuTest *tc) {
  57. char* input = strdup("hello world");
  58. char* actual = StrToUpper(input);
  59. char* expected = "HELLO WORLD";
  60. CuAssertStrEquals(tc, expected, actual);
  61. }
  62. CuSuite* StrUtilGetSuite() {
  63. CuSuite* suite = CuSuiteNew();
  64. SUITE_ADD_TEST(suite, TestStrToUpper);
  65. return suite;
  66. }
  67. Create another file called AllTests.c with these contents:
  68. #include "CuTest.h"
  69. CuSuite* StrUtilGetSuite();
  70. void RunAllTests(void) {
  71. CuString *output = CuStringNew();
  72. CuSuite* suite = CuSuiteNew();
  73. CuSuiteAddSuite(suite, StrUtilGetSuite());
  74. CuSuiteRun(suite);
  75. CuSuiteSummary(suite, output);
  76. CuSuiteDetails(suite, output);
  77. printf("%s\n", output->buffer);
  78. }
  79. int main(void) {
  80. RunAllTests();
  81. }
  82. Then type this on the command line:
  83. gcc AllTests.c CuTest.c StrUtil.c
  84. to compile. You can replace gcc with your favorite compiler.
  85. CuTest should be portable enough to handle all Windows and Unix
  86. compilers. Then to run the tests type:
  87. a.out
  88. This will print an error because we haven't implemented the
  89. StrToUpper function correctly. We are just returning the string
  90. without changing it to upper case.
  91. char* StrToUpper(char* str) {
  92. return str;
  93. }
  94. Rewrite this as follows:
  95. char* StrToUpper(char* str) {
  96. char* p;
  97. for (p = str ; *p ; ++p) *p = toupper(*p);
  98. return str;
  99. }
  100. Recompile and run the tests again. The test should pass this
  101. time.
  102. WHAT TO DO NEXT
  103. At this point you might want to write more tests for the
  104. StrToUpper function. Here are some ideas:
  105. TestStrToUpper_EmptyString : pass in ""
  106. TestStrToUpper_UpperCase : pass in "HELLO WORLD"
  107. TestStrToUpper_MixedCase : pass in "HELLO world"
  108. TestStrToUpper_Numbers : pass in "1234 hello"
  109. As you write each one of these tests add it to StrUtilGetSuite
  110. function. If you don't the tests won't be run. Later as you write
  111. other functions and write tests for them be sure to include those
  112. in StrUtilGetSuite also. The StrUtilGetSuite function should
  113. include all the tests in StrUtil.c
  114. Over time you will create another file called FunkyStuff.c
  115. containing other functions unrelated to StrUtil. Follow the same
  116. pattern. Create a FunkyStuffGetSuite function in FunkyStuff.c.
  117. And add FunkyStuffGetSuite to AllTests.c.
  118. The framework is designed in the way it is so that it is easy to
  119. organize a lot of tests.
  120. THE BIG PICTURE
  121. Each individual test corresponds to a CuTest. These are grouped
  122. to form a CuSuite. CuSuites can hold CuTests or other CuSuites.
  123. AllTests.c collects all the CuSuites in the program into a single
  124. CuSuite which it then runs as a single CuSuite.
  125. The project is open source so feel free to take a peek under the
  126. hood at the CuTest.c file to see how it works. CuTestTest.c
  127. contains tests for CuTest.c. So CuTest tests itself.
  128. Since AllTests.c has a main() you will need to exclude this when
  129. you are building your product. Here is a nicer way to do this if
  130. you want to avoid messing with multiple builds. Remove the main()
  131. in AllTests.c. Note that it just calls RunAllTests(). Instead
  132. we'll call this directly from the main program.
  133. Now in the main() of the actual program check to see if the
  134. command line option "--test" was passed. If it was then I call
  135. RunAllTests() from AllTests.c. Otherwise run the real program.
  136. Shipping the tests with the code can be useful. If you customers
  137. complain about a problem you can ask them to run the unit tests
  138. and send you the output. This can help you to quickly isolate the
  139. piece of your system that is malfunctioning in the customer's
  140. environment.
  141. CuTest offers a rich set of CuAssert functions. Here is a list:
  142. void CuAssert(CuTest* tc, char* message, int condition);
  143. void CuAssertTrue(CuTest* tc, int condition);
  144. void CuAssertStrEquals(CuTest* tc, char* expected, char* actual);
  145. void CuAssertIntEquals(CuTest* tc, int expected, int actual);
  146. void CuAssertPtrEquals(CuTest* tc, void* expected, void* actual);
  147. void CuAssertPtrNotNull(CuTest* tc, void* pointer);
  148. The project is open source and so you can add other more powerful
  149. asserts to make your tests easier to write and more concise.
  150. AUTOMATING TEST SUITE GENERATION
  151. make-tests.sh will grep through all the .c files in the current
  152. directory and generate the code to run all the tests contained in
  153. them. Using this script you don't have to worry about writing
  154. AllTests.c or dealing with any of the other suite code.
  155. CREDITS
  156. [02.23.2003] Dave Glowacki has added
  157. (1) file name and line numbers to the error messages, (2)
  158. AssertDblEquals for doubles, (3) Assert<X>Equals_Msg version of
  159. all the Assert<X>Equals to pass in optional message which is
  160. printed out on assert failure.