testing 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. * Testing
  2. Testing is an important part of the Mono project: every one of its
  3. three major components has a test suite tailored for its needs. This
  4. is very helpful, because in the course of developing the software it
  5. is very common to introduce bugs in existing code. A test suite
  6. helps us fix the bugs as soon as they are introduced.
  7. There are various kinds of tests in Mono:
  8. <ul>
  9. <li><a href="#unit"><b>Class Library Unit
  10. Tests:</b></a> These are used to test the class
  11. libraries.
  12. <li><a href="#compiler"><b>Compiler tests</b></a>: Both
  13. tests that should pass and tests that should fail are included.
  14. <li><a href="#runtime"><b>Runtime tests</b></a>: Tests for
  15. the virtual machine.
  16. <li><a href="#aspnet"><b>ASP.NET tests</b></a>: ASP.NET tests.
  17. <li><a href="#ws"><b>Web Services tests</b></a>: Web Services
  18. client/server tests.
  19. </ul>
  20. <a name="unit"></a>
  21. * Class Library Tests
  22. All classes in Mono libraries should have comprehensive unit test
  23. suites to go with them. Unit testing is a software engineering
  24. methodology that makes it easier to build correct code. Every
  25. method in every class should have a set of tests to verify
  26. that they work correctly. Mono also needs a testing framework
  27. to make it easy to write and run lots of tests.
  28. In some classes, we might also provide standalone tests because of
  29. some reasons such as too huge testcases, another downloading and so on.
  30. (For example, managed XSLT has standalone test which downloads and
  31. expands some megabytes of OASIS test suite.)
  32. Here I list them up as long as I know. If you are going to add another
  33. standalone tests, please add one line here. It is also recommended that
  34. you add some notes on how to build and run tests.
  35. <ul>
  36. * Mono.Data/test/
  37. * System.Data/Test, and some individual ADO.NET libraries:
  38. there are some standalone tests. See the bottom of <a href="ado-net.html">
  39. ADO.NET page</a> for detail.
  40. * System.Web/Test/TestMonoWeb : see README
  41. * System.Web.Services/Test/standalone : see README
  42. * System.Windows.Forms/SWFTest/
  43. * System.XML/Tests/System.Xml.Schema/standalone_tests : see README
  44. * System.XML/System.Xml.Serialization/standalone_tests/
  45. * System.XML/Tests/System.Xml.Xsl/standalone_tests : see README
  46. </ul>
  47. ** Getting started
  48. If you are new to writing NUnit tests, there is a template you may use
  49. to help get started. The file is:
  50. <b>mcs/class/doc/TemplateTest.cs</b>
  51. Save a copy of this file in the appropriate test subdirecty
  52. (see below), and replace all the {text} markers with
  53. appropriate code. Comments in the template are there to guide
  54. you. You should also look at existing tests to see how other
  55. people have written them.
  56. mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs
  57. is a small one that might help.
  58. The directory that will contain your new file depends on the
  59. assembly/namespace of the class for which you are creating the
  60. tests. Under mcs/class there is a directory for each
  61. assembly. In each assembly there is a Test directory,
  62. e.g. mcs/class/corlib/Test. In the Test directory there are
  63. sub-directories for each namespace in the assembly,
  64. e.g. mcs/class/corlib/Test/Sytem. Put your new test file in
  65. the appropriate sub-directory under Test for the class you are
  66. testing.
  67. Once all of that is done, you can do a 'make test' from the top mcs
  68. directory. Your test class will be automagically included in the
  69. build and the tests will be run along with all the others.
  70. * Tips on writing Unit tests.
  71. You should look at the <a href="http://nunit.org">NUnit documentation</a>,
  72. as it is a fantastic product, and includes fantastic documentation,
  73. but here are some tips for those of you who are already reading
  74. this web page.
  75. ** Provide an unique error message for Assert()
  76. Include an unique message for each Assert() so that when the assert
  77. fails, it is trivial to locate it in the source. Otherwise, it may be
  78. difficult to determine which part of the test is failing. A good way
  79. to ensure unique messages is to use something like #A01, #A02 etc.
  80. Ok:
  81. <pre>
  82. AssertEquals("array match", compare[0], i1[0]);
  83. AssertEquals("array match", compare[1], i1[1]);
  84. AssertEquals("array match", compare[2], i1[2]);
  85. AssertEquals("array match", compare[3], i1[3]);
  86. </pre>
  87. Excellent:
  88. <pre>
  89. AssertEquals("#A01", compare[0], i1[0]);
  90. AssertEquals("#A02", compare[1], i1[1]);
  91. AssertEquals("#A03", compare[2], i1[2]);
  92. AssertEquals("#A04", compare[3], i1[3]);
  93. </pre>
  94. Once you used such a number in an Assert(), don't change it later on -
  95. people might use it it identify the test in bug reports or in mailing
  96. lists.
  97. ** Use AssertEquals() to compare things, not Assert().
  98. Do not compare two values with Assert() - if the test fails,
  99. people have no idea what went wrong while AssertEquals()
  100. reports the failed value.
  101. Ok:
  102. <pre>
  103. Assert ("A01", myTicks[0] == t1.Ticks);
  104. </pre>
  105. Excellent:
  106. <pre>
  107. AssertEquals ("A01", myTicks[0], t1.Ticks);
  108. </pre>
  109. ** Test your test with the Microsoft runtime
  110. If possible, try to run your testsuite with the Microsoft runtime on
  111. .NET on Windows and make sure all tests in it pass. This is especially
  112. important if you're writing a totally new testcase - without this
  113. check you can never be sure that your testcase contains no bugs ....
  114. Don't worry if you're writing your test on Linux, other people can
  115. test it for you on Windows.
  116. Sometimes you may discover that a test doesn't show the expected
  117. result when run with the Microsoft runtime - either because there is a
  118. bug in their runtime or something is misleading or wrong in their
  119. documentation. In this case, please put a detailed description of the
  120. problem to mcs/class/doc/API-notes and do also report it to the
  121. <a href="mailing-lists">mailing list</a> - we'll forward this to the
  122. Microsoft people from time to time to help them fix their documentation
  123. and runtime.
  124. ** Unit tests.
  125. Why do unit testing? It becomes simple to run automated tests
  126. for the whole library. Unit tests are a safety net - you can
  127. change part of the code and verify that you haven't broken
  128. anything. Ideally, tests are written before the actual library
  129. code itself. And every time a bug is discovered, a test should
  130. be written to demonstrate the bug and its fix. Then, if
  131. you ever reintroduce the bug, you will know immediately. For
  132. more info, read <a
  133. href="http://junit.sourceforge.net/doc/testinfected/testing.htm">
  134. JUnit Test Infected: Programmers Love Writing Tests</a>.
  135. ** Getting Started
  136. We welcome all contributions to the Class Libary Test Suite.
  137. There is information to help you get started in CVS at
  138. mcs/class/doc/NUnitGuidelines. Once you have written your test, please
  139. post it to <a href="mailing-lists.html">mono-list</a>.
  140. Someone will make sure to add the file or apply the patch as
  141. appropriate. If you plan to be an on-going contributor and
  142. would like to get cvs account, email <a href="mailto:[email protected]">miguel</a>.
  143. Normally, after you send a couple of well-written new files
  144. and/or patches to the list, you will be given cvs access.
  145. <a name="compiler"></a>
  146. * Compiler tests
  147. Mono ships with three compilers: C#, VB.NET and JScript. The
  148. tests are ran by running the makefile target `make
  149. run-test-local' in the appropriate directory.
  150. The C# compilation tests live in mcs/tests, and the C# error
  151. tests live in mcs/errors.
  152. The VB.NET compilation tests live in mcs/btests.
  153. <a name="runtime"></a>
  154. * Runtime Tests
  155. These tests verify the virtual machine, to run these tests, do:
  156. <pre>
  157. cd mono/mono/tests
  158. make test
  159. </pre>
  160. <a name="aspnet"></a>
  161. * ASP.NET tests
  162. XSP, the Mono ASP.NET server has tests for ASP.NET pages. It uses
  163. <a href="http://nunitasp.sourceforge.net">NUnitAsp</a>. Right now
  164. it only has standalone tests, ie., tests that do not need their own
  165. global.asax or web.config files.
  166. If you want to run them, get the xsp CVS module and install it. Then:
  167. <pre>
  168. cd xsp/nunit-tests
  169. make
  170. cd standalone
  171. xsp
  172. </pre>
  173. And from another terminal:
  174. <pre>
  175. cd xsp/nunit-tests/standalone
  176. nunit-console standalone-tests.dll
  177. </pre>
  178. <a name="ws"></a>
  179. * Web Services tests
  180. The Test directory for the System.Web.Services assembly contains a
  181. standalone test suite for testing web services. It tests:
  182. <ul>
  183. <li>Proxy generation using the wsdl tool</li>
  184. <li>Access to web services using the generated client proxies</li>
  185. <li>Execution of web services in the server</li>
  186. </ul>
  187. This suite not only tests web services running on XSP, but it can also test
  188. services running on other platforms and that are available in internet. This
  189. will help track down interoperability issues.
  190. To build the test suite, just run:
  191. <pre>
  192. cd mcs/class/System.Web.Services/Test/standalone
  193. xsp --root server
  194. </pre>
  195. And from another terminal:
  196. <pre>
  197. cd mcs/class/System.Web.Services/Test/standalone
  198. make
  199. nunit-console testclient.dll
  200. </pre>
  201. This will download the wsdl documents, generate the proxies, build a dll with
  202. the proxies, and build the nunit tests. Then you can use nunit-console or
  203. gnunit to run the tests (the nunit dll is testclient.dll).
  204. Read the README file in mcs/class/System.Web.Services/Test/standalone for
  205. more info.