Main Problems and Solutions of unit test practices (5)

Source: Internet
Author: User

(Part 1)

3.2 how to solve the "cannot do" Problem"

The preceding example uses an independent function to demonstrate the etdd process. In actual work, Code usually depends on each other. This dependency will make the test difficult. This is a problem that cannot be done.

First, let's analyze it. "Cannot do" mainly refers to testability issues. The core of testability is internal input. Before explaining the internal input, let's take a look at the general input: external input.

External Input is an input that can be set outside the tested code, including parameters, member variables, and global variables. External input can be set directly.

The core difficulty of unit testing lies in internal input. What is internal input?
As in the following example, both data are obtained within the tested code by calling the associated code, that is, the internal data obtained. What should I do with the code for internally obtained data? Like parameters, it is also classified. Therefore, classification detection is also required during testing. This is the internal input.

Internal input can be handled in six situations using tools.

The main methods for solving internal input include piling, simulated object, and underlying simulation.
Let's first introduce piling. Pile is some code that replaces the real code. The main functions of the pile are isolation, filling and control. You can write a pile of code to solve internal input problems. This is the control function of the pile.

Using piling to solve internal input has some problems: first, writing the pile Code increases the workload; second, separating internal input from external input makes it difficult to manage; third, only some internal input problems can be solved. For example, to call the same correlated function multiple times in a single use case, it is difficult to implement a pile of code if different output is required each time.

Another method for solving internal input is to simulate objects. This is complicated and not suitable for C and C ++. We can use underlying simulation to solve internal input problems.

The underlying simulation has three features: one is the management of internal input and external input; the other is the absence of the State of associated code, the absence of associated code, and the isolation, can be used directly. Third, you do not need to write code.

I will also use a case to explain the underlying simulation. This example is an air-conditioning control program.

The function of the code is to first obtain the ambient temperature, and then compare it with the preset target temperature to calculate the temperature difference. Each time the temperature difference is reached, the refrigerator runs for 60 seconds.

First, set external data. Assume that the preset target temperature is 25 degrees, which is the global variable and is set to 25. If the return value is 1, the operation is successful. If the ambient temperature is 28 degrees, the refrigerator should run for 180 seconds. Enter 180 here. Then perform the test.

The test fails because the ambient temperature has not been set. The environment temperature is obtained by this function. Even if this function works properly, the obtained ambient temperature cannot meet our testing requirements. We can use the underlying simulation to solve the problem.

(To be continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.