Unit test - entry

xiaoxiao2021-03-06  20

Previous article (unit test - theoretical article) discussed what is the advantages of unit testing, unit tests, and has suited a lot of excuses of non-written unit testing. If you agree with our point of view, the identity unit test is indeed an indispensable process in software development, then we start the unit test tour!

A function we compare the maximum value we first introduced a function of comparing the maximum value. We have passed an array parameters that type INT, which will return the element of the maximum value. The code is as follows: public class largest {public static int largest (int [】) {int max = 0; for (int i = 0; i

Add a main method directly to the Largest class, either rewrite a runoff class to test the Largest. Such tests have also brought us great challenge: 1. Verify difficulties. How to verify the behavior of the code and our expectations? Use a lot of if ... Else plus == or equals () to determine? How to deal with the abnormal situation? Confusion verification, it is easy to bring BUG to our test code, so that we are not confident to your test. 2, the test class cannot be managed. How do we intuitively get a successful or failed message? Is the original system.out.println ()? Can we run multiple unit tests at a time? If the previous test is abnormal, can the following tests continue to run? If there are many test classes, hundreds of or more, can we be convenient to output test results by the console? 3, unable to count the test code coverage. Lack of unified test code writing specification and agreed, readability, and maintenance. However, face these challenges don't have to be frustrated. The unit test framework has helped us solve these problems, which provide a lot of testing infrastructure, allowing us to put more experience into the test code.

JUnitjun was presented by Erich Gamma (one of the GOFs) and one of the Kent Beck (one of the pioneers of XP and Refactor), which is an open source Java test framework for writing and run repeatable testing.

Below we gradually describe how to test the LARGEST class: 1. JUnit installation. If the development tool you use is Eclipse, it doesn't have any installation, it has provided JUnit support. Otherwise, you need to go to http://www.junit.org/ to download the JUnit installation package. Installation is very simple, just set the junit.jar package to ClassPath, let your Java code can find it. Second, write test code. The code is as follows: public class largesttest extends testcase {public void testlargest () {int [] DataS = {7,8,9}; assertequals (9, largest.largest (data));}} Description: 1, test class is generally Inherit the abstract class Testcase. It achieves various test methods and provides an architecture of a test process. 2. Test code is determined by assertion (Assert) to determine if a test function is working properly. JUnit provides a lot of assertion functions, used to determine if a condition is true; whether the two data is equal, or some cases, or some other cases. 3, the test method name begins with "Test", so the JUnit framework automatically discovers that this is a test method. Third, run the test class. Run the test success.

Do we finish this test? No, the above test can only be considered a verification. In the data we give, the maximum value 9 is the last element of the array. If 9 is the first element it is correct? What if the data is negative? and many more. Our maximum function has a lot of boundaries that require unit testing to verify. Therefore, we must do a comprehensive plan for testing before writing unit test, pre-set the content to test, and may have erroneous boundary conditions. Below is a test plan for Largest: 1, whether the location of array elements has an impact on maximum? L [7, 8, 9] - 9L [7, 9, 8] - 9L [9, 8, 7] - 92, if there are two equal maximum values, what happens? L [7, 9, 8, 9] - 93, if there is only one element in the array, what is the result? l [1] - 14, if the element is negative? L [-7, -8, -9] - 7 complete test code should be as follows: public class largesttete Extends Testcase {public void testsimple () {askSERTEQUALS (9, Largest.lagest (new int] in {{7, 8, 9});} public void tester () {Assertequals (9, Largest.Largest (new int {{7,9,8}); assertequals (9, largest.largest (new int intersect "{9,8 , 7}));} public void testdups () {Assertequals (9, Largest.lagest (new int {7, 9, 8, 9});} public void testOne () {Assertequals (1, Largest. Largest (new int ");} public void testnegative () {assertequals (-7, landst.largest (new int intersects {- 7, -8, -9}));}} Of course, you You can run it immediately after writing a test method. This time is not so lucky, an error occurred while running the last test method TestNegative (): junit.framework.assertionFailederror: Expected: <- 7> But was: <0> at Test.junit.largestTest.TestNEGATIVE (LargestTest " .java: 24) Careful you, perhaps the Largest's bug may be found at this. It turns out that our field max is initialized to 0 is wrong, it should be changed to Integer.min_Value. From this we can think that the use of unit tests can indeed discover hidden bugs as soon as possible, and the last one we also said, the soaking bugs can save more time and reduce more risks. This is, is our unit testing is endless? Oh, maybe you will think that if you get in the Largest () method is empty, what will it be? This question is left to our readers think.

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

New Post(0)