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 13, 2008
MySpace Profile Page Resources
HTML Goodies
 
May 13, 2008
How to Upload Your Photos onto the Web
HTML Goodies
 
May 13, 2008
Email Marketing for MySpace Artists
HTML Goodies
 
May 13, 2008
Top Online Marketing Techniques
HTML Goodies
 
May 13, 2008
I want to create a site just like ____, is that a violation of...
About
 
May 12, 2008
Film Makers, Bands and Comedians Welcome on MySpace
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

UnitTesting in PHP using SimpleTest 

  Views:    32461
  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 3 4 5 6 7 forwardlast
The Article

Writing The Test Case

Now we want to create a test case for this class to see if it works as we expect or not!
To create a test case we have to create a new class which inherits from UnitTestCase class which is found in the file unit_tester.php which comes in SimpleTest framework package.
So we create our new PHP file, let's name it fileTest.php and include unit_tester.php and our file file.class.php:
[fileTest.php] 
<?php
include_once
'simpletest/unit_tester.php';

include_once
'file.class.php';

Of course you have to change the paths to the correct paths in your environment.
We now start the definition of the test case class and let's name it fileTest as well:

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);
    }

As you can see, we have defined 3 methods (including the constructor) and 2 data members, $file and $filename.
The constructor (fileTest) takes one argument which is the file name and assign it to the data member $filename and create the file object and assign it to $file.
The line:  $this->UnitTestCase('File Manipulation Test');  is optional and it just makes a header line in the test reporter (we will talk about this later).

Also we have defined the methods setUp() and tearDown().
setUp() will be called automatically before each and every test and tearDown() will be called after each and every test. However, these two methods are optional.

We have defined the setUp() method to create a new clean file before each test and tearDown() will delete that file after each test.

Pages: firstback1 3 4 5 6 7 forwardlast

Similar/related articles:


 
  Sponsors