Unit testing is a white-box test. It is the first and most important part of all tests.
It is mainly used for testing the public method of a class.
Writing unit tests is an important way to verify the behavior of Class Functions and optimize the design. Therefore, it is the work of programmers.
Unit Test Writing Process
Retrieve a test case from the test list
Define a test method
Public void test _ {methodname }_{ inputparameter }_{ expectation}
{
// Prepare test data
// Execute the tested method
// Verify the expected results
}
Prepare test data
This mainly includes building parameters of the tested method, simulating the dependent objects called by the tested method, and assigning values to the tested objects through the set attribute.
Method for executing the test
Verify expected results
Asserted the method to be tested, and asserted the call status of the simulated object.
Here is a specific example:
[Test]
Public void test_read_singlekey_successful ()
{
Memcachedclient = schonerstorage. memcacheclient. cacheclient;
// Prepare test data
String businesskey = "100_1 ";
Byte [] returndata = new byte [] {1, 23, 12, 1 };
Reverse CT. Call (memcachedclient. Get (businesskey). Return (returndata );
Memcachedclient. Replay ();
// Execute the tested method
Byte [] actualdata = schonerstorage. Read (businesskey );
// Verify the expected results
Assert. areequal (returndata, actualdata );
Memcachedclient. verifyallexpectations ();
}
Unit Test naming rules
The project that stores the test class is named {name of the project to be tested}. unittest
The test class maintains the same directory structure or package structure as the project to be tested, and is named {tested class} Test
Test Cases in the test class are named as test _ {tested method }_{ test point }_{ expected test result }.
{Test point} can be a parameter type, parameter boundary value, and so on.
Unit Test considerations
Isolate the dependencies of test cases (unit test tools such as xunit usually ensure this)
Test cases should cover normal workflows, abnormal workflows, input parameter boundary values, etc.
The granularity of test cases cannot be too large. If a test case has too many vertices to be verified, You can see whether it can be split into smaller cases.
Test-driven development (TDD)
TDD is a brand new development model, which forces developers to write unit tests before writing product code. The process is as follows:
Abstract The classes and interfaces involved in a feature and their dependencies in units of a feature.
Write test cases for the class (first, list the test list in the test Class header, and then write the test cases according to a certain priority)
Run the test case. The test case failed.
Compile product code
Run the test case. The test case passes
Repeat 3-5 steps to complete the test case of the entire class.
Refactor product code or test cases to eliminate repeated designs and optimize the code structure
Make sure all test cases pass
Submit code to the product Library
CI server performs integration test on the unit test.
If any test case fails, repair the failed test case first before developing the new product code.
TDD helps developers develop product code from the user's perspective, and keeps the product codeSimple and available.
Introduction to Unit Testing