Download Unit Testing Tools in Software Engineering: JUnit and JCoverage - Prof. Spiros Mancoridis and more Study notes Computer Science in PDF only on Docsity!
Dependable Software Systems
Topics in
Unit Testing Tools
Material drawn from [junit.org, jcoverage.com]
Courtesy Spiros Mancoridis
junit and jcoverage
- We will use a combination of two tools to test Java programs: - junit is a unit testing tool - jcoverage is a statement/branch coverage tool
- Both tools are available for free from the WWW.
- jcoverage has its own testing tool ( jtestrun ) but we will use junit as a testing harness. - junit is included in the jcoverage distribution.
Implementation of the SimpleStack class import java.util.Vector; class SimpleStack { private Vector s = null; public SimpleStack (int initSize) { s = new Vector(initSize); } public void push (Object o) { s.addElement(o); } public Object pop () { Object top = null; if (s != null && s.size() != 0) { top = s.elementAt(s.size()-1); s.removeElementAt(s.size()-1); } return top; } public Object top () { Object o = null; if (s != null && s.size() != 0) o = s.elementAt(s.size()-1); return o; } }
Testing the SimpleStack class using a junit StackTest class _import junit.framework.; import java.util.Vector; public class StackTest extends TestCase { private SimpleStack s; public static void main (String args[]) { junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(StackTest.class); } protected void setUp() { s = new SimpleStack(100); } public void testPushOne() { s = new SimpleStack(100); String hiString = new String("Hi"); s.push(hiString); assertTrue(s.top() == hiString); System.out.println(hiString); } public void testPopEmpty () { s = new SimpleStack(0); Object o = s.pop(); assertTrue(o == null); } public void testTopEmpty () { s = new SimpleStack(0); assertTrue(s.top() == null); } }_*
Run the StackTest class test cases in GUI mode
java junit.swingui.TestRunner StackTest
Coverage Tools
- Tools like jcoverage can be used to determine the degree of comprehensiveness of a test suite.
- This is important because a test suite may have many test cases, but may only cover a small percentage of the source code.
- Covering all source statements doesn’t guarantee much, but NOT covering them is a sign of trouble.
jcoverage byte code instrumentation
- java com.jcoverage.coverage.Instrument
[-ignore ignore-regex][-d destination-directory]
[@classlist-file...][classfiles...]
Directory where instrumented classes are written. If this
isn’t specified, the classes are instrumented in place
(overwrite mode).
Specifies the list of classes to be instrumented.
jcoverage byte code instrumentation
- The instrumentation program generates a new version of each class. - It is good practice to save the new versions of the
bytecode in a separate directory.
- Don’t instrument instrumented bytecode.
- The instrumentation program generates a jcoverage.ser file.
- Don’t delete this file before you execute the
instrumented code.
- The file contains a integer entry for each line of code
that is incremented when the code gets executed
(tested).
jcoverage HTML Report Generation
- java com.jcoverage.coverage.reporting.Main
[-i instrumentation-file][-o reports-directory]
[-s java-source-directory]
The instrumentation file to generate reports (i.e.,
jcoverage.ser )
Directory where HTML report is created
Directory containing the Java source code.
jcoverage HTML Report Generation
- Generates a multi-page coverage report in HTML from an instrumentation file. - Statement coverage percentage - Branch coverage percentage - Bar-chart visualization used
- The report has links to HTML versions of the source code: - Highlighted statements that were not executed. - Hit counts for statements that were executed multiple times.
HTML Coverage Report Source Code Annotations
Example invoking the jcoverage HTML Report Generation program
set SERFILE=c:\Documents and Settings\smancori\Calculator\jcovarage.ser
set SRCFILES=c:\Documents and Settings\smancori\Calculator
set OUTFILES=c:\Documents and Settings\smancori\Calculator\doc
java com.jcoverage.coverage.reporting.Main -i “%SERFILE%” -o “%OUTFILE” – s “%SRCFILES%”
- Execute this program from any directory.
- The HTML files for the report will be placed in the location specified by OUTFILES.
- SERFILE directs the program to the instrumentation file (i.e., jcoverage.jar )
jcoverage Merge Instrumentation program
- java com.jcoverage.tool.merge.Main
[-i instrumentation-file …]
[-o destination-directory]
- options:
- -i instrumentation-file …
The two or more instrumentation files to merge into a
unified instrumentation file (i.e., jcoverage.ser )
Directory where the unified instrumentation file is created.
jcoverage Merge Instrumentation program
- Merges two or more instrumentation files into a single instrumentation file.
- A single integrated report can be created from multiple coverage runs.
- The integrated report can then be fed to the Report Generation program to produce an integrated report.