Writing Test Cases with PHPUnit

28
Unit Test Cases Shouvik Chatterjee Senior Product Engineer, Kayako @tweetshouvik

description

Writing Test Cases with PHPUnit

Transcript of Writing Test Cases with PHPUnit

Page 1: Writing Test Cases with PHPUnit

Unit Test Cases

Shouvik Chatterjee

Senior Product Engineer, Kayako

@tweetshouvik

Page 2: Writing Test Cases with PHPUnit

Is It Important?

Yes! Yes!

Yes!Yes!

Yes!

Yes! Yes!

Yes!Yes!

Yes!

Yes!Yes!

Yes!Yes!

Yes!Yes!

Yes!

Yes!

Page 3: Writing Test Cases with PHPUnit

Spot the bug! Pastie.org

Page 4: Writing Test Cases with PHPUnit

PHPUnit

Page 5: Writing Test Cases with PHPUnit

What is PHPUnit?

PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP.

Page 6: Writing Test Cases with PHPUnit

Frequently used Methods

1. assertEquals()

2. assertInstanceOf()

3. setUp() & tearDown()

4. setUpBeforeClass() & tearDownAfterClass()

5. @dataProvider

6. @expectedException & @expectedExceptionMessage

Page 7: Writing Test Cases with PHPUnit

assertEquals()

assertEquals(mixed $expected, mixed $actual[, string $message = ''])

Reports an error identified by $message if the two variables $expected and $actual are not equal.

Page 8: Writing Test Cases with PHPUnit

Pastie.org

Page 9: Writing Test Cases with PHPUnit

assertInstanceOf()

assertInstanceOf($expected, $actual[, $message = ''])

Reports an error identified by $message if $actual is not an instance of $expected.

Page 10: Writing Test Cases with PHPUnit

Pastie.org

Page 11: Writing Test Cases with PHPUnit

setUp() & tearDown()

setUp() method is called before every test case, which means that this method can be called few times per one test class.

Similarly, tearDown() method is called after every test case.

Page 12: Writing Test Cases with PHPUnit

Pastie.org

Page 13: Writing Test Cases with PHPUnit

setUpBeforeClass() & tearDownAfterClass()

setUpBeforeClass() method is executed only once per class, and even before object is constructed, and that is the reason why it is marked as static public function.

Similarly, tearDownAfterClass() method is called after all tests in class finish, and after last tearDown() method is called.

Page 14: Writing Test Cases with PHPUnit

Pastie.org

Page 15: Writing Test Cases with PHPUnit

@dataProvider()

A test method can accept arbitrary arguments. These

arguments are to be provided by a data provider method.

The data provider method to be used is specified using the

@dataProvider annotation.

Page 16: Writing Test Cases with PHPUnit

Pastie.org

Page 17: Writing Test Cases with PHPUnit

@expectedException & @expectedExceptionMessage

The @expectedException annotation is used to test

whether an exception is thrown inside the tested code.

The @expectedExceptionMessage annotation lets you

make an assertion on the error message of an exception.

Page 18: Writing Test Cases with PHPUnit

Pastie.org

Page 19: Writing Test Cases with PHPUnit

Rules of Thumb

Page 20: Writing Test Cases with PHPUnit

Don’t write test cases for

the sake of writing.

Page 21: Writing Test Cases with PHPUnit

Be descriptive when writing test cases.

public function testCreateReturnsTrue() { … }

public function testCaseOne() { … }

Page 22: Writing Test Cases with PHPUnit

Make use of the PHPUnit’s built-in methods.

$this->assertTrue($isValidStatus);

$this->assertEquals(true, $isValidStatus);

Page 23: Writing Test Cases with PHPUnit

Try splitting up your test cases as much as possible for the sake of

readability.// Case 1 public function testCreateException() { … }

// Case 2public function testCreateSuccess() { … }

// Case3public function testCreateReturnsFalse() { … }

Page 24: Writing Test Cases with PHPUnit

Try to keep your test cases

independent.

Page 25: Writing Test Cases with PHPUnit

Pastie.org

Page 26: Writing Test Cases with PHPUnit

Pastie.org

Page 27: Writing Test Cases with PHPUnit

Questions?

Page 28: Writing Test Cases with PHPUnit

Thank You