Software testing-unit testing

Source: Internet
Author: User
1. What isUnit Test

  Software TestingThere are many methods. from different perspectives, there are also different types of virtues. If we look at the process of software development, software testing is generally divided into unit testing, integration testing, validation testing, acceptance testing,System TestIt can be seen that unit testing is the lowest level of testing activities carried out during software development. In unit testing activities, the independent unit of the software willOthersTest in the case of partially isolated parts. Unit testing is done by the programmer himself, and the ultimate benefit is the programmer himself. In this case, programmers have the responsibility to write functional code and unit tests for their own code. The unit test is executed to verify that the behavior of a code segment is consistent with what we expect.

In general, unit testing is a function that implements simple functions. It uses a specific set of inputs (test cases) to test whether the function is normal and returns the correct input.

  Ii. Basic Path Method for unit testing

There are also several methods for unit testing. I want to introduce the basic path method that is widely used. The basic path method is a one-to-one test method based on the Program Control Flow Chart (the control flow chart and the program statement can be said to correspond. Therefore, when talking about the basic path method, you have to first introduce the program control flowchart.

Example:

Note: It is the basic unit structure of the complete control flow diagram.

Next I will write a program for you and draw a control flow diagram of the program.

Code:

Control flow diagram:

 

Note: The number in the figure refers to the line number of the Code. When we see this stream chart, we may be somewhat confused. How are the code line numbers in the figure selected? Why do we need to select those in the figure? Yes:

The code of the row number selected in the figure shows the selection of branches, cyclic operations, judgment, and so on. The statements executed in sequence can be merged. This is also a general rule for drawing a control flow chart.

3. Circle complexity

With the control flow diagram, we naturally need to know how many test cases we need to write to meet the basic path test requirements.

When you see the control flowchart described above, people who have studied discrete mathematics or data structures will feel very familiar with it, the control flowchart is derived from graphs and Directed Graphs (will this make everyone feel the importance of mathematics to us again ?) Next I will introduce the new concept-circle complexity.

Circle complexity is a software measurement that provides quantitative testing for the complexity of program logic. Use this measurement to calculate the number of basic independent paths of a program. The upper limit of the number of tests executed at least once for all statements.

Formula circle complexity V (G) = E-N + 2, e is the number of edge in the flow graph, n is the number of nodes in the flow graph.

Formula circle complexity V (G) = p + 1, p is the number of nodes identified in Flow Graph G.

In layman's terms, the circle complexity is to determine whether the unit is complex and whether the test criteria are good. Generally, if the circle complexity is greater than 20, it indicates that the Unit is not testable and too complex.

We can see that:

V (G) = 10 sides-8 nodes + 2 = 4

V (G) = 3 judgment nodes + 1 = 4

The circle complex graph of is 4. What is the significance of this result for us? It indicates that we only need up to four test cases to reach the basic path coverage.

  Iv. Basic Program path

Now we know that we need to write at least four test cases. But how can we design these four test cases? Export the BASIC Program path, and design the test example based on the basic program path.

Basic Program path: the Basic independent path is to select any path traversal from the Start Node to the end of the program, but each path should contain at least one edge that is not used by the defined path. (It seems hard to understand. Let's look at the example ).

Let's look at the flowchart above: How many paths are there from node 4 to node 24? Obviously:

A 13-> 35

B 13-> 15-> 17-> 35

C 13-> 15-> 23-> 29-> 33-> 13-> 35

D 13-> 15-> 23-> 25-> 33-> 13-> 35

What else?

E 13-> 15-> 23-> 29-> 33-> 13-> 15-> 17-> 35?

No, why? Because the above four paths already contain all edges. The first path does not contain any unused edge. All paths have been traversed.

Now we have four basic independent paths. We can design test cases based on the independent paths.

A 13-> 35

Input data: Flag = 0, or a value of flag <0.

Expected results: temp = 0.

B 13-> 15-> 17-> 35

Input data: Count = 1; flag = 0

Expected results: temp = 101.

C 13-> 15-> 23-> 29-> 33-> 13-> 35

Input data: Count = 1; flag = 1

Expected results: temp = 10.

D 13-> 15-> 23-> 25-> 33-> 13-> 35

Input data: Count = 1; flag = 2

Expected results: temp = 20.

Can the above test cases be simplified? Of course.

See path a 13-> 35 and C 13-> 15-> 23-> 29-> 33-> 13-> 35. Path 1 is the true subset of Path 4, so 1 can be unnecessary. The complexity of the circle is 4. What is the significance of this result for us? It indicates that we only need up to four test cases to reach the basic path coverage. Therefore, the number of test cases is the highest, not four. But one thing to declare is that the simpler the test cases, the less test you have, the lower the program security.

The next task is to use the test tool for testing based on our test cases.

  V. Summary

By now, the basic path method of unit test has been briefly introduced. A complete test is carried out by combining multiple testing methods. In addition, no matter how many test cases you design, no matter how many test methods you use, or how perfect your test solution is, it is impossible for you to discover all the bugs 100%. We will test and use the least resources to do the most test and detection, so that the program can have as few bugs as possible. Another point is that the exhaustive test is the most effective, but that is impossible. This is also one reason why our basic path method is widely used.

Related Article

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.