JUnit Best Practices Cherami Reposted (Participate: 20053, Expert Count: 4960) Published: 2003-9-16 7:57 PM Version: 1.0 Read: 3899 times
Martin Fowler said: "When you try to print some information or debug an expression, write some test code to replace those traditional methods." At the beginning, you will find that you always have some new FixTure, and test It seems that your programming speed is slow. Soon, you will find that you reuse the same fixture, and new tests usually only involve adding a new test method. You may write a lot of test code, but you will soon find that the test you want to think is really useful. The test you need is the test that will fail, that is, those that you think will not fail, or you think that you should fail. We mentioned earlier testing is a process that does not interrupt. Once you have a test, you have to keep it working properly to verify the new work code you join. Don't run test every few days or in the end, you should run test code every day. This kind of investment is small, but you can make sure you get a reliable work code. Your rework rate is low, you will have more time to write work code. Don't think that the pressure is large, don't write the test code. On the contrary, write test code will gradually alleviate your pressure, and you should have an exact understanding of the behavior of the class by writing test code. You will write an efficient work code faster. Here are some specific methods or preferred methods of writing test code: 1. Do not initialize FixTure with TestCase constructor, and use setup () and teardown () methods. 2. Do not rely or assume the order of test run, because JUnit uses the Vector Save Test Method. So different platforms will take out test methods from the vector in different orders. 3. Avoid writing TestCase with side effects. For example: If the subsequent test depends on some specific transaction data, it is not necessary to submit transaction data. Simple will be able to roll. 4. When inheriting a test class, remember to call the SETUP () and Teardown () method of the parent class. 5. Place the test code and work code together and compile and update synchronously. (Task for JUnit is supported in Ant 6. The test class and test method should have a consistent naming scheme. To form a test class name as before the work class is preceded. 7. Make sure the test is not related to time, do not rely on the use of expired data for testing. It is difficult to reproduce the test during subsequent maintenance. 8. If you write software to the international market, you should consider international factors when preparing tests. Don't test only with the mother tongue Locale. 9. Use JUnit to provide an ASSERT / FAIL method as much as possible, and the code can be more concise. 10. Test should be as small as possible and perform fast.