setup and teardown

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

setup and teardown

Post by kpraman »

Code: Select all

<?php


     class MembersTest extends PHPUnit_TestCase {

               public function setUp()
	      {
	     	    mysql_connect(localhost,'root','');
		    mysql_select_db('tblname');

		    mysql_query("TRUNCATE members");

	      }
		  
		  
	public function TearDown()
	     {
		      mysql_close();
	     } 


	$newinsertdata=new Members;
	$query=mysql_query("INSERT INTO members SET memberTypeId='1', memberFName='testFname', memberLName='testLname', memberAddress='testAddress', memberCity='testCity', memberState='testState', memberZip='123456', memberCountry='India', memberPhone='1234567', memberEmail='test@testmail.com', memberFax='1234567890',enableFlag='true'");

              $memberid_insertd=mysql_insert_id();
              $resExpectedInsertd=array((string)$memberid_insertd,'1','testFname','testLname','testAddress','testCity','testState','123456','India','1234567','test@testmail.com','1234567890','true');
		   
             $query=mysql_query("SELECT * FROM members WHERE memberId=$memberid_insertd");
             $resActualInsertd=mysql_fetch_row($query);
             $this->assertEquals($resExpectedInsertd,$resActualInsertd, "COULD NOT INSERT DATA.");




?>

Code: Select all

mysql_query("TRUNCATE members");
is not working.
Last edited by kpraman on Mon Nov 06, 2006 8:08 am, edited 1 time in total.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Does it return an error? Have you tried this outside the test environment with a mysql client?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

When used directly truncate is working. i.e(values are deleted in the database) If used in setUp(), its not working


WORKS

Code: Select all

<?php

  public function setUp()
  {
      mysql_connect(localhost,'root','');
      mysql_select_db('tblname');
     // mysql_query("TRUNCATE members");

}



$newinsertdata=new Members; 
$query=mysql_query("INSERT INTO members SET memberTypeId='1', memberFName='testFname', memberLName='testLname', memberAddress='testAddress', memberCity='testCity', memberState='testState', memberZip='123456', memberCountry='India', memberPhone='1234567', memberEmail='test@testmail.com', memberFax='1234567890',enableFlag='true'"); 
$memberid_insertd=mysql_insert_id(); $resExpectedInsertd=array((string)$memberid_insertd,'1','testFname','testLname','testAddress','testCity','testState','123456','India','1234567','test@testmail.com','1234567890','true'); 
                   
$query=mysql_query("SELECT * FROM members WHERE memberId=$memberid_insertd"); 
$resActualInsertd=mysql_fetch_row($query); 
$this->assertEquals($resExpectedInsertd,$resActualInsertd, "COULD NOT INSERT DATA."); 

mysql_query("TRUNCATE members");

?>


NOT WORKING

Code: Select all

<?php

  public function setUp()
  {
      mysql_connect(localhost,'root','');
      mysql_select_db('tblname');
      mysql_query("TRUNCATE members");

}



$newinsertdata=new Members; 
$query=mysql_query("INSERT INTO members SET memberTypeId='1', memberFName='testFname', memberLName='testLname', memberAddress='testAddress', memberCity='testCity', memberState='testState', memberZip='123456', memberCountry='India', memberPhone='1234567', memberEmail='test@testmail.com', memberFax='1234567890',enableFlag='true'"); 
$memberid_insertd=mysql_insert_id(); $resExpectedInsertd=array((string)$memberid_insertd,'1','testFname','testLname','testAddress','testCity','testState','123456','India','1234567','test@testmail.com','1234567890','true'); 
                   
$query=mysql_query("SELECT * FROM members WHERE memberId=$memberid_insertd"); 
$resActualInsertd=mysql_fetch_row($query); 
$this->assertEquals($resExpectedInsertd,$resActualInsertd, "COULD NOT INSERT DATA."); 

//mysql_query("TRUNCATE members");

?>
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Not certain to be honest.

Shouldn't the truncation occur in tearDown? After data has been inserted, tests performed, and before you pass off to the next test. i.e. ensure a clean slate exists after running the test?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Thanks.

I wrote mysql_query("TRUNCATE members"); in TearDown(). It's working fine.
Post Reply