JUnit Best Implementation

xiaoxiao2021-03-06  76

Reprinted from: http://www.sawin.com.cn/doc/sa/newtech/junit1.htm

What is JUNIT? CHERAMIJUNIT is a Java test framework for the source code to write and run repeatable tests. He is an instance for unit test framework system xUnit (for Java language). It includes the following characteristics: 1. Assertion 2 for testing the desired result 2, test tools for sharing co-test data, for easy tissue and run test test kit 4, graphics, and text test runners JUnit was originally written by Erich Gamma (one of the GOFs) and Kent Beck (one of the pioneers of XP and Refactor). The JUnit is generally used to perform unit test, so you need to understand the internal structure of the test code ( The so-called white box test), additional JUnit is a tool recommended in XP programming and reconstruction (refactor), because in the case of automatic unit testing, it can greatly improve the efficiency of development, but actually write tests. The code also takes a lot of time and energy, then where is the benefit of this stuff? The author thinks that this is: 1. For XP programming, the author requires writing test before writing code, so you can force you to think about the function and logic of the code (method) before writing code, otherwise the code written is unstable. Then you need to maintain test code and actual code at the same time, this workload will greatly increase. Therefore, in XP programming, the basic process is this: concept - "Write test code -" Write code - "test, and write testing and writing code is incremental, write a little test, in the next code If the problem can be discovered by the problem, it is similar to the problem of returning errors. It is similar to the benefits and XP programming. Because the reconstruction is also required to change a little measurement, reduce The time consumption caused by the regression error. 3. For non-above two cases, we use Junit to write some appropriate tests when developing, because usually need to write code code, may not use JUnit, if you use JUnit, and Interface (Method) Writing Test Code will reduce future maintenance, such as modifications within the method (this is equivalent to reconstruction). Also because JUnit has an assertion function. If the test result does not tell us that the test is not passed, why, and if you want the previous general practice to write some test code to see its output results, then to determine the results correctly The advantage of using JUnit is whether this result is correct. It is to be done. We only need to see if it tells us whether it is correct, and it will greatly improve efficiency in general.

JUnit Getting Started CHERAMI Solit Installation JUnit Installation is very simple, first go to the following address to download a latest zip package: http: //download.sourceForge.net/junit/ Download after downloading to your favorite directory, assume it is junit_home, then Add JUnit_Home's junit.jar to your system's ClassPath environment variable, for the IDE environment, for the JUnit project you need to use to Lib, set different IDEs with different settings, not more . How to write tests using JUnit? The simplest examples are as follows: 1. Create a subclass of Testcase: package junitfaq; import junitfaq; *; import junit.framework. *; Public class simpletest extends testcase {public simpletest (String name) {super (name); } 2 The beginning, so these methods can be automatically found and tested. 3. Write a Suite () method, which uses the reflex dynamic to create a test kit containing all TestXxxx methods: public static test suite () {return new testsuite (simpletest.class);} 4, write a main () Methods Easy to run tests in a text runner: public static void main (string args []) {junit.textui.teestrunner.run (suite ());}} 5, run test: Java junitfaq The test result passed by .simpletest is: .time: 0ok (1 tests) Time denotes the number of tests, if the test is passed, OK is displayed. Otherwise, the rear side of the dot is marked, indicating that the test failed. Each test result should be OK, so that the test is successful, if it is not successful, it will be corrected immediately according to the prompt information. If JUnit reports the test is not successful, it will distinguish between failures and errors. Failure is caused by the failure of the assert method in your code; and the error is caused by the code exception, such as ArrayIndexOutofboundSexception. Operation in graphics: Java junit.swingui.teestrunner junitfaq.simpletest passed the test results in the green strip of the graphics interface. The above is the simplest test sample. In the actual test, we test a class of features often need to perform some common operations. After completion, you need to destroy the resource occupied (such as network connection, database connection, close files) Wait, the TestCase class provides us with the setup method and Teardown method. The content of the setup method will run before testing each Testxxxx method you have written, and the content of the Teardown method will execute after each Testxxx method. . This has a total of initialization code, which eliminates the possible interactions between each test code.

JUnit Best Practice Martin Fowler said: "When you try to print some information or debug an expression, write some test code to replace those traditional methods." At first, you will find that you always have some new FixTure, and test seem to make your programming speed slow down. 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.

转载请注明原文地址:https://www.9cbs.com/read-121053.html

New Post(0)