Download presentation
Presentation is loading. Please wait.
Published byMilton Leonard Modified over 9 years ago
1
Software Engineering Modern Approaches Eric Braude and Michael Bernstein 1
2
Chapter 26: Unit Testing 2
3
Learning Goals of This Chapter What parts of the code should be subjected to unit testing? How do you go about unit testing? What is statement coverage? Branch coverage? Path coverage? How does equivalence partitioning help in selecting test cases? How do you use stubs when the unit under test requires other – but unbuilt – units? How do you use JUnit? What is test-driven development? How is unit testing done in case studies? Requirements analysis Design Implementation Testing Maintenance Planning The Software Development Lifecycle Phase most relevant to this chapter is shown in bold 3
4
The Source of a Unit’s Specification Requirement document (SRS) Design document (SDD) Implementation unit (code for method or class) Implementation-time specification Key: “specifies” Example: “It shall be possible to add a DVD to the video store’s inventory” Example: method addDVD( … ) “Preconditions: …. Postconditions: …” Example: void setDVDName( … ) Example: get DVD title Domain classes © 2010 John Wiley & Sons Ltd. 4
5
Relating Tests to Requirements & Design (1) D-Requirements 3.2.EC.1.2 Qualities of Encounter characters Every game character has the same set of qualities. Each quality shall be a non- negative floating point number with at least one decimal of precision.... Characters GameCharacter EncounterCharacter adjustQuality() (2) Design GameCharacter: An abstract class with attribute name...... against this requirement Test this method... Test this class...... against this requirement Encounter Characters © 2010 John Wiley & Sons Ltd. 5
6
Unit Test Methods White-Box Methods Statement coverage Test cases cause every line of code to be executed Branch coverage Test cases cause every decision point to execute Path coverage Test cases cause every independent code path to be executed Black-Box Methods Equivalence partitioning Divide input values into equivalent groups Boundary Value Analysis Test at boundary conditions © 2010 John Wiley & Sons Ltd. 6
7
Program Control Graph of computeFine() False True 1 2 3 4 5 6 7 False © 2010 John Wiley & Sons Ltd. 7
8
Branch Coverage of computeFine() Test Case #1 False True 1 2 3 4 5 6 7 False True Test Case #2 False True 1 2 3 4 5 6 7 False True © 2010 John Wiley & Sons Ltd. 8
9
Distinct Paths of computeFine() Path #1 False True 1 2 3 4 5 6 7 False True Path #3 False True 1 2 3 4 5 6 7 False True Path #2 False True 1 2 3 4 5 6 7 False True Path #4 False True 1 2 3 4 5 6 7 False True © 2010 John Wiley & Sons Ltd. 9
10
Test Cases for Distinct Paths of computeFine() Test Case #daysLateprintONPath 11TRUE1-2-3-4-5-6 7 260FALSE1-2-4-5--7 31FALSE1-2-3-4-5-7 460TRUE1-2-4-5-6-7 © 2010 John Wiley & Sons Ltd. 10
11
Program Control Graph for computeFines() False True 1 2 3 5 6 10 4 8 9 7 True False © 2010 John Wiley & Sons Ltd. 11
12
Basis Paths for computeFines() Basis Path #1 False True 1 2 3 5 6 10 4 8 9 7 True False Basis Path #2 False True 1 2 3 5 6 10 4 8 9 7 True False Basis Path #4 False True 1 2 3 5 6 10 4 8 9 7 True False Basis Paths #3 False True 1 2 3 5 6 10 4 8 9 7 True False © 2010 John Wiley & Sons Ltd. 12
13
Parameter Space of computeFine() 051015-5 old releases one-of-a-kind new releases Days late e.g., (6 days late, “Casablanca”) Each element represents a pair: ( Days late, Movie name ) © 2010 John Wiley & Sons Ltd. 13
14
0 new release Days late 5 One Equivalence Partition of computeFine() © 2010 John Wiley & Sons Ltd. 14
15
Equivalence Partitions of computeFine() Method 01015-5 old release one-of-a-kind new release Days late 5 New release DVD’s 6-15 days late © 2010 John Wiley & Sons Ltd. 15
16
Using Equivalence Partitions to Create Test Cases Test … … values strictly within each region … values at region borders Notes: Include all illegal regions Within each constraint, select randomly –E.g., With “new release” input, select lateness days at random betw. 6 & 15, excluding 6 & 15 © 2010 John Wiley & Sons Ltd. 16
17
RentalItem neededByRent() Using Stubs to Test a Method Rental rent() RentalCustomer neededByRent() RentalItem neededByRent() for testing dependence Rental rent() RentalCustomer neededByRent() stubbed © 2010 John Wiley & Sons Ltd. 17
18
Partitioning adjustQuality() Top Level © 2010 John Wiley & Sons Ltd.
19
Partitioning adjustQuality(): “Within Range” © 2010 John Wiley & Sons Ltd.
20
Partitioning adjustQuality(): On Boundary © 2010 John Wiley & Sons Ltd.
21
Test-Driven Development 1, 2. Write a test case (will fail) 3. Write/fix code to pass test 4. Run test case Test passes, more tests to be written Test passes, no more tests to be written Test fails © 2010 John Wiley & Sons Ltd. 21
22
Output of JUnit for a Simple Test © 2010 John Wiley & Sons Ltd. 22
23
Red Output of JUnit for a Simple Test © 2010 John Wiley & Sons Ltd. 23
24
Partitioning adjustQuality() Out of Range © 2010 John Wiley & Sons Ltd.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.