testing 9.1 KB

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