White-box test instance-11 logic coverage test

Source: Internet
Author: User

Original article, copyright Hu Tian Fa (hutianfa@163.com) All, reproduced please indicate the source: http://blog.csdn.net/aidisheng/archive/2008/10/07/3025617.aspx

 

The logic coverage test is to overwrite the program by traversing the logic structure of the program. The coverage of source code can be divided into the following six standards: Statement overwrite, decision overwrite (also known as branch overwrite), condition overwrite, and Decision-condition overwrite (also known as branch-condition overwrite), condition combination overwrite, and path overwrite.

Let's take a look at the source code of the specific example (C language ):

  1. /*
  2. * White-box test logic coverage test example
  3. * Author: Hu Tian Fa (hutianfa@126.com)
  4. */
  5. Int logicexample (int x, int y)
  6. {
  7. Int magic = 0;
  8. If (x> 0 & Y> 0)
  9. {
  10. Magic = x + y + 10; // statement Block 1
  11. }
  12. Else
  13. {
  14. Magic = x + y-10; // statement Block 2
  15. }
  16. If (MAGIC <0)
  17. {
  18. Magic = 0; // statement block 3
  19. }
  20. Return magic; // statement block 4
  21. }

Generally, white-box tests do not directly follow the source code, but design test cases and write test code according to the flowchart. When there is no design document, draw a flowchart based on the source code:

 

After completing the above preparations, we will begin to explain the six logical coverage standards:

I. Statement Overwrite

1. concept:

Design enough test cases so that each executable statement in the tested program is executed at least once. In this example, the executable statement refers to the statement in Block 1 to block 4.

2. Test cases:

{X = 3, y = 3} can be executed to statement Block 1 and statement block 4. The path is a-B-e-f.

{X =-3, y = 0} can be executed to statement Block 2, statement block 3, and statement block 4. The path is a-c-d-f.

In this way, two test cases are used to reach the statement coverage standard. Of course, the test cases (test case group) are not unique.

3. Test adequacy:

Assume that "&" in the first judgment statement if (x> 0 & Y> 0 is incorrectly written as "|" by the programmer ", that is, if (x> 0 | Y> 0), you can use a set of test cases designed above to perform the test. It can still overwrite 100% of the statements, therefore, the preceding logic error cannot be found during statement overwrite.

Among the six logic coverage standards, the statement coverage standards are the weakest.

 

Ii. Decision coverage (branch coverage)

1. concept:

Design enough test cases so that the "true" and "false" branches of each judgment in the tested program are executed at least once. In this example, there are two judgments: If (x> 0 & Y> 0) (marked as P1) And if (MAGIC <0) (marked as P2 ).

2. Test cases:

Data P1 P2 Path

{X = 3, y = 3}

T F

A-B-e-f

{X =-3, y = 0}

F T

A-c-d-F

 

The extract truth and false branches of the two judgments have been executed, so they meet the criteria covered by the judgment.

3. Test adequacy:

Assume that "&" in the first judgment statement if (x> 0 & Y> 0 is incorrectly written as "|" by the programmer ", that is, if (x> 0 | Y> 0), a set of test cases designed above can be used for testing, and the coverage can still reach 100%, therefore, the above logic errors cannot be found during the decision coverage.

Compared with statement overwrite: Because the executable statement is not in the true branch of the decision, or on the false branch, as long as the decision coverage criteria are met, the statement overwrite criteria must be met, otherwise. Therefore, the decision overwrite is stronger than the statement overwrite.

 

Iii. Conditional coverage

1. concept:

Design enough test cases so that the possible values of each logical condition in each judgment statement in the tested program are met at least once.

It can also be described as follows:

Design enough test cases so that the possible values of each logical condition in the tested program are met at least once.

In this example, if (x> 0 & Y> 0) (marked as P1) And if (MAGIC <0) (marked as P2 ), there are three conditions in total: x> 0 (marked as C1), Y> 0 (marked as C2), and magic <0 (marked as C3 ).

2. Test cases:

Data C1 C2 C3 P1 P2 Path

{X = 3, y = 3}

T T T T F

A-B-e-f

{X =-3, y = 0}

F F F F T

A-c-d-F

All the values of the three conditions are met once. Therefore, the 100% conditions are covered.

3. Test adequacy:

The above test cases have also reached the 100% criteria for decision coverage, but it cannot be ensured that all test cases (groups) that meet the 100% criteria for coverage can reach the 100% criteria for decision coverage, see the following example:

Data C1 C2 C3 P1 P2 Path

{X = 3, y = 0}

T F T F F

A-c-e-f

{X =-3, y = 5}

F T F F F

A-c-e-f

Since the coverage criteria cannot reach 100% of the coverage criteria, it may not necessarily reach 100% of the statement coverage criteria.

 

4. Decision-condition coverage (branch-condition coverage)

1. concept:

A sufficient number of test cases are designed so that each judgment result (true or false) in the tested program can be met at least once, and the possible values of each logical condition can be met at least once. That is, it meets the criteria of both 100% judgment coverage and 100% condition coverage.

2. Test cases:

Data C1 C2 C3 P1 P2 Path

{X = 3, y = 3}

T T T T F

A-B-e-f

{X =-3, y = 0}

F F F F T

A-c-d-F

The values of all conditions are met once, and the results of all judgments are met once.

3. Test adequacy:

Reaching the 100% criterion-the coverage criterion will certainly be able to reach 100% criterion coverage, 100% criterion coverage, and 100% statement coverage.

 

5. Condition combination coverage

1. concept:

Design enough test cases so that the combinations of all possible condition values in each judgment in the tested program are met at least once.

Note:

A. Condition combination is only applicable to multiple conditions in the same judgment statement, so that the values of these conditions are combined by Cartesian product.

B. There is no need to combine the condition values in different judgment statements.

C. For Single-condition judgment statements, you only need to satisfy all your values.

2. Test cases:

Data C1 C2 C3 P1 P2 Path

{X =-3, y = 0}

F F F F F

A-c-e-f

{X =-3, y = 2}

F T F F F

A-c-e-f

{X =-3, y = 0}

T F F F F

A-c-e-f

{X = 3, y = 3}

T T T T T

A-B-d-F

C1 and c2 are in the same judgment statement, and all their values are combined once.

3. Test adequacy:

100% meeting the condition combination criteria must meet the 100% condition coverage criteria and 100% judgment coverage criteria.

However, in the above example, only two paths a-c-e-f and a-B-d-F are taken, and the program in this example has three paths.

 

6. Path coverage

1. concept:

Design enough test cases so that each path in the test program is overwritten at least once.

2. Test cases:

Data C1 C2 C3 P1 P2 Path

{X = 3, y = 5}

T T T T T

A-B-d-F

{X = 0, y = 2}

F T T F T

A-c-d-F

This path is not possible

A-B-e-f

{X =-8, y = 3}

F T F F F

A-c-e-f

All possible paths are met once.

3. Test adequacy:

As can be seen from the table above, 100% meets the path coverage, but not necessarily 100% meets the condition coverage (C2 only gets true ), but it must meet the decision coverage standard by 100% (because the path is taken from a branch of the decision)

 

7. Strong/weak logical coverage

In many tutorials, we think that the six logic overwrites the weak to strong sorting order:

Statement overwrite-> Decision overwrite-> condition overwrite-> Decision-condition overwrite-> path Overwrite

However, after the above analysis, the relationships between them can be expressed as follows:

The path overwrite is hard to represent in this figure.

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.