Ch3 UnitTesting
Ch3 UnitTesting
Unit Testing
1
Outline of the Chapter
• Concept of Unit Testing
• Static Unit Testing
• Defect Prevention
• Dynamic Unit Testing
• Mutation Testing
• Debugging
• Unit Testing in eXtreme Programming
• Tools For Unit Testing
2
Concept of Unit Testing
• Static Unit Testing
– Code is examined over all possible behaviors that might arise during run
time
– Code of each unit is validated against requirements of the unit by reviewing
the code
• Dynamic Unit Testing
– A program unit is actually executed and its outcomes are observed
– One observe some representative program behavior, and reach conclusion
about the quality of the system
• Static unit testing is not an alternative to dynamic unit testing
• Static and Dynamic analysis are complementary in nature
• In practice, partial dynamic unit testing is performed concurrently
with static unit testing
• It is recommended that static unit testing be performed prior to the
dynamic unit testing
3
Static Unit Testing
• In static unit testing code is reviewed by applying techniques:
– Inspection: It is a step by step peer group review of a work product, with each
step checked against pre-determined criteria
– Walkthrough: It is review where the author leads the team through a manual
or simulated executed of the product using pre-defined scenarios
• The idea here is to examine source code in detail in a systematic
manner
• The objective of code review is to review the code, and not to
evaluate the author of the code
• Code review must be planned and managed in a professional manner
• The key to the success of code is to divide and conquer
– An examiner inspect small parts of the unit in isolation
• nothing is overlooked
• the correctness of all examined parts of the module implies the
correctness of the whole module
4
Static Unit Testing (Code Review)
• Step 1: Readiness
– Criteria
• Completeness
• Minimal functionality
• Readability
• Complexity
• Requirements and design
documents
– Roles
• Moderator
• Author
• Presenter
• Record keeper
• Reviewers
• Observer
• Step 2: Preparation
– List of questions
– Potential Change Request (CR)
– Suggested improvement opportunities Figure 3.1: Steps in the code review process
5
Static Unit Testing (Code Review)
• Step 3: Examination A Change Request (CR) includes the
– The author makes a presentation following details:
– The presenter reads the code – Give a brief description of the issue
– The record keeper documents the CR – Assign a priority level (major or
minor) to a CR
– Moderator ensures the review is on
track – Assign a person to follow it up
• Step 4: Re-work – Set a deadline for addressing a CR
– Make the list of all the CRs
– Make a list of improvements
– Record the minutes meeting
– Author works on the CRs to fix the
issue
• Step 5: Validation
– CRs are independently validated
• Step 6: Exit
– A summary report of the meeting
minutes is distributes
6
Static Unit Testing (Code Review)
7
Static Unit Testing (Code Review)
• The code review methodology can be applicable to review other documents
• Five different types of system documents are generated by engineering department
– Requirement
– Functional Specification
– High-level Design
– Low-level Design
– code
• In addition installation, user, and trouble shooting guides are developed by
technical documentation group
10
Dynamic Unit Testing
Selection of test data is broadly based on the following techniques:
• Control flow testing
– Draw a control flow graph (CFG) from a program unit
– Select a few control flow testing criteria
– Identify a path in the CFG to satisfy the selection criteria
– Derive the path predicate expression from the selection paths
– By solving the path predicate expression for a path, one can generate the data
• Data flow testing
– Draw a data flow graph (DFG) from a program unit and then follow the
procedure described in control flow testing.
• Domain testing
– Domain errors are defined and then test data are selected to catch those faults
• Functional program testing
– Input/output domains are defined to compute the input values that will cause
the unit to produce expected output values
11
Mutation Testing
• Modify a program by introducing a single small change to the code
• A modified program is called mutant
• A mutant is said to be killed when the execution of test case cause it
to fail. The mutant is considered to be dead
• A mutant is an equivalent tot the given program if it always produce
the same output as the original program
• A mutant is called killable or stubborn, if the existing set of test
cases is insufficient to kill it
• A mutation score for a set of test cases is the percentage of non-
equivalent mutants killed by the test suite
• The test suite is said to be mutation-adequate if its mutation score is
100%
12
Mutation Testing
Mutation testing makes two major assumptions:
• Coupling effects
– Complex faults are coupled to simple faults in such a way that a test suite
detecting simple faults in a program will detect most of the complex faults
13
Debugging
• The process of determining the cause of a failure is known as
debugging
• It is a time consuming and error-prone process
• Debugging involves a combination of systematic evaluation,
intuition and a little bit of luck
• The purpose is to isolate and determine its specific cause, given a
symptom of a problem
• There are three approaches to debugging
– Brute force
– Cause elimination
• Induction
• Deduction
– Backtracking
14
Unit Testing in eXtreme Programming
15
Unit Testing in eXtreme Programming
Three laws of Test Driven development (TDD)
• One may not write production code unless the first failing unit test is
written
• One may not write more of a unit test than is sufficient to fail
• One may not write more production code than is sufficient to make
the failing unit test pass
Pair programming:
– In XP code is being developed by two programmers working side by side
– One person develops the code tactically and the other one inspects it
methodically by keeping in mind the story they are implementing
16
JUnit – A Framework for Unit Testing
• JUnit: It is a framework for performing unit testing of Java
programs.
– Other frameworks: NUnit (C#), CPPUnit (C++), fUnit (Fortran)
• Intuitive steps to test a method in Java (Ex. Move() method of
PlanetClass)
– Create an object instance of PlanetClass. Call it Mars.
– Select values of all input parameters of Move().
– Compute the expected value to be returned by Move(). Let it be y.
– Execute method Move() on Mars with the selected input values.
• Let Move() return a value called z.
– Compare the actual output (z) returned by Move() with the expected value (y).
• If (z == y), Move() passes the test; otherwise it fails. Report the result.
• JUnit makes writing of test cases easier. Next slide …
17
JUnit – A Framework for Unit Testing
• JUnit provides a basic class called TestCase.
• The tester
– Extends the TestCase class for each test case. 10 extensions for 10 test cases.
– Alternatively, extend TestCase to have 10 methods for 10 test cases.
• The TestCase class provides methods to make assertions.
– assertTrue(Boolean condition)
– assertFalse(Boolean condition)
– assertEquals(Object expected, Object actual)
– assertEquals(int expected, int actual)
– assertEquals(double expected, double actual, double tolerance)
– assertSame(Object expected, Object actual)
– assertNull(Object testobject)
– …
• The tester can have her own assertions.
18
JUnit – A Framework for Unit Testing
• Each assertion accepts an optional first parameter of type String; if
the assertion fails, the string is displayed. Help for the tester…
• The assertEquals() method displays a message upon failure.
• junit.framework.AssertionFailedError: expected: <x> but was: <y>
• Note that only failed tests are reported.
• The following shows how assertTrue() works.
19
JUnit – A Framework for Unit Testing
import TestMe; // TestMe is the class whose methods are going to be tested.
import junit.framework.*; // This contains the TestCase class.
20
Tools For Unit Testing
• Code auditor
– This tool is used to check the quality of the software to ensure that it meets
some minimum coding standard
• Bound checker
– This tool can check for accidental writes into the instruction areas of memory,
or to other memory location outside the data storage area of the application
• Documenters
– These tools read the source code and automatically generate descriptions and
caller/callee tree diagram or data model from the source code
• Interactive debuggers
– These tools assist software developers in implementing different debugging
techniques
Examples: Breakpoint and Omniscient debuggers
• In-circuit emulators
– It provides a high-speed Ethernet connection between a host debugger and a
target microprocessor, enabling developers to perform source-level debugging
21
Tools for Unit Testing
• Memory leak detectors
– These tools test the allocation of memory to an application which request for memory
and fail to de-allocate memory
• Static code (path) analyzer
– These tool identify paths to test based on the structure of code such as McCabe’s
cyclomatic complexity measure
22
Tools for Unit Testing
• Software inspection support
– Tools can help schedule group inspection
• Test coverage analyzer
– These tools measure internal test coverage, often expressed in terms of control
structure of the test object, and report the coverage metric
• Test data generator
– These tools assist programmers in selecting test data that cause program to
behave in a desired manner
• Test harness
– This class of tools support the execution of dynamic unit tests
• Performance monitors
– The timing characteristics of the software components be monitored and
evaluate by these tools
• Network analyzers
– These tools have the ability to analyze the traffic and identify problem areas
23
Tools for Unit Testing
• Simulators and emulators
– These tools are used to replace the real software and hardware that are not
currently available. Both the kinds of tools are used for training, safety, and
economy purpose
• Traffic generators
– These produces streams of transactions or data packets.
• Version control
– A version control system provides functionalities to store a sequence of
revisions of the software and associated information files under development
24