Junit (3) Introduction to JUnit and unit testing, junit Unit Testing
1. Several Related Concepts
White-box test-the test object is regarded as an open box. The logic structure and other information in the program are made public to the tester.
Regression testing-software or environment repair or corrected "retest", which is especially useful for such testing.
Unit Test-a minimum-granularity test to test a function or code block. Generally, it is done by programmers because they need to know the details of internal program design and coding.
JUnit is a Java testing framework for source code development. It is used to write and run repeated tests. This is an example of the unit test framework system xUnit (for java ). It is mainly used for white box testing and regression testing.
2. unit test Overview
2.1 Benefits of Unit Testing
A. Improve development speed-testing is executed in an automated manner, improving the efficiency of testing code execution.
B. Improve the quality of software code-it is released to integration using a minor version to facilitate debugging. At the same time, the refactoring concept is introduced to make the code clean and flexible.
C. improve the reliability of the system-it is a type of regression testing. It supports "retest" after repair or correction to ensure code correctness.
2.2 target objects of Unit Tests
A. process-oriented software development.
B. object-oriented software development for objects.
C. You can perform class tests, function tests, and interface tests (the most common method in the test class ).
2.3 unit test tools and frameworks
Currently, the most popular unit testing tool is the xUnit series framework, which is commonly divided into JUnit (java), CppUnit (C ++), DUnit (Delphi), and NUnit (. net), PhpUnit (Php), and so on. The first and most outstanding application of this testing framework was developed by Erich Gamma (author of design patterns) and Kent Beck (founder of XP (Extreme Programming) open Source Code provided by JUnit.
3. Getting started with Junit
3.1. Benefits of JUnit and compilation principles of JUnit Unit Testing
Benefits:
A. the test code can be separated from the product code.
B. The test code for a class can be applied to another class with few changes.
C. It is easy to integrate into the construction process of testers. The combination of JUnit and Ant can implement incremental development.
D. JUnit is open source code and can be used for secondary development.
C. You can easily expand JUnit.
Writing principles:
A. simplified testing, including learning the test framework and compiling the actual test unit.
B. Keep the test unit persistent.
C. You can use existing tests to compile relevant tests.
3.2 JUnit features
A. Use the assertion method to determine the difference between the expected value and the actual value, and return A Boolean value.
B. the test driver uses a common initialization variable or instance.
C. The test package structure facilitates organization and integration.
D. Graphic and text interaction modes are supported.
3.3 JUnit framework composition
A. A set of testing methods and processes for the test target, which can be called A test case (TestCase ).
B. A set of test cases. It can contain multiple test cases (TestCase) and is called a test suite ).
C. Description and record of test results. (TestResult ).
D. Test listener ).
E. Description of the inconsistency between each test method and the expected result, TestFailure)
AssertionFailedError ).
The JUnit framework is a typical Composite mode: TestSuite can accommodate any object derived from Test. When the run () method of the TestSuite object is called, it will traverse the object it holds, call their run () Methods one by one. (See programmer issue-6 ).
3.4 JUnit installation and configuration
The steps for installing JUnit are as follows:
Download the JUnit package at http://download.sourceforge.net/junit/and decompress the JUnit package to a physical directory (for example, C: \ Junit3.8.1 ).
Record the Directory Name of the Junit. jar file (for example, C: \ Junit3.8.1 \ Junit. jar ).
Go to the operating system (based on the Windows operating system) and click "start to set control panel" in order ".
Select "system" in the control panel options, click "environment variables", and select the "CLASS-PATH" keyword (Case Insensitive) in the "variables" list box of "system variables ), if this keyword does not exist, add it.
Double-click the "CLASS-PATH" keyword to add the string "C: \ Junit3.8.1 \ Junti. jar "(Note: if other strings exist, add a semicolon"; "at the end of the string), so that after modification, Junit can be applied in the integration environment.
For the IDE environment, add the JUnit project to lib and set different IDE settings.
3.5 common interfaces and classes in JUnit
Test interface -- run Test and collect Test results
The Test interface uses the Composite design mode. It is a common interface for separate Test cases (TestCase), aggregation Test mode (TestSuite), and Test extension (TestDecorator.
Its public int countTestCases () method is used to calculate the number of TestCase in this test. The other method is public void run (TestResult), and TestResult is the test result of the instance, run method to execute this test.
TestCase abstract class -- Define a fixed method in the test
TestCase is an abstract Implementation of the Test interface (it cannot be instantiated but can only be inherited). Its constructor TestCase (string name) creates a Test instance based on the input Test name. Because each TestCase must have a name when it is created, if a test fails, you can identify which test failed.
The setUp () and tearDown () methods contained in the TestCase class. In the setUp () method set, all variables and instances required for the test are initialized, And the setUp () method is executed again before each test method in the test class is called in turn. The tearDown () method releases the variables and instances referenced in the test procedure method after each test method.
When writing test cases, developers only need to inherit TestCase to complete the run method. Then, JUnit obtains the test case, executes its run method, and records the test results in TestResult.
Assert static Class-A set of assertion Methods
Assert contains a set of Static Test Methods for comparing the expected value with the actual value. That is, if the test fails, the Assert class throws an AssertionFailedError exception, the JUnit testing framework classifies this error as Failes and records it, and marks it as failing the test. If a parameter of the String type is specified in this method, this parameter is used as the identifier of the AssertionFailedError exception, telling the tester to modify the exception details.
JUnit provides six types of 31 groups of assertions, including basic assertions, digital assertions, character assertions, Boolean assertions, and object assertions.
The internal logic of assertEquals (Object expcted, Object actual) determines whether the equals () method is used. This indicates whether the internal hash values of the two instances are equal, it is best to use this method to compare the values of corresponding class instances. The internal logic judgment of assertSame (Object expected, Object actual) uses the Java operator "=", which indicates that the assertSame determines whether two instances come from the same Reference ), it is best to use this method to compare the values of instances of different classes. AsserEquals (String message, String expected, String actual) This method performs logical comparison on the two strings. If the two strings do not match, the difference is displayed. The ComparisonFailure class provides two strings for comparison. If the two strings do not match, the detailed differences are given.
TestSuite test package-a combination of multiple tests
The TestSuite class is responsible for assembling multiple Test Cases. The class to be tested may include multiple tests for the class to be tested, and TestSuit is responsible for collecting these tests so that in a test, complete all tests on the classes under test.
The TestSuite class implements the Test interface and can contain other TestSuites. It can handle all the exceptions thrown when the Test is added.
TestSuite has six protocols for processing test cases (otherwise, the test will be rejected)
A test case must be Public)
B test cases must inherit from the TestCase class
C. The test method of the test case must be Public)
D. the test method of the test case must be declared as Void.
E. The prerequisite for the test method in the test case must be test.
F. In the test case, the test method mistakenly transmits any parameters.
N TestResult result class and other classes and interfaces
The TestResult result class combines any accumulated test results and passes each test Run () method through the TestResult instance. If the execution of TestCase fails, TestResult will throw an exception.
The TestListener interface is an event listening protocol and can be used by the TestRunner class. It notifies listener of Object-related events, including startTest (Test test), endTest (Test test), error, and addError (Test test, Throwable t) exceptions) and add failed addFailure (Test test, AssertionFailedError t)
The TestFailure failure class is a collection class that describes exceptions during each test execution. Its toString () method returns a brief description of the "failure" status
Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.