If you are first contact junit, you may have a lot of questions, here, according to the online information, some list of questions that can be used to relieve your doubts, I want to be useful to you:
[001] No one wants to see how others design test class. If everyone has different design test classes, light maintenance is tested enough, who is still maintaining test class?
JUnit is a regression test framework. The so-called frame is his author Erich Gamma and K ENT BECK to order some bar frames. The test code you have written must follow this bar box: inherit a class, implement an interface. In fact, it is the specification we mentioned earlier. Fortunately, JUnit is currently recognized by most software engineers. We will get a lot of support to follow JUnit. If there is no specification, the test class will be a standard that needs to be defined.
[002] There is another point I don't want to mention, but this problem is too obvious, the test class is more than the test class! Does this mean this double work?
Do not! 1) Whether the method of the test class is complex, the method of the class in the test code will only maintain the same amount of code. 2) Improve the quality of the software and solve the problem of software entropy is not cost. The class in the test code is the price. What we can do now is to minimize the consideration of the payment: The test code we have written must be readily read by maintenance personnel. We have a certain specification to write test code.
[003] What is software entropy?
There is a smart person proposing the concept of software entropy: starting from a very good state of the design, as the new features continue to join, the program gradually lost the original structure, eventually become a group Mess.
[004] How to prepare the test code to really find the problem?
Pay attention to writing a certain test code (such as the previous example) does not make sense, only test code can help us find bugs, test code has its value. In addition, the test code should also test the work code. If the parameters called by the method are sent to the null value, the error value, and the correct value, see if the behavior of the method is as expected. You can search for some of the experiences summarized online.
[005] What is an assertion?
As an assertion is a Boolean sentence, the statement cannot be false. If it is a fake, it indicates a bug. I know this word when I use JMeter, what does it mean about this word? People who use Rational or MI automation test tools must know one of the terms "checkpoint" (or verification point), we can understand the assertion as a checkpoint or verification point.
Note: The following 006-013 is taken from the People's Posts Publishing House published Wang Dongge's "Software Test and JUnit Practice", the biggest feature of the book is "learning", is the accumulation of author experience, recommended reading.
【006】 White box test case should be written?
Regardless of which test cases are inevitably involved in three types of data: normal data, boundary values, and error data. Since the number of test cases is enabled, the automated test tool must be used, but the extraction test data will still rely on the experience of writing test cases.
[007] What is the difference between class test and traditional unit test?
Slightly different types of tests and traditional tests, which are caused by the specificity of the class and its implementation, so the testing of the class is more complicated. The traditional unit tests the interface test between the units, that is to say that each unit has its own input output interface, if a serious error occurs in the call, then these errors are also caused by the implementation of the interface between the unit, and There is no direct relationship between the unit itself.
In the object-oriented world, each class is a minimum interactive unit, and its internal encapsulates various attributes and messaging methods. The class is instantiated, and each object has a relative declaration cycle and activity range. In this range, the class test In addition to the class method included in the test class, it is also testing the status of the class, which is a traditional unit test. Nothing. [008] What do you pay attention to?
Class test cases need to ensure that the class instance fully meets the design description of the class, and tests a certain class In addition to ensuring that it enables a predetermined design, it is necessary to pay attention to this class and other classes (may be associated, It is an implementation or dependent on, if the class test case can only override the priority implementation unit, then reduce the effective execution of the entire test case, or need to put more resource production more test cases to test the class, It is also very difficult to maintain the maintenance of post-test cases.
[009] What is the test process of the class?
Test the test drive drive to cover the test case of this class, generate the test environment and instance required for the use case according to the specified situation of the test case. In this test environment, the test drive to instance sends one or more to verify messages, and then determine whether the verification message is passed according to the instance status change, response value, returns parameters, and results. After all test case verification items are executed, the test driver releases all generated instances and value domains.
If the class contains static attributes or static methods, it also needs to be tested. These static properties and methods belong to the class itself, indicating that the class itself is an object, and these properties and methods can be called directly without instantification.
[010] What extent does the class need to test?
The degree of testing is depends on how many class implementations and class descriptions have been tested, and different types of testing levels are more difficult, such as 1 group method in the interface class, there is a specific class to implement it, we have test cases in specific classes The test of the interface method is added, and if the other instance method of the specific class does not interact with any class, the changed test coverage is 100%. Each class has its own status after instantiation, the status impact of the status affects the specific meaning. For any class, it is necessary to perform comprehensive considerations, exhaustion is a non-efficient working method, we can use a combination test or to draw an important in multiple test examples, and the maximum test case is performed.
[011] How to determine the class test case?
The determination and configuration of class test cases depends on the accurate class description, and the class description may contain a class UML design, class CRC card, and class state machine. Two working roles (designers and realiques) were interpreted in class tests, so that the implementation personnel were misused in the test case due to misunderstanding, and the test was not accurate or test failure. . If the class does not match the description of the description, please don't describe the designer, although the "reverse engineering" can be used, such a class description is lacking.
1. Determine the test case according to the preamp and back state
The test case determines according to the preamp and rear conditions, the specific implementation form is various test method, and specifies the input value (including the common value and the boundary value) in the front condition (including the common value and boundary value) to increase the test coverage rate of the test case, each Test cases can include special pre-conditions (ie, error preamplifiers) to observe whether the exception handling mechanism after the test case failure is correct. Depending on the class description, the preamble and post conditions of the test case are derived, and different test case specific test methods are produced according to different combinations of pre-predetermined and back conditions.
2. Determine the test case according to the status conversion
Many people are used to use the status conversion diagram to determine testing examples rather than based on the pre-condition and rear state of the test case, the class association behavior is obviously very easy to be identified by the tester, but this also requires testing. Multi-special value verification class association relationship (for example using extreme input values), it is difficult to determine all test cases using the status conversion map using the extreme input value). 3, determine the test case according to the code
[012] How to construct a test drive?
The concept of test in RUP is that the test is preferred to design, and in XP, it behaves as a test priority to programming. The basic idea of testing drivers is to take into account the design (or write) test code before designing (or), so that the test work is not only testing, but it becomes a specification of design (or code).
Class Test Drives is the executable program that is new to dry one or more test cases, and three methods of writing class test drivers are (1) call the main method, embed static test methods, and implement the independent test class. (2) In class embedded static test method, the execution result of each test case is collected by calling the test method. (3) Realize the independent test class. Three types of test driver writing methods are the same as the effect of performing test cases and obtaining the execution test results, it is recommended to use the third test driver writing method.
[013] What are the test methods of class?
There are three different test methods:
1. Functional test interface: Test case is determined according to class description;
2. Constructing the test interface: Test cases focus on the structure of the test class (class may contain multiple constructors);
3, interactive test interface: Test case test Send message to an object of an object is correct.
Determining these test interfaces are for subsequent maintenance, it also reflects what rules we follow to determine test cases, and related to changes in the class' s impact on test cases, expressing behavior using TestCase.
If the class changes in the specific instantiation process, then the corresponding test driver needs to be adjusted.
[014] JUnit common interface and class
1, Test interface: run test and collect test results;
2, TestCase abstract class: define the fixed method in the test;
TestCase is an abstract implementation of the Test interface (cannot be instantiated, can only be inherited)
3. ASSERT Static Class: Collection of a series of assertion methods;
4, Testsuit Test Package: Multiple tests combination
The TestSuit class is responsible for assembling multiple TestCase. In the test, you may include multiple tests of the tested class, while TestSuit is responsible for collecting these tests, allowing us to complete all of the tested programs in one test. .
Supplement: A test becomes testcase (test case), and a group of TestCase constitutes the TestSuite (test suite).
[015] JUnit Writing the Basic steps of the test class:
1> Expand TestCase class;
2> Override Runtest () method (optional);
3> Write some TestXxxxxx () methods;
[016] some of the skills or better practices for some specific written test code:
1. Do not initialize FixTure with the constructor of TestCase, and use the 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, which is supported in Ant, support JUnit.)
6. Test class and test methods 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.