iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 12, 2008
Film Makers, Bands and Comedians Welcome on MySpace
About
 
May 12, 2008
Software Engineering for Ajax
WebReference.com
 
May 12, 2008
What is the Head Tag For?
About
 
May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

UnitTesting in PHP using SimpleTest 

  Views:    32433
  Votes:    6
by Saleh Jamal 5/15/05 Rating: 

Synopsis:

Unit testing is writing more code which will test the main code wirtten by "throwing" sample data at it and examining what it gets back.
Pages: firstback1 2 3 4 5 7 forwardlast
The Article

Running The Test Case

So now all our tests are ready. We then instantiate the test case by:

$test = new fileTest('test.txt');

Now try running this script….
Nothing appeared, right? Remember when we talked about the line $this->UnitTestCase('File Manipulation Test') and how it's optional and it will only create a header in the reporter? Yes, We have to use the reporter to show how the tests went!

To use the reporter, we have to include the file reporter.php (at the top for convenience) so our file top should look like this:

include_once 'simpletest/unit_tester.php';
include_once 'simpletest/reporter.php';

include_once 'file.class.php';

and then create the reporter and run it by the test case object by adding this line at the end:

$test = new fileTest('test.txt');
$test->run(new HtmlReporter());

Finally, our test case script should be like this:
[fileTest.php]
<?php
include_once
'simpletest/unit_tester.php';
include_once 'simpletest/reporter.php';

include_once 'file.class.php';

class fileTest extends UnitTestCase
{
    var $file;
    var $filename;

    function fileTest($filename)
    {
        $this->filename = $filename;
       
$this->file = new file($this->filename);
        $this->UnitTestCase('File Manipulation Test');
    }

    function setUp()
    {
        $fp = @fopen($this->filename, 'x');
        if (!($fp === false)) {
           fwrite($fp, '');
           fclose($fp);
        }
    }

    function tearDown()
    {
        @unlink($this->filename);
    }

    function testFileExists()
    {
        $this->assertTrue($this->file->exists());
        clearstatcache();
        $this->assertTrue(file_exists($this->file->filename));
    }

    function testCreate()
    {
       $this->tearDown();
       $this->assertFalse(file_exists($this->file->filename));
       clearstatcache();

       $this->assertTrue($this->file->create());
       $this->assertTrue(file_exists($this->file->filename));
    }

    function testReadWrite()
    {
        $contents = 'contents';
        $this->assertTrue($this->file->putContents($contents));
        $this->assertEqual($this->file->getContents(), $contents);
    }
   
   
function testAppend()
    {
        $this->file->putContents("SimpleTest");
        $this->assertTrue($this->file->append("NeverMind"));
        $this->assertWantedPattern('~nevermind$~i', $this->file->getContents());
    }

    function testDelete()
    {
        $this->assertTrue($this->file->delete());
        $this->assertFalse(file_exists($this->filename));
    }
}
$test = new fileTest('test.txt');
$test->run(new HtmlReporter());
?>

Now run the script again and if everything goes fine you should see something similar to this if all tests passed:

File Manipulation Test

1/1 test cases complete: 11 passes, 0 fails and 0 exceptions.



In case we had any failure, you will see something similar to:

File Manipulation Test

Fail: testAppend -> Pattern [~^nevermind$~i] not detected in [String: SimpleTestNeverMind] at line [61]

1/1 test cases complete: 10 passes, 1 fails and 0 exceptions.


As you can see, there was a failure in the method testAppend() and the error message has clearly stated what and where the error is.
Note that this test failed because I edited the pattern and forced the contents of the file to be "NeverMind" only in order to pass (case-insensitive) which, of course, made the test fails.

In case you had any failures, the script will continue and will not terminate until all tests are performed.
If any PHP errors occurred, such as warnings or notices, they will be caught and reported as exceptions.
Pages: firstback1 2 3 4 5 7 forwardlast

Similar/related articles:


 
  Sponsors