unit testing with phpunit

20

Click here to load reader

Upload: fercasl

Post on 09-May-2015

2.099 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Unit testing with PHPUnit

UNIT TESTING with PHPUnit

Page 2: Unit testing with PHPUnit

about me @ferca_tw

ferran caellas puig

Core Developer at eyeOS Computer geek and Open Source enthusiast

Page 3: Unit testing with PHPUnit

UNIT TESTING What is

Page 4: Unit testing with PHPUnit

class CoreTest extends PHPUnit_framework_TestCase {

public function TestCount() { $testArray = array(‘value1’,’value2’,’value3’); $countResult = count($testArray); $this->assertEquals(3, $countResult); }

}

Page 5: Unit testing with PHPUnit

What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail. You want to write tests that will pay you back with information.

Erich Gamma

Page 6: Unit testing with PHPUnit

During development Writing tests

Page 7: Unit testing with PHPUnit

TEST-DRIVEN DEVELOPMENT TDD

Page 8: Unit testing with PHPUnit

During debugging Writing tests

Page 9: Unit testing with PHPUnit

1. Verify that you can reproduce the bug. 2. Find the smallest-scale demonstration of the bug in the code. 3. Write an automated test that fails now but will succeed when the bug is fixed. 4. Fix the bug.

STEPS

Page 10: Unit testing with PHPUnit

CODE

COVE

RAGE

The beauty of testing is found not in the effort but in the effiency. Knowing what should be tested is beautiful, and knowing what is being tested is beautiful.

Murali Nandigama

Page 11: Unit testing with PHPUnit
Page 12: Unit testing with PHPUnit

@de

pend

s public function testEmpty() { $stack = array(); $this->assertEmpty($stack); return $stack; } /** * @depends testEmpty */ public function testPush(array $stack) { array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertNotEmpty($stack); return $stack; }

Page 13: Unit testing with PHPUnit

@da

taPr

ovide

r /** * @dataProvider provider */ public function testAdd($a, $b, $c) { $this->assertEquals($c, $a + $b); } public function provider() { return array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 3) ); }

Page 14: Unit testing with PHPUnit

@ex

pect

edEx

cept

ion

/** * @expectedException InvalidArgumentException * @expectedExceptionMessage Right Message */ public function testExceptionHasRightMessage() { throw new InvalidArgumentException('Some Message', 10); } /** * @expectedException InvalidArgumentException * @expectedExceptionCode 20 */ public function testExceptionHasRightCode() { throw new InvalidArgumentException('Some Message', 10); }

Page 15: Unit testing with PHPUnit

expe

ctOu

tput

Strin

g public function testExpectFooActualFoo() { $this->expectOutputString('foo'); print 'foo'; }

Page 16: Unit testing with PHPUnit

Asse

rtion

s assertArrayHasKey()  assertClassHasA/ribute()  assertClassHasSta4cA/ribute()  assertContains()  assertContainsOnly()  assertCount()  assertEmpty()  assertEqualXMLStructure()  assertEquals()  assertFalse()  assertFileEquals()  assertFileExists()  assertGreaterThan()  assertGreaterThanOrEqual()  assertInstanceOf()  assertInternalType()  assertLessThan()  assertLessThanOrEqual()  assertNull()  

assertObjectHasA/ribute()  assertRegExp()  assertStringMatchesFormat()  assertStringMatchesFormatFile()  assertSame()  assertSelectCount()  assertSelectEquals()  assertSelectRegExp()  assertStringEndsWith()  assertStringEqualsFile()  assertStringStartsWith()  assertTag()  assertThat()  assertTrue()  assertXmlFileEqualsXmlFile()  assertXmlStringEqualsXmlFile()  assertXmlStringEqualsXmlString()  

Page 17: Unit testing with PHPUnit

Fixtu

res

setUp() tearDown()

setUpBeforeClass() tearDownAfterClass()

protected function setUp() { $this->stack = array(); } public function testEmpty() { $this->assertTrue(empty($this->stack)); } public function testPush() { array_push($this->stack, 'foo'); $this->assertEquals('foo', $this->stack[count($this->stack)-1]); $this->assertFalse(empty($this->stack)); }

Page 18: Unit testing with PHPUnit

Incom

plete

& Sk

ipped

markTestIncomplete() markTestSkipped()

$this->markTestIncomplete( 'This test has not been implemented yet.' );

phpunit  -­‐-­‐verbose  SampleTest  PHPUnit  3.6.0  by  Sebas4an  Bergmann.  I  Time:  0  seconds,  Memory:  3.75Mb    There  was  1  incomplete  test:    1)  SampleTest::testSomething  This  test  has  not  been  implemented  yet.    /home/sb/SampleTest.php:12  OK,  but  incomplete  or  skipped  tests!  Tests:  1,  Asser4ons:  1,  Incomplete:  1.  

Page 19: Unit testing with PHPUnit
Page 20: Unit testing with PHPUnit

UNIT TESTING with PHPUnit