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 5 6 7 forwardlast
The Article

Now, let's proceed to the next test:

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

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

This test will test the create() method and makes sure it's creating the file!

We first delete the file created (automatically) by setUp() by using tearDown() and then check and insure that the file no longer exists by using assertFalse() which will pass only if file_exists() returned false (file does not exists).
We then check to see if the method create() successfully created the file and returned true in the last two lines of the test.

Needless to say that it's more reading friendly to make the test name similar or related to the method/part you are testing.

We'll now see a new assertion type in the next test:

    function testReadWrite()
    {
        $contents = 'contents';
        $this->assertTrue($this->file->putContents($contents));
        $this->assertEqual($this->file->getContents(), $contents);
    }

This test will check the putContents() and getContents() methods.
We will write some contents to the file using putContents() and make sure everything went ok and then use assertEqual() to make sure that what we'd written in the file is equal to what we get from getContents().

So you think I am paranoid when I check one part twice? It will be useless to just check for putContents() since it might pass the test but without actually doing what expected by just returning true or maybe because we had defined the method incorrectly! So we have to check the contents of this file and insure they are equal.
This is why we wrote two tests to check file existence and creation as well.

Pages: firstback1 2 3 5 6 7 forwardlast

Similar/related articles:


 
  Sponsors