What are unit tests?

Unit testing is the work of checking and verifying the smallest testable unit of software, usually a function or class, in isolation from the rest of the program.

Unit tests are performed in an automated manner, which makes them more profitable in scenarios with a lot of regression testing.

Use examples of functions are provided in the unit test code, because unit tests take the form of calling functions with various combinations of input parameters.

How to do unit testing well?

1) Basic characteristics of the code and the cause of the error

Both development language and scripting language, will be conditional branch, circulation processing and function calls the most basic logic control, if the code needs to achieve specific business logic aside, just look at the code structure, all of the code are in classifying data processing, every condition judgement is a classification processing, nested conditions determine or cycle, It’s also doing sorting.

If any of the categories are missing, a defect will occur; If any of the categories are wrong, defects will also occur; If the classification is correct, there are no omissions, but the processing logic of the classification is wrong, which can also cause defects.

2) Detailed explanation of unit test cases

A use case for a unit test is a collection of “input data” and “expected output.” You need to infer the correct expected output from the logical function for the identified input and verify it by executing the code under test. That is, “what input and what output should be produced based on the logical function that the code needs to perform”.

Type of unit test case input data

The input parameters of the function being tested; A global static variable that needs to be read from within the function under test; A member variable that needs to be read from within the function under test; Data obtained from calls to subfunctions within a function; Function internal call subfunction overwritten data; In embedded systems, data overwritten when interrupted calls; … Expected Output:

The return value of the function under test; The output parameters of the function being tested; The member variable overwritten by the test function; Global variables overwritten by test functions; File updates in the function under test; Database updates made in the function under test; Message queue updates in the function under test; … 3) Driver code, pile code, and Mock code

The driver code is used to call the function under test, while the stub code and Mock code are used to replace the real code called by the function under test.

If you are interested in software testing and want to learn more about testing, solve testing problems, and get started with guidance to help you solve the puzzles encountered in testing, we have technical experts here. If you are looking for a job or just out of school, or have already worked but often feel a lot of difficult, think that their test learning is not enough, want to continue learning, want to change careers afraid of learning not, can join us 1079636098, In the group, you can get the latest software test factory interview materials and Python automation, interface, framework building learning materials!

The relationship between drivers, piles, and Mock code

Driver refers to the code that calls the function under test. In the process of unit test, the Driver module usually includes three steps: data preparation for calling the function under test, calling the function under test and verifying the relevant results.

Code stubs are temporary code that replaces real code. For example, an internal implementation of function A calls an unimplemented function B. In order to test the logic of function A, it is necessary to simulate A function B. The simulated implementation of function B is called the pile code.

Pseudo code:



In order to achieve full path coverage of function A, you need to control the return value of function B in different test cases, then the pseudocode of pile function B should be:



Pile function B should return true for the first test case and false for the second test case. This covers the two if-else branches of the function being tested.

The application of pile code first plays the role of isolation and completion, so that the code under test can compile, link, and run independently. At the same time, the pile code also has the function of controlling the execution path of the function under test.

There are three general principles to follow when writing stub code:

  • The pile function should have exactly the same form as the original function, only the internal implementation is different, so that the test code can be correctly linked to the pile function; The pile function used to implement isolation and completion is relatively simple, just keep the declaration of the original function, add an empty implementation, the purpose is to compile the link; Pile function to achieve control function is the most widely used, according to the needs of test cases, output appropriate data as the internal input of the function under test. The essential difference between Mock and stub code is that you test for Assert and Expectiation of expected results.

    For Mock code, we focus on whether the Mock method is called, written arguments are called, how many times it is called, and the order in which Mock functions are called. So, in tests that use Mock code, validation of the result (that is, assert) usually occurs in the Mock function. For Stub code, we focus on using stubs to control the execution path of the function under test, not whether or how they are called. So, when you test using stubs, the verification of the result is usually in the driver code.

How do unit tests work in real projects?

1) Not all code should be unit tested. Unit tests are usually only used for tests of underlying modules or core modules.

2) You need to determine the type of unit testing framework, which is directly related to the development language. For example, the most commonly used unit testing frameworks for Java are Junit and TestNG; The most commonly used C/C++ unit testing frameworks are CppTest and Parasoft C/C++ Test; Once the framework selection is complete, you will also need to select the stub framework and Mock framework, depending on the specific technology stack used for development. Typically, the selection of unit test frameworks, piles/mocks is a joint decision between the development architect and the test architect.

3) In order to be able to measure code coverage for unit tests, you often also need to introduce tools to calculate code coverage. Different languages have different code coverage statistics tools, such as JaCoCo for Java, Istanbul for JavaScript.

4) Finally you need to integrate unit test execution, code coverage statistics, and continuous integration pipeline. To ensure that each code delivery will automatically trigger the unit test, and the code coverage will be counted automatically during the execution of the unit test. Finally, the “unit test pass rate” and “code coverage” will be used as the criteria to determine whether the backstabbing code delivery can be accepted.

Q&A

When you implement unit testing across the entire project, you may find that there are some hurdles to overcome:

1) Tightly coupled code is difficult to isolate;

2) It is difficult to compile links after isolation;

3) The testability of the code itself is poor, and the testability of the code is usually proportional to the size of the code;

4) Unable to directly simulate the call of the underlying functions of the system through the pile code;

5) The more difficult it is to improve code coverage.

The future of you will certainly thank now desperately own!

To recommend a software testing technology exchange group: 1079636098 group benefits free

I wish you and I meet, both gain! Welcome to wechat public account: Programmer Yifan

1. Receive a free 216-page software test engineer interview guide.

2. Software test learning route and corresponding video learning tutorial free to share!