CS 240 – Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Black Box Testing Sources: Code Complete, 2 nd Ed., Steve McConnell Software Engineering, 5 th Ed., Roger Pressman Testing Computer Software, 2 nd Ed.,
SOFTWARE TESTING. INTRODUCTION  Software Testing is the process of executing a program or system with the intent of finding errors.  It involves any.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
White Box Testing Sources: Code Complete, 2 nd Ed., Steve McConnell Software Engineering, 5 th Ed., Roger Pressman.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
Black box testing  Black box tests focus on the input/output behavior of the component  Black-box tests do not deal with the internal aspects of the.
Testing an individual module
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
1 Software Testing Techniques CIS 375 Bruce R. Maxim UM-Dearborn.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Software Testing Sudipto Ghosh CS 406 Fall 99 November 9, 1999.
TESTING.
CMSC 345 Fall 2000 Unit Testing. The testing process.
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP.
Path Testing + Coverage Chapter 9 Assigned reading from Binder.
Coverage – “Systematic” Testing Chapter 20. Dividing the input space for failure search Testing requires selecting inputs to try on the program, but how.
Testing, Bug Fixing and Debugging the Code Yordan Dimitrov Telerik Corporation
Agenda Introduction Overview of White-box testing Basis path testing
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
White-box Testing.
CPS120: Introduction to Computer Science Decision Making in Programs.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Software Engineering 2004 Jyrki Nummenmaa 1 BACKGROUND There is no way to generally test programs exhaustively (that is, going through all execution.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Theory and Practice of Software Testing
SOFTWARE TESTING. Introduction Software Testing is the process of executing a program or system with the intent of finding errors. It involves any activity.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Software Testing. SE, Testing, Hans van Vliet, © Nasty question  Suppose you are being asked to lead the team to test the software that controls.
Testing Data Structures
Software Engineering (CSI 321)
Information and Computer Sciences University of Hawaii, Manoa
CS240: Advanced Programming Concepts
Software TestIng White box testing.
Software Testing.
Java Language Basics.
Software Testing.
Software Testing.
The Joy of Breaking Code Testing Logic and Case Selection
Black Box Testing PPT Sources: Code Complete, 2nd Ed., Steve McConnell
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CompSci 230 Software Construction
EGR 2261 Unit 4 Control Structures I: Selection
Lecture 13 & 14.
Structural testing, Path Testing
Types of Testing Visit to more Learning Resources.
CHAPTER 4 Test Design Techniques
UNIT-4 BLACKBOX AND WHITEBOX TESTING
MSIS 655 Advanced Business Applications Programming
More Selections BIS1523 – Lecture 9.
Chapter 4: Control Structures I (Selection)
Software Testing (Lecture 11-a)
Chapter 14 Software Testing Techniques
CS240: Advanced Programming Concepts
CS240: Advanced Programming Concepts
White-Box Testing Techniques I
Control Structure Testing
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Black-Box Testing Techniques III
For loops Taken from notes by Dr. Neil Moore
Chapter 3: Selection Structures: Making Decisions
Review of Previous Lesson
Problem 1 Given n, calculate 2n
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Presentation transcript:

CS 240 – Advanced Programming Concepts White Box Testing CS 240 – Advanced Programming Concepts

White Box Testing Looking at the class's internal implementation, in addition to its inputs and expected outputs, enables you to test it more thoroughly Testing that is based both on expected external behavior and knowledge of internal implementation is called "white box testing"

White Box Testing White box testing is primarily used during unit testing Unit testing is usually performed by the engineer who wrote the code In rare cases an independent tester might do unit testing on your code

Complete Path Coverage Test ALL possible paths through a subroutine What test cases are needed to achieve complete path coverage of this method? (white-box-example.java) Some paths may be impossible to achieve. Skip those paths  Often there are too many paths to test them all, especially if there are loops in the code. In this case, we use less complete approaches: Line coverage Branch coverage Condition testing Loop testing

Line Coverage At a minimum, every line of code should be executed by at least one test case What test cases are needed to achieve complete line coverage of this subroutine? (white-box-example.java) Developers tend to significantly overestimate the level of line coverage achieved by their tests Coverage tools (like Cobertura) are important for getting a realistic sense of how completely your tests cover the code Complete line coverage is necessary, but not sufficient

Branch Coverage Similar to line coverage, but stronger Test every branch in all possible directions If statements test both positive and negative directions Switch statements test every branch If no default case, test a value that doesn't match any case Loop statements test for both 0 and > 0 iterations

Branch Coverage What test cases are needed to achieve complete branch coverage of this subroutine? (white-box-example.java) Why isn't branch coverage the same thing as line coverage?

Branch Coverage What test cases are needed to achieve complete branch coverage of this subroutine? (white-box-example.java) Why isn't branch coverage the same thing as line coverage? Consider an if with no else, or a switch with no default case Line coverage can be achieved without achieving branch coverage

Complete Condition Testing For each compound condition, C Find the simple sub-expressions that make up C Simple pieces with no ANDs or ORs Suppose there are n of them Create a test case for all 2n T/F combinations of the simple sub-expressions If (!done && (value < 100 || c == 'X')) … Simple sub-expressions !done, value < 100, c == 'X' n = 3 Need 8 test cases to test all possibilities

Complete Condition Testing Use a “truth table” to make sure that all possible combinations are covered by your test cases Doing this kind of exhaustive condition testing everywhere is usually not feasible Some combinations might be impossible to achieve (omit these cases, since they are impossible) !done value < 100 c == ‘X’ Case 1: False Case 2: True Case 3: Case 4: Case 5: Case 6: Case 7: Case 8:

Partial Condition Testing A partial, more feasible approach For each condition, C, test the True and False branches of C and every sub-expression (simple or not) within C, but not all possible combinations If (!done && (value < 100 || c == 'X')) … (!done && (value < 100 || c == 'X')), both T and F (value < 100 || c == 'X'), both T and F !done, both T and F value < 100, both T and F c == 'X', both T and F One test case may cover several of these, thus reducing the number of required test cases

Partial Condition Testing Example Cases (!done && (value < 100 || c == 'X')), both T and F [FALSE] done == true (value and c are not reachable and can be any value) [TRUE] done == false AND value == 99 (c is not reachable and can be any value) (value < 100 || c == 'X'), both T and F [TRUE] Already covered by 2 [FALSE] done == false AND value == 100 AND c == ‘Y’ !done, both T and F [FALSE] Already covered by 1 value < 100, both T and F [FALSE] Already covered by 3 c == 'X', both T and F [TRUE] done == false AND value == 100 AND C == ‘X’

Partial Condition Testing Cases covered by partial condition testing example with short-circuit operators Cases 1, 3, 4, 7 covered as far as reachable by example case 1 Cases 5, 8 covered as far as reachable by example case 2 Case 2 covered by example case 3 Case 6 covered by example case 4 All cases covered as far as reachable by 4 test cases !done value < 100 c == ‘X’ Case 1 (Ex. 1): False Case 2 (Ex. 3): True Case 3 (Ex. 1): Case 4 (Ex. 1): Case 5 (Ex. 2): Case 6 (Ex. 4): Case 7 (Ex. 1): Case 8 (Ex. 2):

Partial Condition Testing without Short-Circuit Operators Example Cases (!done & (value < 100 | c == 'X')), both T and F [FALSE] done == true AND value == 100 AND c == ‘Y’ [TRUE] done == false AND value == 99 AND c == ‘X’ (value < 100 | c == 'X'), both T and F [TRUE] Already covered by 2 [FALSE] Already covered by 1 !done, both T and F value < 100, both T and F c == 'X', both T and F [TRUE] Already covered by case 2

Partial Condition Testing Cases covered by partial condition testing example Case 1 covered by example case 1 Case 8 covered by example case 2 Previous definition of partial coverage satisfied by only 2 test cases, but most possible cases not covered at all Sometimes we need something between complete and partial In practice, we expand the definition of partial to include other reasonable cases based on equivalence classes !done value < 100 c == ‘X’ Case 1 (Ex 1): False Case 2: True Case 3: Case 4: Case 5: Case 6: Case 7: Case 8 (Ex 2):

Partial Condition Testing without Short-Circuit Operators Back to equivalence partitioning: Consider equivalence classes that cause the whole statement to be true: done == false AND value < 100 done == false AND value >= 100 AND c == ‘X’ Consider equivalence classes that cause the whole statement to be false: done == true done == false AND value >= 100 AND c != ‘X’ Which equivalence classes do our two cases cover? [FALSE] done == true AND value == 100 AND c == ‘Y’ [TRUE] done == false AND value == 99 AND c == ‘X’ What boundary cases should we add?

What test cases do we need to achieve // Compute Net Pay totalWithholdings = 0; for ( id = 0; id < numEmployees; ++id) { // compute social security withholding, if below the maximum if ( m_employee[ id ].governmentRetirementWithheld < MAX_GOVT_RETIREMENT) { governmentRetirement = ComputeGovernmentRetirement( m_employee[ id ] ); } // set default to no retirement contribution companyRetirement = 0; // determine discretionary employee retirement contribution if ( m_employee[ id ].WantsRetirement && EligibleForRetirement( m_employee[ id ] ) ) { companyRetirement = GetRetirement( m_employee[ id ] ); grossPay = ComputeGrossPay( m_employee[ id ] ); // determine IRA contribution personalRetirement = 0; if (EligibleForPersonalRetirement( m_employee[ id ] ) { personalRetirement = PersonalRetirementContribution( m_employee[ id ], companyRetirement, grossPay ); // make weekly paycheck withholding = ComputeWithholding( m_employee[ id ] ); netPay = grossPay - withholding - companyRetirement - governmentRetirement - personalRetirement; PayEmployee( m_employee[ id ], netPay ); // add this employee's paycheck to total for accounting totalWithholdings += withholding; totalGovernmentRetirement += governmentRetirement; totalRetirement += companyRetirement; SavePayRecords( totalWithholdings, totalGovernmentRetirement, totalRetirement ); Line coverage? Branch coverage? Complete condition testing? Partial condition testing?

Loop Testing Design test cases based on looping structure of the routine Testing loops Skip loop entirely One pass Two passes N-1, N, and N+1 passes [N is the maximum number of passes] M passes, where 2 < M < N-1

Loop Testing What test cases do we need? Skip loop entirely: public int readInputStreamAsChars(InputStream is, char[] buffer) throws IOException { int count = 0; while (count < buffer.length) { int c = is.read(); if (c == -1 || c == '\n') { break; } else { buffer[count++] = (char) c; } return count; What test cases do we need? Skip loop entirely: buffer.length == 0 Exactly one pass: Empty input stream OR buffer.length == 1 Exactly two passes: Input stream length 1 OR buffer.length == 2 N-1, N, and N+1 passes: buffer.length = stream length -2, stream length, and stream length + 1 M passes, where 2 < M < N-1 line of length buffer.length / 2 where buffer.length >= 3

Data Flow Testing The techniques discussed so far have all been based on "control flow" You can also design test cases based on "data flow“ (i.e., how data flows through the code) Some statements "define" a variable’s value (i.e., a “variable definition”) Variable declarations with initial values Assignments Incoming parameter values Some statements "use" variable’s value (i.e., a “variable use”) Expressions on right side of assignment Boolean condition expressions Parameter expressions

Data Flow Testing For every "use" of a variable Determine all possible places in the program where the variable could have been defined (i.e., given its most recent value) Create a test case for each possible (Definition, Use) pair

Data Flow Testing What test cases do we need? Definitions: x = a; if ( Condition 1 ) { x = a; } else { x = b; } if ( Condition 2 ) { y = x + 1; y = x – 1; What test cases do we need? Definitions: x = a; x = b; Uses: y = x + 1; y = x – 1; 1. (x = a, y = x + 1) 2. (x = b, y = x + 1) 3. (x = a, y = x – 1) 4. (x = b, y = x – 1)

Data Flow Testing Use data flow testing to design a set of test cases for this method: data-flow-example.java

Relational Condition Testing Testing relational sub-expressions (E1 op E2) ==, !=, <, <=, >, >= Three test cases to try: Test E1 == E2 Test E1 slightly bigger than E2 Test E1 slightly smaller than E2

Internal Boundary Testing Look for boundary conditions in the code, and create test cases for boundary – 1, boundary, boundary + 1 void sort(int[] data) { if (data.length < 30) { insertionSort(data); } else { quickSort(data); }

Internal Boundary Testing const int CHUNK_SIZE = 100; char * ReadLine(istream & is) { int c = is.get(); if (c == -1) { return 0; } char * buf = new char[CHUNK_SIZE]; int bufSize = CHUNK_SIZE; int strSize = 0; while (c != '\n' && c != -1) { if (strSize == bufSize - 1) { buf = Grow(buf, bufSize); bufSize += CHUNK_SIZE; buf[strSize++] = (char)c; c = is.get(); buf[strSize] = '\0'; return buf; What test cases do we need? Lines of length 99, 100, 101

Data Type Errors Scan the code for data type-related errors such as: Arithmetic overflow If two numbers are multiplied together, what happens if they're both large positive values? Large negative values? Is divide-by-zero possible? Other kinds of overflow If two strings are concatenated together, what happens if they're both unusually long Casting a larger numeric data type to a smaller one short s = (short) x; // x is an int Combined signed/unsigned arithmetic

Built-in Assumptions Scan the code for built-in assumptions that may be incorrect Year begins with 19 Age is less than 100 String is non-empty Protocol in URL is all lower-case What about "hTtP://..." or FTP://...?

Limitations of White Box Testing Whatever blind spots you had when writing the code will carry over into your white box testing Testing by independent test group is also necessary Developers often test with the intent to prove that the code works rather than proving that it doesn't work Developers tend to skip the more sophisticated types of white box tests (e.g., condition testing, data flow testing, loop testing, etc.), relying mostly on line coverage White box testing focuses on testing the code that's there. If something is missing (e.g., you forgot to handle a particular case), white box testing might not help you. There are many kinds of errors that white box testing won't find Timing and concurrency bugs Performance problems Usability problems Etc.