PHP Software Engineering Writing a Unit Test Suite - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Software Engineering Writing a Unit Test Suite - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, July 6, 2019

PHP Software Engineering Writing a Unit Test Suite

PHP Software Engineering


Writing a Unit Test Suite

Problem

You want to be able to run more than one unit test conveniently on a regular basis.

Solution

Wrap your unit tests into a group known as a unit test suite.

Discussion

It’s rare to have a program simple enough that a single unit test will fulfill all the testing needs that it will have during its lifespan. Over time, as applications grow there is a need to add more and more tests, either to test new functionality or verify that fixed bugs stay fixed.

When your library of tests gets larger than a handful, you’ll find it much more convenient to group your tests into a unit test suite. A test suite, despite its formal-sounding name, is just a wrapper around a bunch of tests that can all be run by referring to the name of the test suite.

Using the PHPUnit framework, create a test suite to test more than just the str_replace function in PHP. A number of tests related to string functions can be put in a single file. For example, in a file named StringTest.php, put:

       class StringTest extends PHPUnit_Framework_TestCase
       {
              function testStrReplace()
              {
                     $str = 'Hello, all!';
                     $this->assertEquals('Hello, world!', str_replace('all', 'world', $str));
              }

              function testSubstr()
              {
                     $str = 'Hello, all!';
                     $this->assertEquals('e', substr($str, 1, 1));
              }
       }

Now you have two tests that will be run from the StringTest class. Create a similar file called ArrayTest.php, with the following tests defined in it:

       class ArrayTest extends PHPUnit_Framework_TestCase
       {
              function testArrayFlip()
              {
                     $array = array('foo' => 'bar', 'cheese' => 'hotdog');
                     $flipped = array_flip($array);
                     $this->assertEquals('foo', reset($flipped));
              }

              function testArrayPop()
              {
                     $array = array('foo' => 'bar', 'cheese' => 'hotdog');
                     $popped = array_pop($array);
                     $this->assertEquals('hotdog', $popped);
                     $this->assertEquals(1, sizeof($array));
              }
       }

With four tests to run, it’s time to put together a suite that will run all of these whenever you want to check to make sure things are working as they should be. By saving both those files in the same directory, you can just point PHPUnit to that directory to run them both:

       % phpunit testDir

Assuming you’ve saved those two test files in the testDir directory, your output will look something like:

       PHPUnit 3.7.24 by Sebastian Bergmann.
       
       ....
       
       Time: 45 ms, Memory: 5.00Mb

       OK (4 tests, 5 assertions)

whose name matches *Test.php. PHPUnit will recurse into subdirectories as well.

Using this approach, you can grow your automated testing system to include a large number of tests and still be able to trigger them all through a single command.


No comments:

Post a Comment

Post Top Ad