UNIT TESTING
with PHPUnit
about me
        @ferca_tw
             ferran caellas puig

             Core Developer at eyeOS
Computer geek and Open Source enthusiast
What is
UNIT TESTING
class CoreTest extends PHPUnit_framework_TestCase {

    public function TestCount() {
       $testArray = array(‘value1’,’value2’,’value3’);
       $countResult = count($testArray);
       $this->assertEquals(3, $countResult);
    }
}
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
Writing tests
During development
TDD
TEST-DRIVEN DEVELOPMENT
Writing tests
During debugging
STEPS
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.
CODE COVERAGE   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
public function testEmpty()
           {
             $stack = array();
@depends     $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;
           }
/**
                 * @dataProvider provider
                 */
@dataProvider   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)
                   );
                }
@expectedException
                     /**
                      * @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);
                     }
expectOutputString
                     public function testExpectFooActualFoo()
                       {
                         $this->expectOutputString('foo');
                         print 'foo';
                       }
assertArrayHasKey()	
               assertObjectHasA/ribute()	
  
             assertClassHasA/ribute()	
          assertRegExp()	
  
             assertClassHasSta4cA/ribute()	
     assertStringMatchesFormat()	
  
             assertContains()	
                  assertStringMatchesFormatFile()	
  
Assertions
             assertContainsOnly()	
              assertSame()	
  
             assertCount()	
                     assertSelectCount()	
  
             assertEmpty()	
                     assertSelectEquals()	
  
             assertEqualXMLStructure()	
         assertSelectRegExp()	
  
             assertEquals()	
                    assertStringEndsWith()	
  
             assertFalse()	
                     assertStringEqualsFile()	
  
             assertFileEquals()	
                assertStringStartsWith()	
  
             assertFileExists()	
                assertTag()	
  
             assertGreaterThan()	
               assertThat()	
  
             assertGreaterThanOrEqual()	
        assertTrue()	
  
             assertInstanceOf()	
                assertXmlFileEqualsXmlFile()	
  
             assertInternalType()	
              assertXmlStringEqualsXmlFile()	
  
             assertLessThan()	
                  assertXmlStringEqualsXmlString()	
  
             assertLessThanOrEqual()	
  
             assertNull()	
  
setUp()                        setUpBeforeClass()
           tearDown()                     tearDownAfterClass()

            protected function setUp()
Fixtures
             {
               $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));
             }
markTestIncomplete()

Incomplete & Skipped   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.	
  
UNIT TESTING
with PHPUnit

Unit testing with PHPUnit

  • 1.
  • 2.
    about me @ferca_tw ferran caellas puig Core Developer at eyeOS Computer geek and Open Source enthusiast
  • 3.
  • 4.
    class CoreTest extendsPHPUnit_framework_TestCase { public function TestCount() { $testArray = array(‘value1’,’value2’,’value3’); $countResult = count($testArray); $this->assertEquals(3, $countResult); } }
  • 5.
    What you wantis 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
  • 6.
  • 7.
  • 8.
  • 9.
    STEPS 1. Verify thatyou 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.
  • 10.
    CODE COVERAGE 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
  • 12.
    public function testEmpty() { $stack = array(); @depends $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; }
  • 13.
    /** * @dataProvider provider */ @dataProvider 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) ); }
  • 14.
    @expectedException /** * @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); }
  • 15.
    expectOutputString public function testExpectFooActualFoo() { $this->expectOutputString('foo'); print 'foo'; }
  • 16.
    assertArrayHasKey()   assertObjectHasA/ribute()   assertClassHasA/ribute()   assertRegExp()   assertClassHasSta4cA/ribute()   assertStringMatchesFormat()   assertContains()   assertStringMatchesFormatFile()   Assertions assertContainsOnly()   assertSame()   assertCount()   assertSelectCount()   assertEmpty()   assertSelectEquals()   assertEqualXMLStructure()   assertSelectRegExp()   assertEquals()   assertStringEndsWith()   assertFalse()   assertStringEqualsFile()   assertFileEquals()   assertStringStartsWith()   assertFileExists()   assertTag()   assertGreaterThan()   assertThat()   assertGreaterThanOrEqual()   assertTrue()   assertInstanceOf()   assertXmlFileEqualsXmlFile()   assertInternalType()   assertXmlStringEqualsXmlFile()   assertLessThan()   assertXmlStringEqualsXmlString()   assertLessThanOrEqual()   assertNull()  
  • 17.
    setUp() setUpBeforeClass() tearDown() tearDownAfterClass() protected function setUp() Fixtures { $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)); }
  • 18.
    markTestIncomplete() Incomplete & Skipped 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.  
  • 20.

Editor's Notes

  • #12  phpunit --coverage-html ./result TwitterTest.php