Coverage tools Program is typically compiled with special options, to add extra source or object code. –Additional data structures, such as a flow graph,

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Introducing JavaScript
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Computer Science 2212a/b - UWO1 Structural Testing Motivation The gcov Tool An example using gcov How does gcov do it gcov subtleties Further structural.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Testing an individual module
Chapter 6 Control Statements Continued
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Agenda Introduction Overview of White-box testing Basis path testing
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Introduction to Software Testing Chapter 8.1 Building Testing Tools –Instrumentation Paul Ammann & Jeff Offutt
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 6 Control Statements Continued
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 Test Coverage Coverage can be based on: –source code –object code –model –control flow graph –(extended) finite state machines –data flow graph –requirements.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
© Dr. A. Williams, Fall Present Software Quality Assurance – Clover Lab 1 Tutorial / lab 2: Code instrumentation Goals of this session: 1.Create.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 14 Throwing Custom Exceptions
Dept of Computer Science University of Maryland College Park
Why should we test? How should we test?
Testing and Debugging.
Computer Programming I
Designing and Debugging Batch and Interactive COBOL Programs
MSIS 655 Advanced Business Applications Programming
Topics Introduction to File Input and Output
Structured Program
Using C++ Arithmetic Operators and Control Structures
Topics Introduction to File Input and Output
Paul Ammann & Jeff Offutt
Plug-In Architecture Pattern
Presentation transcript:

Coverage tools Program is typically compiled with special options, to add extra source or object code. –Additional data structures, such as a flow graph, may also be created. Program is run, possibly via test cases –During execution, information is accumulated and written to an output file. Post-processing phase: –User report is generated from output file.

How to measure coverage? Possibilities: –Instrument the source code before compilation –Instrument the virtual machine or object code. –The Apache Byte Code Engineering Library (BCEL) is used by several Java coverage tools – –Use a customized virtual machine.

Coverage filters Normally, a filtering function is provided to determine for which classes coverage is to be measured: –Include or exclude specified packages –Filter out calls to provided class libraries or framework. –Exclude certain source code statements –Example: When a Java class is not intended to have instances created, the default constructor is often set to be private. This would show up as uncovered code.

Source code instrumentation Create a copy of the source code, instrument the copy, and compile the instrumented copy to.class files. –This is the approach used by Clover. Typical method: –Set up an array of counters. –Insert source code at points of interest that increment specific counters. –Coverage tool calls main() method –After main() method returns, dump values of counters.

Source code before instrumentation public final void add( final MatrixRow newValues ) { if ( getNumColumns( ) == 0 ) { contents = newValues.copy( ).getContents( ); } else { contents.addAll( newValues.copy( ).getContents( ) ); } A counter is inserted for each method, statement, and branch. Statement counters are before each statement, in case the statement throws an exception. Branch counters need to identify true and false branches taken.

Same code after instrumentation public final void add( final MatrixRow newValues ) { CLOVER.M[348]++; CLOVER.S[1814]++; if ( ( getNumColumns( )==0 && ++CLOVER.CT[338]!=0 | true ) || ( ++CLOVER.CF[338] == 0 & false ) ) { CLOVER.S[1815]++; contents = newValues.copy( ).getContents( ); } else { CLOVER.S[1816]++; contents.addAll( newValues.copy( ).getContents( ) ); } method counter statement counters branch counters

Counting branches Count number of times that a branch condition evaluates to true and false. if ( ( aBoolean && ++CT[338]!=0 | true ) || ( ++CF[338] == 0 & false ) ) If aBoolean is true, then the && is also evaluated, which will increment the CT counter. We want the expression’s overall evaluation to be true. If aBoolean is false, then execution switches to the right side of the || clause. Increment the CF counter, but then ensure that the entire expression still evaluates to false to take the else branch. Short-circuit evaluation means that as soon as the expression result can be deterimined, evaluation stops. This avoids: –Incrementing the true counter if aBoolean is false. –Incrementing the false counter if aBoolean is true.

Analysis Advantages: –Results are directly mapped to source code. –If a statement throws an exception, it will be counted as having been executed. Disadvantages: –Requires keeping a second version of source code, and ensuring that original source is not over-written. –Instrumented code must not affect original code execution. –Management of.class files from compiling alternate source code. –Memory usage: for statement coverage alone, every statement requires one integer as a counter.

Byte code instrumentation Can be done in two ways: –Instrument object files in advance to produce a alternate version. –Example: create instrumented.class files for Java. –Instrument object files as they are used. –Example: create customized.class file loader that inserts the instrumentation at run time.

Analysis Pre-instrumentation: –Advantages: –source code is not modified. –can be done as a batch process before execution –Disadvantages: –management of two.class file versions. –mapping of source code statements to/from object code –counting source code statements can be difficult –object code may need extra information such as special compiler options

Clover –Commercial tool: requires license Supports method, statement, and branch coverage Reports its findings in multiple formats –From project level down to individual lines of source code Shows list of test cases that hit each line of course code Historical charting of code coverage and other metrics Plug-in versions available for Eclipse, NetBeans

Clover plugin for Eclipse Image source: atlassian.com

Clover coverage filters One can choose not to instrument certain types of blocks in the code (e.g. assertions and exception catching), in order to focus on the coverage of interest.

Emma Open-source tool –Supports class, method, “basic block”, and line coverage. –“Fractional” line coverage supported, but not branch coverage. –Standalone version works with Ant builds – –Eclipse plugin EclEmma also available –

Emma Uses byte code instrumentation. –Classes can be instrumented in advance, or during class loading. Tool keeps a metadata file to associate byte code with source code –A regular compile will allow for only limited metadata. –A compile with debug option will provide more information to support branch coverage.

Block Coverage Block coverage assumes that if a block of statements without branches is entered and exited, all statements in the block were executed. –That is, the counter is at the end of the block, instead of before the source code statement. Result: If an exception occurs in the block, the entire block is not recorded as having executed. –This may be fine for application source code, but it does not work well with JUnit test source code or in code for which exceptions are commonplace. –JUnit throws exceptions internally when tests fail, so the test may not have appeared to be executed.

Emma coverage report

Emma source code annotations

Fractional line coverage Only part of conditional executed Loop increment not executed

Anomalies with byte code coverage There are cases where the compiler may emit multiple copies of object code for a single instance of source code. In the above example, there is an instance variable initialized for each of two constructors. However, only one constructor is called. The result is the yellow annotation at initialization statement 17.

Coverlipse Open source Eclipse plug-in – Provides block coverage, all-uses data flow coverage –No branch coverage –Uses Apache BCEL to instrument bytecode Appears to be dormant – latest version is compatible with Eclipse Java 6 may also be an issue

Line coverage report

All-uses coverage report

CodeCover Open source Eclipse plug in –Web site: –Version available Jan Performs source-code instrumentation to obtain: –Statement coverage –Branch coverage –Loop coverage: loop executed zero/once/many times. –Multiple-condition coverage (called “strict condition” coverage by CodeCover)

CodeCover summary report

CodeCover detailed report

CodeCover features Boolean value analyzer: shows how many Boolean combinations in conditions have been covered. Code “hot spots”: highlighting of code that is executed more frequently than most. Test correlation matrix: for each pair of test cases, the overlap in coverage for the two test cases is shown. Source instrumentation is done following a statement. –The following statements do not appear as coverable items: –throw statements –return statements –Methods that throw exceptions are marked as not covered.

CodeCover Boolean analyzer For the compound condition shown, combinations of atomic conditions that have occurred are shown. –The x shows a short-circuit evaluation.

CodeCover Correlation view The colours give an indication of the overlap for pairs of test cases. –The selected square shows that for these two test cases, they have 24 and 25 coverable items, and that 19 are shared, for an overlap of 88.1%

Cobertura Open source tool for Java code coverage – Measures line coverage, branch coverage, and McCabe cyclomatic complexity metric for methods. –Branch coverage requires compilation with debug option. Must be run as an Ant task, with four stages –Compile source code –Instrument classes –Run code –Generate report

Cobertura summary report

Cobertura detail report

Code Coverage: Benefits If you haven’t exercised part of the code, it could contain a bug. Can be used to locate “dead” or unexecutable code. –WARNING: Be very careful when removing code, if the purpose is to test for some exceptional condition that is difficult to trigger.

Code Coverage: Caveats Coverage tests that the code has been exercised, and not that you built what the customer wanted. If the logical structure of the code contains errors such as a missed case in a switch statement, the absence of code will not be detected. 100% coverage is often unrealistic, especially for fine- grained coverage measures –Effort to achieve high coverage may be unjustified in terms of: –time spent –producing unreliable test cases –80% coverage is a “rule of thumb” often used in industry

References Clover: Emma: – – Coverlipse: CodeCover: Cobertura: A. Glover, “Don’t be fooled by the Coverage Report”, IBM developer works article S. Gornett, “Code Coverage Analysis” –