All pastes #13925 Raw Edit

Stuff

public text v1 · immutable
#13925 ·published 2005-06-10 23:55 UTC
rendered paste body
/**
 * a KISS calculator.
 */
class Calculator {
  /**
   * This method takes a number and multiplies it by 2.
   * @param int $number the number
   * @return int the multiplied number
   */
  function multiplyByTwo($number) {
    return $number * 2;
  }
}

// now we want to write a test for the calculator methods. Every test will test one method of the calculator
// While running the test this happens: setup() --> testXXX() --> teardown() ; setup() --> testYYY() --> teardown() ; and so on

class TestCalculator extends TestCase {
  var $_calculator;

  /**
   * This method is run before every test, and can be used to prepare the environment.
   */
  function setup() {
    $this->_calculator = new Calculator();
  }

  /**
   * This method is run after every test and can be used to clean the environment.
   */
  function teardown() {
    // nothing to tear down curretly. An example could be to close a database connection
  }

  /**
   * Tests the multiplyByTwo method. (this is the actual test)
   */
  function testMultiplyByTwo() {
    $result = $this->_calculator->multiplyByTwo(5);
    // now we have the result of the method in $result. Ofcouse this should be 10 (expected result), but is it?
    // to check if it is really 10 we can use one of the assert methods:
    $this->assertEqual(10, $result);
    // so the assertEquals simply checks if the value of $result is 10. If this is true the test is ok (and your code works)
    // and "the test passes". If $result is not 10 there is a bug in your calculator. In this case the test fails
  }

}