





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Testing
Agenda
code
guidelines
Test Cases
Inputs Expected outputs
1, 2, 3, 4 2.
1 1
1, 2, 3, 4, 5 3
Black box testing example
n You have to test a function that allegedly correctly categorizes triangles
n Prepare a list of test cases
Equilateral
Isosceles
Scalene
Unfeasible
Requirement Without Necessary Errors
(Myers) A function that tells whether three
numbers produce an equilateral triangle
(whose sides are all equal), an isosceles
triangle (containing exactly two equal
sides) or a scalene triangle (a triangle
which is neither equilateral nor
isosceles).
A function that tells whether a triplet of numbers produces:
(1) an equilateral triangle (whose sides are all greater than
zero and equal), in which case it outputs ‘E’ at the prompt,
or
(2) an isosceles triangle (whose sides are greater than zero,
exactly two of which are equal, and which form a
triangle), in which case it outputs ‘I’ at the system, or
(3) a scalene triangle (whose sides are all greater than
zero, which form a triangle, and which is neither
equilateral nor isosceles), in which case it outputs ‘S’ at
the prompt, or
(4) no triangle, in which case it outputs ‘N’ at the prompt.
… More Complete
*** The following sequences occur commonly:
/ Test C1: ge-aq-so / EncounterCharacter eC1M = new EncounterCharacter( "CharForTestC1" ); // method “ge” eC1M.adjustQuality(QUAL_STRENGTH, 40.0f ); // aq TestExecution.printReportToFile( outM, "Class test ge-aq-so", eC1M.sumOfQualities(), 100.0f ); // so
/ Test C2: ge-aq-aq-gq-so / EncounterCharacter eC2M = new EncounterCharacter( "CharForTestC2" ); // ge eC2M.adjustQuality(QUAL_STRENGTH, 40.0f ); // aq eC2M.adjustQuality(QUAL_STAMINA, 20.9876f ); // aq
Listing
page 4
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
TestExecution.printReportToFile( outM, "Class test ge-aq-aq-gq-so: part 1", eC2M.getQualityValue( QUAL_STAMINA ), 20.9876f ); // gq
TestExecution.printReportToFile( outM, "Class test ge-aq-aq-gq-so: part 2", eC2M.sumOfQualities(), 100.0f ); // so
*/ INVARIANT-ORIENTED TESTS
/ Conclude / outM.close(); System.out.println( "\nClass tests of EncounterChar class concluded." );
} // end of testEncounterCharacterClass
Listing
page 5 Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
/ Tests all the methods of this class one at a time
/ Tests for getEncounterCharacter() /
EncounterCharacter eCNorM = new EncounterCharacter( "qwerty" ); // normal TestExecution.reportToFile( outM, "GetCharacter Test 1: nominal value", eCNorM.getName(), "qwerty" );
EncounterCharacter eCNullM = new EncounterCharacter( null ); // null TestExecution.reportToFile( outM, "GetCharacter Test 2: null parameter", eCNullM.getName(), GameCharacter. DEFAULT_NAME);
Listing page 6
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
**String tooLongM = "1234567890123456789012345678901234567890"; EncounterCharacter eCTooLongM = new EncounterCharacter(tooLongM); // too long TestExecution.reportToFile( outM, "GetCharacter Test 3: Limit parameter values, "
EncounterCharacter eCZeroM = new EncounterCharacter( "" ); // zero-len TestExecution.reportToFile( outM, "GetCharacter Test 4: zero-length", eCZeroM .getName(), GameCharacter. DEFAULT_NAME);
EncounterCharacter eCPuncM = new EncounterCharacter( "a+b" ); // bad chars TestExecution.reportToFile( outM, "GetCharacter Test 5: bad char '+' ", eCPuncM .getName(), GameCharacter. DEFAULT_NAME);
Listing page 7
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
/ Tests for indexOf() for every valid quality name. / for( int i = 0; i < qualityTypeS.length; ++i ) try { TestExecution.reportToFile( outM, "indexOf() Test 1." + i + ": valid name: " + qualityTypeS[i], indexOf(qualityTypeS[i]), i ); } catch( Exception eP ) { TestExecution.reportToFile( outM, "indexOf() Test 1: valid name: compare ", "indexOf('" + qualityTypeS[i] + "')", "with expected " + i ); }
/ Tests for indexOf() for an invalid quality name. / try { TestExecution.reportToFile( outM, "indexOf() Test 2: invalid name: zorch", indexOf("zorch"), -1 ); } catch( Exception eP ) { TestExecution.reportToFile( outM, "indexOf() Test 2: valid name: compare ", "indexOf("zorch")", "with expected -1" ); }
Listing page 8
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
/ Tests for setQuality() /
// Set up for test EncounterCharacter hank = new EncounterCharacter( "Hank" );
// Nominal value hank.setQuality(QUAL_STRENGTH , 10.3f ); TestExecution.reportToFile( outM, "setQuality() Test 1: nominal value", hank.getQualityValue( QUAL_STRENGTH ), 10.3f );
// Out of range value hank.setQuality( QUAL_PATIENCE, -6.2f ); TestExecution.reportToFile( outM, "setQuality() Test 2: nominal value", hank.getQualityValue(QUAL_PATIENCE ), 0.0f );
// Value below close-to-zero threshold. hank.setQuality( QUAL_STAMINA, getTolerance() * 0.9f ); TestExecution.reportToFile( outM, "setQuality() Test 3: value close to zero", hank.getQualityValue(QUAL_STAMINA), 0.0f );
Listing page 9
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
// Tests for adjustQuality().
// Set up for test and verify: Values should be 20 each. EncounterCharacter harvey = new EncounterCharacter( "Harvey" ); TestExecution.reportToFile( outM, "adjustQuality() test 0: verify that values add to 100", harvey.sumOfQualities(), 100.0f );
// Nominal adjustment harvey.adjustQuality(QUAL_STRENGTH , 30.0f ); // strength 30 rest 70/4 each TestExecution.reportToFile( outM, "adjustQuality() test 1: values sum to 100 after adjusting", harvey.sumOfQualities(), 100.0f ); TestExecution.reportToFile ( outM, "adjustQuality() test 2: values adjusted as commanded", harvey.getQualityValue(QUAL_STRENGTH ), 30.0f );
// Adjustment resulting in a zero value harvey.adjustQuality( QUAL_STAMINA, 99.0f ); TestExecution.reportToFile( outM, "adjustQuality() test 3: verify low value reverts to zero", harvey.getQualityValue( QUAL_STRENGTH ), 0.0f );
// Conclude outM.close(); System.out.println( "\nMethod tests of EncounterCharacter class concluded." ); }
Listing
page 10
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
/ Class to test repainting of characters. Creates a window, which will contain
/ Instance attribute that remembers which character image to display. / private EncounterCharacter characterI;*
/ Basic constructor -- create a window for testing some character's image.
Listing page 11
Adapted fromAdapted fromAdapted fromAdapted fromAdapted fromAdapted from Software Engineering: An Object-Oriented Perspective Software Engineering: An Object-Oriented Perspective Software Engineering: An Object-Oriented Perspective Software Engineering: An Object-Oriented Perspective Software Engineering: An Object-Oriented Perspective Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 20001), with permission. by Eric J. Braude (Wiley 20001), with permission.by Eric J. Braude (Wiley 20001), with permission.by Eric J. Braude (Wiley 20001), with permission.by Eric J. Braude (Wiley 20001), with permission.by Eric J. Braude (Wiley 2001), with permission.
/ Repaint the display areaof the frame.
characterI.showCharacter(this, drawP, // Drawn small, facing right. new Point(widthUnitM, heightUnitM), heightUnitM, false);
characterI.showCharacter(this, drawP, // Drawn large, facing left. new Point(widthUnitM4, heightUnitM3), heightUnitM2, true);*
characterI.showCharacter(this, drawP, // Drawn large, facing right. new Point(widthUnitM2, heightUnitM2), heightUnitM2, false);*
characterI.showCharacter(this, drawP, // Drawn small, facing left. new Point(widthUnitM3, heightUnitM4), heightUnitM, true); }**
} // End of testCharacterImage inner class
Listing page 12
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
public static boolean checkAssertion ( int loopSoFarP, int indexOfMaxP, double[] arrayP ) { // First establish following booleans:
boolean b1M = true; / means values of arrayP[ ] are lower than arrayP[ indexOfMaxP ] for all indices < indexOfMaxP / if( indexOfMaxP != 0 ) // b1M true if max so far is first element for( int u = 0; u < indexOfMaxP; ++u ) b1M &= ( arrayP[u] < arrayP[ indexOfMaxP ] );
....
Assertion-based testing for max() 1/
public static boolean checkAssertion
( int loopSoFarP, int indexOfMaxP, double[] arrayP )
{
// First establish following booleans:
boolean b1M = true; / means values of arrayP[ ] are lower than arrayP[ indexOfMaxP ] for all indices < indexOfMaxP / if( indexOfMaxP != 0 ) // b1M true if max so far is first element for( int u = 0; u < indexOfMaxP; ++u ) b1M &= ( arrayP[u] < arrayP[ indexOfMaxP ] );
boolean b2M = true; / means vals. of arrayP[ ] no higher*
than arrayP[ indexOfMaxP ] for indices
*indexOfMaxP … loopSoFarP / for( int v = indexOfMaxP; v <= loopSoFarP; ++v ) b2M &= ( arrayP[v] <= arrayP[ indexOfMaxP ] );
Assertion-based
Testing for Defining an max() Assertion Checker: 1 of 2:
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
if ( // Loop has progressed up to index loopSoFarP ( 0 <= loopSoFarP ) && (loopSoFarP < arrayP.length ) && // indexOfMaxP is the index <= loopSoFarP ... ( 0 <= indexOfMaxP ) && ( indexOfMaxP <= loopSoFarP ) && b1M && b2M // ... where the first max occurs ) { System.out.println( "Assertion valid" ); return true; } else { System.out.println( "Assertion invalid" ); return false; } }
Defining an Assertion Checker for max(): 2 of 2
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Paths to be Checked
Parameter & settings make sense?
Parameter name too long?
N
N Y
Decision
Coverag
e
Set _name to “defaultName"
Y
Truncate name
Set _name to parameter
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Coverage example
Perform Method Testing (Humphrey)
(black box)
(black box)
(statement coverage)
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Perform Method Testing (Humphrey
(part of a correctness proof)
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
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....
(2) Design GameCharacter Requirements
An abstract class with attribute name ...
....
Relating Tests to
Requirements &
(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 Requirements
An abstract class with attribute name ...
Encounter Characters ... against this requirement
Test this method ...
Test this class ...
... against this requirement
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Partitioning of
Range for Unit
Testing 1 of 2 Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Partitioning of
Range for Unit
Testing 2 of 2 Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Agenda
code
guidelines
Plan for Unit Testing
1. Decide on the philosophy for unit testing - individual engineer responsible (common)? - reviewed by others? - **designed & performed by others?
Adapted from Software Engineering: An Object-Oriented Perspective by Eric J. Braude (Wiley 2001), with permission.
Quality of test cases
Mutation testing
n How does a manager measure the adequacy of the test cases?
n (2) Mutation testing