presentation for how we can integrate PHPUnit with php
Size: 3.03 MB
Language: en
Added: Jul 15, 2014
Slides: 18 pages
Slide Content
Unit Testing In PHP Quality is never an accident; It is always the result of intelligent effort.
About Speakers Hrishikesh Raverkar A Geek A haPHPy’er Full Stack Engineer hrishi4u hrishithegreat Madhavi Ghadge-Kumbhar A Technology Enthusiastic A haPHPy’er Love to work in cross platform madhavi.ghadge madhavighadge
Why Unit testing It's a great idea! Makes you confident about impact Helps other developers understand code onboarding easy Speeds up future development process. It will force you to think things such as "how am I going to test this?" Lets you sleep peacefully at night ;) Lots of methodologies have come and gone, paradigms have changed but the requirements are always the same; Make it good and make it fast.
Unit Testing Tools Php Unit : PHPUnit is a package of xUnit family(JUnit, NUnit, CppUnit, PyUnit). Amock : Amock is a mock object library written in PHP 5, inspired by EasyMock. SimpleTest : Unit testing, web testing and mock objects framework. Community support is not available any more Snap Test : SnapTest is a powerful unit testing framework. Simplify the unit test process without sacrificing the agility tests provide.
Why PHPUnit It is the standard. PHPUnit is integrated in every PHP IDE and gives required reports. Now coming with new releases of all frameworks. Works fine with every continuous integration Actively maintained, stable and works great for every codebase, every scenario and every way you want to write your tests. Provides better code coverage reports. One Framework with all facilities to get unit testing done
Installation Traditional way - PEAR Pear channel-discover pear.phpunit.de Pear install phpunit/package_name The New Way - PHAR or Composer http://phpunit.de/manual/current/en/installation.html One Framework with all facilities to get unit testing done
Ways to do Unit testing using PHPUnit Assertion Data Provider Exceptions Fixtures(setUp() and tearDown()) Database testing
Assertion A predicate (a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. We asserted that true will assert to true ( if (true == true) ). There is no magic here, what you see is what you get with assertions. If we assert that false is true ( if (false == true) ), we would get a failing test
<?php class AssertsTest extends PHPUnit_Framework_TestCase { public function testTrueIsTrue(){ $foo = true; $this->assertTrue($foo); } } ?>
Data Provider A test method can accept arbitrary arguments. These arguments are to be provided by a data provider method
<?php class DataTest extends PHPUnit_Framework_TestCase { /** @dataProvider additionProvider*/ public function testAdd($a, $b, $expected) { $this->assertEquals($expected, $a + $b); } public function additionProvider(){ return array( array(0, 0, 0), array(0, 1, 1), array(1, 1, 3)); } } ?>
Exceptions By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception. Using these exceptions, for an instance, you can expect a test to trigger a PHP error
<?php class ExceptionTest extends PHPUnit_Framework_TestCase { /** * @expectedException InvalidArgumentException */ public function testException(){ $this->setExpectedException('InvalidArgumentException'); } } ?>
Fixtures A test fixture is all the things that must be in place in order to run a test and expect a particular outcome. Four phases of a test: Set up -- Setting up the test fixture. Exercise -- Interact with the system under test. Verify -- Determine whether the expected outcome has been obtained. Tear down -- Tear down the test fixture to return to the original state.
<?php class StackTest extends PHPUnit_Framework_TestCase{ protected $stack; protected function setUp() { $this->stack = array(); } public function testEmpty(){ $this->assertTrue(empty($this->stack)); } public function tearDown(){ print "\nMySuite::tearDown()"; } } ?>
Database Testing The four stages of a database test Clean-Up Database Set up fixture Run Test, Verify outcome Teardown