writing test cases with phpunit
DESCRIPTION
Writing Test Cases with PHPUnitTRANSCRIPT
![Page 1: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/1.jpg)
Unit Test Cases
Shouvik Chatterjee
Senior Product Engineer, Kayako
@tweetshouvik
![Page 2: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/2.jpg)
Is It Important?
Yes! Yes!
Yes!Yes!
Yes!
Yes! Yes!
Yes!Yes!
Yes!
Yes!Yes!
Yes!Yes!
Yes!Yes!
Yes!
Yes!
![Page 4: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/4.jpg)
PHPUnit
![Page 5: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/5.jpg)
What is PHPUnit?
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP.
![Page 6: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/7.jpg)
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 9: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/9.jpg)
assertInstanceOf()
assertInstanceOf($expected, $actual[, $message = ''])
Reports an error identified by $message if $actual is not an instance of $expected.
![Page 11: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/11.jpg)
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 13: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/13.jpg)
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 15: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/15.jpg)
@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 17: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/17.jpg)
@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 19: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/19.jpg)
Rules of Thumb
![Page 20: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/20.jpg)
Don’t write test cases for
the sake of writing.
![Page 21: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/21.jpg)
Be descriptive when writing test cases.
public function testCreateReturnsTrue() { … }
public function testCaseOne() { … }
![Page 22: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/22.jpg)
Make use of the PHPUnit’s built-in methods.
$this->assertTrue($isValidStatus);
$this->assertEquals(true, $isValidStatus);
![Page 23: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/24.jpg)
Try to keep your test cases
independent.
![Page 27: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/27.jpg)
Questions?
![Page 28: Writing Test Cases with PHPUnit](https://reader035.vdocuments.us/reader035/viewer/2022062304/559239351a28ab275d8b459e/html5/thumbnails/28.jpg)
Thank You