Page 1 of 1

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

Posted: Tue Jul 24, 2007 8:43 am
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.

Posted: Tue Jul 24, 2007 9:14 am
by Chris Corbyn
Functions or methods?

$this->pass(), $this->fail($message);

Posted: Tue Jul 24, 2007 10:20 am
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]#

Posted: Tue Jul 24, 2007 11:08 am
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.

Posted: Wed Jul 25, 2007 2:51 am
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
}

Posted: Wed Jul 25, 2007 3:15 am
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 ); 
	}