A2: Testing a Data Abstraction

CPSC 210

Learning Goals

  • To design tests based on the specification of a data abstraction
  • To design tests using the jUnit 5 framework
  • Devising test cases given a (clear) EFFECTS clause (knowing what to test)

  • Using combinations of inputs and outputs to determine what tests to write

Test-Driven Dev. (TDD)

Write Tests

Run Tests

FAIL

Implement

PASS

Testing Rules

  • If a method has a REQUIRES clause, test only cases that satisfy that clause
  • We do not test with dx <= 0 as behaviour is not specified under those circumstances
// REQUIRES: dx > 0
// MODIFIES: this
// EFFECTS: adds dx to xcoord
public void move(int dx) {…}

Testing Overview (2)

  • Test boundary cases (e.g. endpoints of an interval)
  • Test typical cases (e.g. midpoints of an interval)
  • Test all cases/aspects of the EFFECTS clause
  • Test that the behaviour of the method is as expected when called multiple times
    • ​esp. when the method has a MODIFIES clause

Testing Example

// MODIFIES: this
// EFFECTS: inserts the integer into the set, unless it's
// already there, in which case it does nothing.
// if the number is 0 it returns "that's nothing!"
// if the number is <0 it returns "that's negative!"
// if the number is >0 it returns "that's positive!"
public String insert(Integer num) { }

-1      0      1

-∞

+∞

-10

+10

-10

testInsertNegative
testInsertNegativeAtUpperBoundary
testInsertZero
testInsertPositive
testInsertPositiveAtLowerBoundary

Lecture Ticket

Lecture Ticket Review

public class Oven {
  private int temperature;

  // EFFECTS: constructs an oven 
  // whose temperature is 65F
  public Oven() {
    // stub
  }

  // REQUIRES: 180 <= degrees <= 500
  // MODIFIES: this
  // EFFECTS: heats oven to degrees Fahrenheit
  public void preheatTo(int degrees) {
    // stub
  }
}

Lecture Ticket Review (2)

public class Oven {
  private int temperature;

  // EFFECTS: constructs an oven 
  // whose temperature is 65F
  public Oven() {
    // stub
  }

  // REQUIRES: 180 <= degrees <= 500
  // MODIFIES: this
  // EFFECTS: heats oven to degrees Fahrenheit
  public void preheatTo(int degrees) {
    // stub
  }
}

Lecture Ticket Review (3)

public class Oven {
  private int temperature;

  // EFFECTS: constructs an oven 
  // whose temperature is 65F
  public Oven() {
    // stub
  }

  // REQUIRES: 180 <= degrees <= 500
  // MODIFIES: this
  // EFFECTS: heats oven to degrees Fahrenheit
  public void preheatTo(int degrees) {
    // stub
  }
}

JUnit Example

public class Truck {

  // MODIFIES: this
  // EFFECTS: sets the status of 
  //   the truck to 'driving'
  public void drive() { ... }

  // EFFECTS: returns true if the 
  //   truck has a status of 'driving'
  public boolean isDriving() { ... }
  
  // MODIFIES: this
  // EFFECTS: sets the status of 
  //   the truck to 'parked'
  public void park() { ... }

  // ...
}
public class TruckTest {

















}
@Test
public void driveTest() {
	


}
@Test
public void parkTest() { ... }
private Truck truck;
@BeforeEach
public void setup() {
 this.truck = new Truck();
}
this.truck.drive();
assertTrue(this.truck.isDriving());

Lecture Lab

A2: Testing a Data Abstraction

The End - Thank You!

CPSC210 - A2 Testing a Data Abstraction

By Steven Wolfman

CPSC210 - A2 Testing a Data Abstraction

  • 284