3 Testing is an important part of the Mono project: every one of its
4 three major components has a test suite tailored for its needs. This
5 is very helpful, because in the course of developing the software it
6 is very common to introduce bugs in existing code. A test suite
7 helps us fix the bugs as soon as they are introduced.
11 All classes in Mono libraries should have comprehensive unit test
12 suites to go with them. Unit testing is a software engineering
13 methodology that makes it easier to build correct code. Every
14 method in every class should have a set of tests to verify
15 that they work correctly. Mono also needs a testing framework
16 to make it easy to write and run lots of tests.
21 If you are new to writing NUnit tests, there is a template you may use
22 to help get started. The file is:
24 <b>mcs/class/doc/TemplateTest.cs</b>
26 Save a copy of this file in the appropriate test subdirecty
27 (see below), and replace all the {text} markers with
28 appropriate code. Comments in the template are there to guide
29 you. You should also look at existing tests to see how other
30 people have written them.
31 mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs
32 is a small one that might help.
34 The directory that will contain your new file depends on the
35 assembly/namespace of the class for which you are creating the
36 tests. Under mcs/class there is a directory for each
37 assembly. In each assembly there is a Test directory,
38 e.g. mcs/class/corlib/Test. In the Test directory there are
39 sub-directories for each namespace in the assembly,
40 e.g. mcs/class/corlib/Test/Sytem. Put your new test file in
41 the appropriate sub-directory under Test for the class you are
44 Once all of that is done, you can do a 'make test' from the top mcs
45 directory. Your test class will be automagically included in the
46 build and the tests will be run along with all the others.
48 * Tips on writing Unit tests.
50 You should look at the <a href="http://nunit.org">NUnit documentation</a>,
51 as it is a fantastic product, and includes fantastic documentation,
52 but here are some tips for those of you who are already reading
56 ** Provide an unique error message for Assert()
58 Include an unique message for each Assert() so that when the assert
59 fails, it is trivial to locate it in the source. Otherwise, it may be
60 difficult to determine which part of the test is failing. A good way
61 to ensure unique messages is to use something like #A01, #A02 etc.
66 AssertEquals("array match", compare[0], i1[0]);
67 AssertEquals("array match", compare[1], i1[1]);
68 AssertEquals("array match", compare[2], i1[2]);
69 AssertEquals("array match", compare[3], i1[3]);
74 AssertEquals("#A01", compare[0], i1[0]);
75 AssertEquals("#A02", compare[1], i1[1]);
76 AssertEquals("#A03", compare[2], i1[2]);
77 AssertEquals("#A04", compare[3], i1[3]);
80 Once you used such a number in an Assert(), don't change it later on -
81 people might use it it identify the test in bug reports or in mailing
84 ** Use AssertEquals() to compare things, not Assert().
86 Do not compare two values with Assert() - if the test fails,
87 people have no idea what went wrong while AssertEquals()
88 reports the failed value.
92 Assert ("A01", myTicks[0] == t1.Ticks);
97 AssertEquals ("A01", myTicks[0], t1.Ticks);
100 ** Test your test with the Microsoft runtime
102 If possible, try to run your testsuite with the Microsoft runtime on
103 .NET on Windows and make sure all tests in it pass. This is especially
104 important if you're writing a totally new testcase - without this
105 check you can never be sure that your testcase contains no bugs ....
107 Don't worry if you're writing your test on Linux, other people can
108 test it for you on Windows.
110 Sometimes you may discover that a test doesn't show the expected
111 result when run with the Microsoft runtime - either because there is a
112 bug in their runtime or something is misleading or wrong in their
113 documentation. In this case, please put a detailed description of the
114 problem to mcs/class/doc/API-notes and do also report it to the
115 <a href="mailing-lists">mailing list</a> - we'll forward this to the
116 Microsoft people from time to time to help them fix their documentation
121 Why do unit testing? It becomes simple to run automated tests
122 for the whole library. Unit tests are a safety net - you can
123 change part of the code and verify that you haven't broken
124 anything. Ideally, tests are written before the actual library
125 code itself. And every time a bug is discovered, a test should
126 be written to demonstrate the bug and its fix. Then, if
127 you ever reintroduce the bug, you will know immediately. For
129 href="http://junit.sourceforge.net/doc/testinfected/testing.htm">
130 JUnit Test Infected: Programmers Love Writing Tests</a>.
135 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>.
141 Someone will make sure to add the file or apply the patch as
142 appropriate. If you plan to be an on-going contributor and
143 would like to get cvs account, email <a href="mailto:miguel@ximian.com">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.