testing 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. ** Class Library Tests
  8. All classes in Mono libraries should have comprehensive unit test
  9. suites to go with them. Unit testing is a software engineering
  10. methodology that makes it easier to build correct code. Every
  11. method in every class should have a set of tests to verify
  12. that they work correctly. Mono also needs a testing framework
  13. to make it easy to write and run lots of tests.
  14. ** Getting started
  15. If you are new to writing NUnit tests, there is a template you may use
  16. to help get started. The file is:
  17. <b>mcs/class/doc/TemplateTest.cs</b>
  18. Save a copy of this file in the appropriate test subdirecty
  19. (see below), and replace all the [text] markers with
  20. appropriate code. Comments in the template are there to guide
  21. you. You should also look at existing tests to see how other
  22. people have written them.
  23. mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs
  24. is a small one that might help.
  25. The directory that will contain your new file depends on the
  26. assembly/namespace of the class for which you are creating the
  27. tests. Under mcs/class there is a directory for each
  28. assembly. In each assembly there is a Test directory,
  29. e.g. mcs/class/corlib/Test. In the Test directory there are
  30. sub-directories for each namespace in the assembly,
  31. e.g. mcs/class/corlib/Test/Sytem. Put your new test file in
  32. the appropriate sub-directory under Test for the class you are
  33. testing.
  34. Once your test class is complete, you need to add it to the
  35. AllTests.cs file in the same directory as your new test. Add a
  36. call to "suite.AddTest()" passing the name of your new test
  37. class's suite property as the parameter. You will see
  38. examples in the AllTests.cs file, so just copy and paste
  39. inside there.
  40. Once all of that is done, you can do a 'make test' from the top mcs
  41. directory. Your test class will be automagically included in the
  42. build and the tests will be run along with all the others.
  43. * Tips on writing Unit tests.
  44. You should look at the NUnit documentation, as it is a
  45. fantastic product, and includes fantastic documentation, but
  46. here are some tips for those of you who are already reading
  47. this web page.
  48. ** Provide an unique error message for Assert()
  49. Include an unique message for each Assert() so that when the assert
  50. fails, it is trivial to locate the failing one. Otherwise, it may be
  51. difficult to determine which part of the test is failing. A good way
  52. to ensure unique messages is to use something like #A01, #A02 etc.
  53. Ok:
  54. <pre>
  55. AssertEquals("array match", compare[0], i1[0]);
  56. AssertEquals("array match", compare[1], i1[1]);
  57. AssertEquals("array match", compare[2], i1[2]);
  58. AssertEquals("array match", compare[3], i1[3]);
  59. </pre>
  60. Excellent:
  61. <pre>
  62. AssertEquals("#A01", compare[0], i1[0]);
  63. AssertEquals("#A02", compare[1], i1[1]);
  64. AssertEquals("#A03", compare[2], i1[2]);
  65. AssertEquals("#A04", compare[3], i1[3]);
  66. </pre>
  67. Once you used such a number in an Assert(), don't change it later on -
  68. people might use it it identify the test in bug reports or in mailing
  69. lists.
  70. ** Use AssertEquals() to compare things, not Assert().
  71. Do not compare two values with Assert() - if the test fails,
  72. people have no idea what went wrong while AssertEquals()
  73. reports the failed value.
  74. Ok:
  75. <pre>
  76. Assert ("A01", myTicks[0] == t1.Ticks);
  77. </pre>
  78. Excellent:
  79. <pre>
  80. AssertEquals ("A01", myTicks[0], t1.Ticks);
  81. </pre>
  82. ** Constructors
  83. When writing your testcase, please make sure to provide a constructor
  84. which takes no arguments:
  85. <pre>
  86. public class DateTimeTest : TestCase
  87. {
  88. public DateTimeTest() : base ("[MonoTests.System.DateTimeTest]") {}
  89. public DateTimeTest (string name): base(name) {}
  90. public static ITest Suite
  91. {
  92. get {
  93. TestSuite suite = new TestSuite ();
  94. return suite;
  95. }
  96. }
  97. }
  98. </pre>
  99. ** Namespace
  100. Please keep the namespace within each test directory consistent - all
  101. tests which are referenced in the same AllTests.cs must be in the same
  102. namespace. Of course you can use subnamespaces as you like -
  103. especially for subdirectories of your testsuite.
  104. For instance, if your AllTests.cs is in namespace "MonoTests" and you
  105. have a subdirectory called "System", you can put all the tests in that
  106. dir into namespace "MonoTests.System".
  107. ** Test your test with the Microsoft runtime
  108. If possible, try to run your testsuite with the Microsoft runtime on
  109. Windows and make sure all tests in it pass. This is especially
  110. important if you're writing a totally new testcase - without this
  111. check you can never be sure that your testcase contains no bugs ....
  112. Don't worry if you're writing your test on Linux, other people can
  113. test it for you on Windows.
  114. Sometimes you may discover that a test doesn't show the expected
  115. result when run with the Microsoft runtime - either because there is a
  116. bug in their runtime or something is misleading or wrong in their
  117. documentation. In this case, please put a detailed description of the
  118. problem to mcs/class/doc/API-notes and do also report it to the list -
  119. we'll forward this to the Microsoft people from time to time to help
  120. them fix their documentation and runtime.
  121. ** Unit tests.
  122. Why do unit testing? It becomes simple to run automated tests
  123. for the whole library. Unit tests are a safety net - you can
  124. change part of the code and verify that you haven't broken
  125. anything. Ideally, tests are written before the actual library
  126. code itself. And every time a bug is discovered, a test should
  127. be written to demonstrate the bug and its fix. Then, if
  128. you ever reintroduce the bug, you will know immediately. For
  129. more info, read <a
  130. href="http://junit.sourceforge.net/doc/testinfected/testing.htm">
  131. JUnit Test Infected: Programmers Love Writing Tests</a>.
  132. ** Getting Started
  133. We welcome all contributions to the Class Libary Test Suite.
  134. There is information to help you get started in CVS at
  135. mcs/class/doc/NUnitGuidelines. Once you have written your test, please
  136. post it to <a href="mailing-lists.html">mono-list</a>.
  137. Someone will make sure to add the file or apply the patch as
  138. appropriate. If you plan to be an on-going contributor and
  139. would like to get cvs account, email <a href="mailto:[email protected]">miguel</a>.
  140. Normally, after you send a couple of well-written new files
  141. and/or patches to the list, you will be given cvs access.