[solved] how to use pass() and fail()

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

[solved] how to use pass() and fail()

Post by raghavan20 »

suppose in an unit test, i have a complex condition and i do not want to use any of the assert() functions then i would like to issue a pass() or a fail if the result satisfy a set of conditions. i learnt from advanced php book that i can use pass() and fail() for that purpose, but when i try to use those, the PHPUnit library reports those functions are not available.

the version information i collected is below

Code: Select all

@version    SVN: $Id: TestSuite.php 537 2007-02-24 06:58:18Z sb $

any ideas? any working example is deeply appreciated.
Last edited by raghavan20 on Wed Jul 25, 2007 4:36 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Functions or methods?

$this->pass(), $this->fail($message);
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

php unit test class

Code: Select all

<?php

##echo "<br />" . __FILE__;
##echo "<br />" . $_SERVER['PHP_SELF'];

//initialize
require_once( "../include.php" );
setIncludePath( '/var/www/html/CDF/backend/modules/' );


//build test case
require_once( '../../helpers/SupplierHelper.php' );
require_once( 'PHPUnit/Framework/TestCase.php'  );
class SupplierHelperTest extends PHPUnit_Framework_TestCase{
	
	public function __construct( $name ){
		parent::__construct( $name );
	}
	

      //some code removed
	
	//5
	public function testFindCodeFromSAN(){
		$result = SupplierHelper::findSupplierInfoFromInfo( 'SAN', '1697978', 'Code' );
		$result1 = SupplierHelper::findSupplierInfoFromInfo( 'SAN', '1556150', 'Code' );
		if( $result && $result1 ) $this->pass();
		else $this->fail( 'did not pass' );
		##$this->assertEquals( 'ING', $result ); 
	}	 
	
	
		//some code removed
	
}



?>

<?php

require_once( 'PHPUnit/Framework/TestSuite.php' );
require_once( 'PHPUnit/TextUI/TestRunner.php' );

$suite = new PHPUnit_Framework_TestSuite;
##$suite->addTest( new SupplierHelperTest( 'testFindSupplierID' ) );
$suite->addTestSuite( 'SupplierHelperTest' );
PHPUnit_TextUI_TestRunner::run( $suite );




?>

error

Code: Select all

PHPUnit 3.1.2 by Sebastian Bergmann.

....PHP Fatal error:  Call to undefined method SupplierHelperTest::pass() in /va                                                                                                 r/www/html/CDF/backend/testing/helpersTest/SupplierHelperTest.php on line 48
[root@mail helpersTest]#
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Quickly looking over the PHPUnit source code, it seems that you will need throw a PHPUnit_Framework_ExpectationFailedException in order to "fail" something.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

As far as I know PHPUnit has no pass() method, but does have a fail() method. I think the basis is that if something works, but is not an assertion, then it should be ignored. If something fails, and is not an assertion, you should manually fail it (so somebody notices in case the issue raises no error/exception).

For example:

Code: Select all

try {
    $op = new OID();
    $op->start();
} catch (Exception $e) {
    $this->fail();  // Fail gracefully without interrupting testrun from an uncaught exception
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

thanks maugrim. looks like pass() does not exist. The working code is below.

Code: Select all

	public function testFindCodeFromSAN(){
		$result = SupplierHelper::findSupplierInfoFromInfo( 'SAN', '1697978', 'Code' );
		$result1 = SupplierHelper::findSupplierInfoFromInfo( 'SAN', '1556150', 'Code' );
		if( !( $result == 'ING' && $result1 == 'BT' ) ) $this->fail();
		##$this->assertEquals( 'ING', $result ); 
	}
Post Reply