Upload
manel-selles
View
63
Download
0
Embed Size (px)
Citation preview
Testing
● Say it once, check every time automatically.○ “Don’t do repetitive checks”
● Increase reliability and quality of software.○ “I didn’t think of that!”
● Discover regressions in software.○ “Why did it break?”
● Improve confidence in our code.○ “I think this will work.”
Testing
Fibonacci● 0 : 0● 1 : 1● 2 : 1● 3 : 2● 4 : 3● 5 : 5● 30 : 832040● 40 : 102334155● 102 : 927372692193078999
Testing
/**
* Fibonacci using an iterative approach
*/
public function fibonacci($n)
{
$a = 0;
$b = 1;
for ($i = 0; $i < $n; $i++) {
$c = $a;
$a = $b;
$b += $c;
}
return $a;
}
Testing
function testFibonacci()
{
echo ($this->fibonacci(0) === 0) === TRUE;
echo ($this->fibonacci(1) === 1) === TRUE;
echo ($this->fibonacci(2) === 1) === TRUE;
echo ($this->fibonacci(3) === 2) === TRUE;
echo ($this->fibonacci(4) === 3) === TRUE;
echo ($this->fibonacci(5) === 5) === TRUE;
echo ($this->fibonacci(30) === 832040) === TRUE;
echo ($this->fibonacci(40) === 102334155) === TRUE;
echo ($this->fibonacci(102) === 927372692193078999) === TRUE;
}
Testing
class FibonacciTest extends PHPUnit_Framework_TestCase{
function testFibonacci(){
$this->assertEquals($this->fibonacci(0), 0);
$this->assertEquals($this->fibonacci(1), 1);
$this->assertEquals($this->fibonacci(2), 1);
$this->assertEquals($this->fibonacci(3), 2);
$this->assertEquals($this->fibonacci(4), 3);
$this->assertEquals($this->fibonacci(5), 5);
$this->assertEquals($this->fibonacci(30), 832040);
$this->assertEquals($this->fibonacci(40), 102334155);
$this->assertEquals($this->fibonacci(102), 927372692193078999);
}}
Testing
/**
* Fibonacci using recursion
*/
public function fibonacci($n)
{
if ($n == 0) {
return 0;
}
if ($n == 1) {
return 1;
}
return $this->fibonacci($n - 1) + $this->fibonacci($n - 2);
}
Testing
Assertions● assertEquals● assertEmpty● assertSame● assertInstanceOf ● assertArrayHasKey● assertArrayHasSubset● assertCount● assertNull
Testing
Without behaviour● Dummy -> no content● Stub -> to guide the test
With behaviour● Fake -> to simulate● Spy -> a call has been made● Mock -> ensure expectation
Testing
● Global can not be substituted○ Avoid static and singleton
● It’s easier to fake collaborators○ Composition over inheritance○ Isolate dependencies○ Inject dependencies
● It’s easier to test small and one-responsibility classes○ Single responsibility principle