Lecture - How to do unit testing

How to do unit testing

Necessary skills:
1, the basic characteristics of code;
2, causes the error;
3, unit testing methods and major basic techniques, such as what driver code, the code stub code and Mock

First, the basic characteristics of the reason code and the error

Code rules can be found, the most basic logic control by a conditional branch, loop processing, and the like function calls.

To do function code is logically correct, complete and correct classification must be done exhaustive, processing logic colleagues in each category must be correct.

In order to implement the logic design and develop correct code, typically have the following considerations:
1, to achieve the correct functional logic, there are several normal input;
2, if there is more input boundary require special treatment;
3, various the possibility of potentially illegal input and how to deal with.

Second, unit test Comments

A unit test cases is a "data input" and "output expected" set
complete "input data":

  • The measured input parameters of the function;
  • Global static variable internal test functions to be read;
  • Internal member variable test function needs to be read;
  • Functions internal function call data obtained;
  • Internal function call subroutine rewrite data;
  • Embedded systems, data is rewritten in the interrupt calls;
    . . . .
    If there is no clear expected output, the test itself to lose significance. "It is expected to enter" absolutely not just as simple as the return value of the function, should contain all the data after completion of function execution rewritten.
  • Return value of the function test;
  • The measured input parameters of the function;
  • Measured rewritable function member variables;
  • Test function rewritable global variables;
  • File updating functions under test;
  • Database updating function measured;
  • Message queue updating function measured;
    . . . .
    For the expected output value, it must be strictly in accordance with the code of the function logic set, but can not read the code to calculate the expected output.
    If some equivalence classes or boundary value, without considering the development, testing time will not design test cases, the test will cause blind spots.
Third, driver code, the code stub code and Mock

Driver code is used to call the function test, and Mock stub code and the code is the code used in place of the real test of the function call.


13191251-55d225dfb5af3c97.png
Three logical relationship

Driver code (Driver) means a code that calls the function test. In the test unit, it comprises a drive module typically measured prior to call data preparation function, and a verification function call correlation result measured three steps. Normal driving structure of the code, determined by the frame unit testing.
Stub code (the Stub) is used in place of the provisional code real code. Such as: the internal implementation of a function A calls function B to a yet to be achieved, in order to test the logic function A, you need to simulate a function B, B to realize the function of this simulation is the so-called stub code

For example:


13191251-2b81b11fb829493f.png
A function is a function of the measured

Visible, the function A calls function B, but B cell function testing phase has not been achieved, with a fake instead of real function B function B, B is the function of this false stubs.

13191251-fcb2cd44cb724511.png
Pile achieve function (Function B),

Visible application stub code first played the role of isolation and filled, the code under test can independently compile, link, and run independently. Colleague, the stub code execution path further has a function of the measured function.
Write stub code is subject to three principles:

  • Stubs to have exactly the same function of the original prototype, only different internal implementation, this test code to connect to stubs;
  • Stubs for achieving isolation and afford relatively simple, just need to keep the original function declaration, plus a empty implementation, aimed at connecting the compile;
  • Stubs control functions is the most widely used, according to the needs of the test case, the output data as appropriate internal input test function.

Mock and stub code is very similar to the code used in place of the real temporary code, play the role of isolation and filled.


13191251-7db0874c635afd19.png
The difference between Mock and Stub

How to carry out the actual project unit tests?

1, usually only the bottom module or modules will be tested using the core unit testing;
2, unit testing framework determined, associated with the development language. Such as, Java-Junit & TestNG; C / C ++ - CppTest $ Parasoft C / C ++ test; Mock code and selection codes pile frame is a stack technology developed specifically employed.
3, code coverage statistics tool, Java's JaCoCo; JavaScript in Istanbul.
4, it needs to perform unit testing, continuous integration and code coverage statistics do assembly line integration, to ensure that each code is submitted, it will automatically trigger the unit tests. In the unit test pass rate and code coverage as this submission code can be accepted.

The article cites:
https://time.geekbang.org/column/article/10275

Guess you like

Origin blog.csdn.net/weixin_34200628/article/details/90819662