IS 4500 WHITE BOX TESTING Copyright 2012 by

IS 4500 WHITE BOX TESTING Copyright © 2012 by The Cathris Group and Martin J. Schedlbauer. All Rights Reserved. Do Not Duplicate or Distribute Without Written Consent of the Author. www. cathris. com · info@cathris. com

Objectives �Upon completion of this chapter you will be able to: ◦ Create test cases based on statement and branch coverage ◦ Calculate statement and branch coverage metrics 2 Solution Validation & Testing v 1. 01

What is White Box Testing? �The basis for white box testing is the test object’s source code. �It is a code-based testing technique and therefore the source code must be available and it must be possible to manipulate the code by adding test “hooks”. �The general idea is to exercise every part of the code of the test object at least once.

White Box Testing Techniques �Flow-oriented test cases that represent the program logic are identified and all paths are executed. �The expected result is generally what the programmer wrote not necessarily what the requirements stated.

What is Tested? �The overall goal of WBT is to achieve coverage of the entire code base: ◦ ◦ Statements Decisions and branches Conditions Paths

Statement Testing �This analysis focuses on each statement of the test object. �Code is first translated into a control flow graph: ◦ Branches ◦ Sequential statements are a single block as statement execution is guaranteed ◦ All statements must be executed

Test Cases �Map out the sequence of control and each path becomes a test case. �Ensure that all paths are covered.

C 0 Test Completion Criterion �The completion criteria for tests can be defined by the following metric: Statement Coverage = (number of executed statements / total number of statements) * 100% � 100% coverage is difficult to achieve and may be too expensive. ◦ For example, how would you trigger all exception handling catches?

Dead Code Analysis �If complete coverage of all statements is required and some statements cannot be triggered, then that means there is code that is unreachable (“dead code”).

Branch Testing & Coverage �In this form of testing, the execution of each statement is not considered. �Instead focus is directed to the execution of branches (decisions). �Testing should ensure that each branch is taken following a decision. �For loops testing should ensure execuction of the body as well as fallthrough.

C 1 Test Completion Criterion �Analogous to the statement coverage, the degree of coverage for branch coverage is defined as: Branch Coverage = (number of executed branches / total number of branches) * 100%

Code Analysis Example 1 private static Point 2 D. Double calc. Path. Line(Point s, Point e) { double m = 0. 0, b = 0. 0; m = (double)(e. y - s. y) / (double)(e. x - s. x); if (Double. is. Infinite(m)) if (m < 0. 0) m = (-1. 0) * Double. MAX_VALUE; else m = Double. MAX_VALUE; b = s. y - (m * s. x); if (Double. is. Infinite(b)) if (b < 0. 0) b = (-1. 0) * Double. MAX_VALUE; else b = Double. MAX_VALUE; return new Point 2 D. Double(m, b); }

Code Analysis Example 2 double calc_price ( double baseprice, double specialprice, double extraprice, int extras, double discount) { double addon_discount; double result; if (extras >= 3) addon_discount = 10; else if (extras >= 5) addon_discount = 15; else addon_discount = 0; if (discount > addon_discount) addon_discount = discount; result = baseprice / 100. 0 * (100 – discount) + special_price + extraprice / 100. 0 * (100 – addon_discount) return (result) }

Control Flow Graph

Test Cases // test case 01 price = calc_price(10000. 0, 2000. 0, 1000. 0, 3, 0); test_ok = test_ok && (abs(price-12900. 0) < 0. 01; // test case 02 price = calc_price(25500. 0, 3450. 0, 6000. 0, 6, 0); test_ok = test_ok && (abs(price-34050. 0) < 0. 01; � Which edges in the control flow graph were executed? � What is the branch coverage? � Does running test 02 improve branch coverage? � What additional test cases do you need to improve branch coverage? � Which cases would you need to achieve 100% coverage?

Summary �In this module we learned that: ◦ White box testing complements black box testing 16 Solution Validation & Testing v 1. 01
- Slides: 16