testing 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 all of that is done, you can do a 'make test' from the top mcs
  35. directory. Your test class will be automagically included in the
  36. build and the tests will be run along with all the others.
  37. * Tips on writing Unit tests.
  38. You should look at the <a href="http://nunit.org">NUnit documentation</a>,
  39. as it is a fantastic product, and includes fantastic documentation,
  40. but here are some tips for those of you who are already reading
  41. this web page.
  42. ** Provide an unique error message for Assert()
  43. Include an unique message for each Assert() so that when the assert
  44. fails, it is trivial to locate it in the source. Otherwise, it may be
  45. difficult to determine which part of the test is failing. A good way
  46. to ensure unique messages is to use something like #A01, #A02 etc.
  47. Ok:
  48. <pre>
  49. AssertEquals("array match", compare[0], i1[0]);
  50. AssertEquals("array match", compare[1], i1[1]);
  51. AssertEquals("array match", compare[2], i1[2]);
  52. AssertEquals("array match", compare[3], i1[3]);
  53. </pre>
  54. Excellent:
  55. <pre>
  56. AssertEquals("#A01", compare[0], i1[0]);
  57. AssertEquals("#A02", compare[1], i1[1]);
  58. AssertEquals("#A03", compare[2], i1[2]);
  59. AssertEquals("#A04", compare[3], i1[3]);
  60. </pre>
  61. Once you used such a number in an Assert(), don't change it later on -
  62. people might use it it identify the test in bug reports or in mailing
  63. lists.
  64. ** Use AssertEquals() to compare things, not Assert().
  65. Do not compare two values with Assert() - if the test fails,
  66. people have no idea what went wrong while AssertEquals()
  67. reports the failed value.
  68. Ok:
  69. <pre>
  70. Assert ("A01", myTicks[0] == t1.Ticks);
  71. </pre>
  72. Excellent:
  73. <pre>
  74. AssertEquals ("A01", myTicks[0], t1.Ticks);
  75. </pre>
  76. ** Test your test with the Microsoft runtime
  77. If possible, try to run your testsuite with the Microsoft runtime on
  78. .NET on Windows and make sure all tests in it pass. This is especially
  79. important if you're writing a totally new testcase - without this
  80. check you can never be sure that your testcase contains no bugs ....
  81. Don't worry if you're writing your test on Linux, other people can
  82. test it for you on Windows.
  83. Sometimes you may discover that a test doesn't show the expected
  84. result when run with the Microsoft runtime - either because there is a
  85. bug in their runtime or something is misleading or wrong in their
  86. documentation. In this case, please put a detailed description of the
  87. problem to mcs/class/doc/API-notes and do also report it to the
  88. <a href="mailing-lists">mailing list</a> - we'll forward this to the
  89. Microsoft people from time to time to help them fix their documentation
  90. and runtime.
  91. ** Unit tests.
  92. Why do unit testing? It becomes simple to run automated tests
  93. for the whole library. Unit tests are a safety net - you can
  94. change part of the code and verify that you haven't broken
  95. anything. Ideally, tests are written before the actual library
  96. code itself. And every time a bug is discovered, a test should
  97. be written to demonstrate the bug and its fix. Then, if
  98. you ever reintroduce the bug, you will know immediately. For
  99. more info, read <a
  100. href="http://junit.sourceforge.net/doc/testinfected/testing.htm">
  101. JUnit Test Infected: Programmers Love Writing Tests</a>.
  102. ** Getting Started
  103. We welcome all contributions to the Class Libary Test Suite.
  104. There is information to help you get started in CVS at
  105. mcs/class/doc/NUnitGuidelines. Once you have written your test, please
  106. post it to <a href="mailing-lists.html">mono-list</a>.
  107. Someone will make sure to add the file or apply the patch as
  108. appropriate. If you plan to be an on-going contributor and
  109. would like to get cvs account, email <a href="mailto:[email protected]">miguel</a>.
  110. Normally, after you send a couple of well-written new files
  111. and/or patches to the list, you will be given cvs access.