क्या किसी को पता है कि क्या ऐसा assert
कुछ है या नहीं जो परीक्षण कर सकता है कि क्या एक अपवाद को परीक्षण किए गए कोड में फेंक दिया गया था?
क्या किसी को पता है कि क्या ऐसा assert
कुछ है या नहीं जो परीक्षण कर सकता है कि क्या एक अपवाद को परीक्षण किए गए कोड में फेंक दिया गया था?
जवाबों:
<?php
require_once 'PHPUnit/Framework.php';
class ExceptionTest extends PHPUnit_Framework_TestCase
{
public function testException()
{
$this->expectException(InvalidArgumentException::class);
// or for PHPUnit < 5.2
// $this->setExpectedException(InvalidArgumentException::class);
//...and then add your test code that generates the exception
exampleMethod($anInvalidArgument);
}
}
अपेक्षित अपवाद () PHPUnit प्रलेखन
PHPUnit लेखक लेख अपवाद सर्वोत्तम प्रथाओं के परीक्षण पर विस्तृत विवरण प्रदान करता है।
$this->setExpectedException('\My\Name\Space\MyCustomException');
setExpectedException
विधि को पदावनत किया जाता है, expectException
एक के साथ बदल दिया जाता है । :)
expectException()
। हालांकि यह कुछ के लिए स्पष्ट हो सकता था, यह मेरे लिए एक गोच था ।
PHPUnit 9 जारी होने तक आप एक डॉकब्लॉक एनोटेशन का भी उपयोग कर सकते हैं :
class ExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function testException()
{
...
}
}
PHP 5.5+ के लिए (विशेष रूप से नामांकित कोड के साथ), मैं अब उपयोग करना पसंद करता हूं ::class
IncorrectPasswordException
पर्याप्त होना चाहिए - कि संदेश बराबर होता "Wrong password for bob@me.com"
है। इसमें जोड़ें कि आप यथासंभव कम समय परीक्षण लिखने में खर्च करना चाहते हैं, और आप देखना शुरू करते हैं कि सरल परीक्षण कैसे महत्वपूर्ण हो जाते हैं।
यदि आप PHP 5.5+ पर चल रहे हैं, तो आप / के साथ वर्ग का नाम प्राप्त करने के लिए ::class
रिज़ॉल्यूशन का उपयोग कर सकते हैं । इससे कई लाभ मिलते हैं:expectException
setExpectedException
string
इसलिए यह PHPUnit के किसी भी संस्करण के साथ काम करेगा।उदाहरण:
namespace \My\Cool\Package;
class AuthTest extends \PHPUnit_Framework_TestCase
{
public function testLoginFailsForWrongPassword()
{
$this->expectException(WrongPasswordException::class);
Auth::login('Bob', 'wrong');
}
}
PHP संकलन करता है
WrongPasswordException::class
में
"\My\Cool\Package\WrongPasswordException"
PHPUnit समझदार होने के बिना।
नोट : PHPUnit 5.2
expectException
को एक प्रतिस्थापन के रूप में पेश किया गया हैsetExpectedException
।
नीचे दिए गए कोड अपवाद संदेश और अपवाद कोड का परीक्षण करेंगे।
महत्वपूर्ण: यह असफल होगा यदि अपेक्षित अपवाद भी नहीं फेंका गया।
try{
$test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
$this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
$this->assertEquals(1162011, $e->getCode());
$this->assertEquals("Exception Message", $e->getMessage());
}
$this->fail()
इस तरह से इस्तेमाल करने का मतलब यह नहीं है कि मुझे नहीं लगता, कम से कम वर्तमान में नहीं (PHPUnit 3.6.11); यह एक अपवाद के रूप में कार्य करता है। अपने उदाहरण का उपयोग करना, अगर $this->fail("Expected exception not thrown")
कहा जाता है, तो catch
ब्लॉक शुरू हो रहा है और $e->getMessage()
है "अपवाद उत्पन्न नहीं की उम्मीद" ।
fail
संभवतः ब्लॉक के बाद होता है, कोशिश के अंदर नहीं।
fail
दिखाना होगा क्योंकि कॉल try
ब्लॉक में नहीं होनी चाहिए । यह अपने आप में catch
गलत परिणाम पैदा करने वाले ब्लॉक को ट्रिगर करता है ।
catch(Exception $e)
। जब मैं विशिष्ट अपवादों को पकड़ने की कोशिश करता हूं तो यह विधि मेरे लिए काफी try { throw new MySpecificException; $this->fail('MySpecificException not thrown'); } catch(MySpecificException $e){}
आप assertException एक्सटेंशन का उपयोग कर सकते हैं एक परीक्षण निष्पादन के दौरान एक से अधिक अपवादों का दावा करने के का ।
अपने TestCase में विधि डालें और उपयोग करें:
public function testSomething()
{
$test = function() {
// some code that has to throw an exception
};
$this->assertException( $test, 'InvalidArgumentException', 100, 'expected message' );
}
मैंने अच्छे कोड के प्रेमियों के लिए एक विशेषता भी बनाई है ।
assertException
परिभाषित नहीं है। मैं इसे PHPUnit मैनुअल में भी नहीं ढूँढ सकता।
asertException
विधि मूल PHPUnit का हिस्सा नहीं है। आपको PHPUnit_Framework_TestCase
क्लास को इनहेरिट करना होगा और मैन्युअल रूप से ऊपर पोस्ट में लिंक किया हुआ तरीका जोड़ना होगा । आपके परीक्षण के मामले तब इस विरासत वाले वर्ग को प्राप्त होंगे।
एक वैकल्पिक तरीका निम्नलिखित हो सकता है:
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected Exception Message');
कृपया सुनिश्चित करें कि आपकी परीक्षा की कक्षा बढ़ रही है \PHPUnit_Framework_TestCase
।
PHPUnit expectException
विधि बहुत असुविधाजनक है क्योंकि यह परीक्षण पद्धति के अनुसार केवल एक अपवाद का परीक्षण करने की अनुमति देता है।
मैंने इस सहायक समारोह को मुखर करने के लिए बनाया है कि कुछ फ़ंक्शन एक अपवाद फेंकता है:
/**
* Asserts that the given callback throws the given exception.
*
* @param string $expectClass The name of the expected exception class
* @param callable $callback A callback which should throw the exception
*/
protected function assertException(string $expectClass, callable $callback)
{
try {
$callback();
} catch (\Throwable $exception) {
$this->assertInstanceOf($expectClass, $exception, 'An invalid exception was thrown');
return;
}
$this->fail('No exception was thrown');
}
इसे अपने परीक्षण वर्ग में जोड़ें और इस तरह से कॉल करें:
public function testSomething() {
$this->assertException(\PDOException::class, function() {
new \PDO('bad:param');
});
$this->assertException(\PDOException::class, function() {
new \PDO('foo:bar');
});
}
अपवाद परीक्षण के लिए PHPUnit का वर्तमान " सर्वोत्तम अभ्यास " लगता है। अभाव ( डॉक्स )।
चूंकि मैं वर्तमान कार्यान्वयन से अधिक चाहता थाexpectException
, इसलिए मैंने अपने परीक्षण मामलों पर उपयोग करने के लिए एक विशेषता बनाई। यह केवल ~ 50 लाइनों का कोड है ।
assert
वाक्यविन्यासassertNotThrows
Throwable
त्रुटियों का समर्थन करता हैमैंने गितुबAssertThrows
और पैकैगिस्ट को विशेषता प्रकाशित की ताकि इसे संगीतकार के साथ स्थापित किया जा सके।
सिंटैक्स के पीछे की भावना को स्पष्ट करने के लिए:
<?php
// Using simple callback
$this->assertThrows(MyException::class, [$obj, 'doSomethingBad']);
// Using anonymous function
$this->assertThrows(MyException::class, function() use ($obj) {
$obj->doSomethingBad();
});
काफी साफ़?
अधिक व्यापक उपयोग उदाहरण के लिए कृपया नीचे देखें:
<?php
declare(strict_types=1);
use Jchook\AssertThrows\AssertThrows;
use PHPUnit\Framework\TestCase;
// These are just for illustration
use MyNamespace\MyException;
use MyNamespace\MyObject;
final class MyTest extends TestCase
{
use AssertThrows; // <--- adds the assertThrows method
public function testMyObject()
{
$obj = new MyObject();
// Test a basic exception is thrown
$this->assertThrows(MyException::class, function() use ($obj) {
$obj->doSomethingBad();
});
// Test custom aspects of a custom extension class
$this->assertThrows(MyException::class,
function() use ($obj) {
$obj->doSomethingBad();
},
function($exception) {
$this->assertEquals('Expected value', $exception->getCustomThing());
$this->assertEquals(123, $exception->getCode());
}
);
// Test that a specific exception is NOT thrown
$this->assertNotThrows(MyException::class, function() use ($obj) {
$obj->doSomethingGood();
});
}
}
?>
public function testException() {
try {
$this->methodThatThrowsException();
$this->fail("Expected Exception has not been raised.");
} catch (Exception $ex) {
$this->assertEquals($ex->getMessage(), "Exception message");
}
}
assertEquals()
है assertEquals(mixed $expected, mixed $actual...)
, अपने उदाहरण के रूप में, रिवर्स तो यह होना चाहिए$this->assertEquals("Exception message", $ex->getMessage());
यहां सभी अपवाद दावे हैं जो आप कर सकते हैं। ध्यान दें कि वे सभी हैं वैकल्पिक हैं ।
class ExceptionTest extends PHPUnit_Framework_TestCase
{
public function testException()
{
// make your exception assertions
$this->expectException(InvalidArgumentException::class);
// if you use namespaces:
// $this->expectException('\Namespace\MyException');
$this->expectExceptionMessage('message');
$this->expectExceptionMessageRegExp('/essage$/');
$this->expectExceptionCode(123);
// code that throws an exception
throw new InvalidArgumentException('message', 123);
}
public function testAnotherException()
{
// repeat as needed
$this->expectException(Exception::class);
throw new Exception('Oh no!');
}
}
/**
* @expectedException Exception
* @expectedExceptionMessage Amount has to be bigger then 0!
*/
public function testDepositNegative()
{
$this->account->deposit(-7);
}
बहुत लापरवाह रहें "/**"
, डबल "*" पर ध्यान दें। केवल "**" (तारांकन) लिखना आपके कोड को विफल कर देगा। यह भी सुनिश्चित करें कि आपका अंतिम संस्करण phpUnit का उपयोग कर रहा है। Phpunit के कुछ पुराने संस्करणों में @ अप्रत्याशित अपवाद अपवाद समर्थित नहीं है। मेरे पास 4.0 था और यह मेरे लिए काम नहीं करता था, मुझे संगीतकार के साथ अपडेट करने के लिए 5.5 https://coderwall.com/p/mklvdw/install-phpunit-with-composer पर अपडेट करना पड़ा ।
PHPUnit 5.7.27 और PHP 5.6 के लिए और एक परीक्षण में कई अपवादों का परीक्षण करने के लिए, अपवाद परीक्षण को मजबूर करना महत्वपूर्ण था। अपवाद का दावा करने के लिए अकेले अपवाद का उपयोग करना अपवाद नहीं होने पर स्थिति का परीक्षण करना छोड़ देगा।
public function testSomeFunction() {
$e=null;
$targetClassObj= new TargetClass();
try {
$targetClassObj->doSomething();
} catch ( \Exception $e ) {
}
$this->assertInstanceOf(\Exception::class,$e);
$this->assertEquals('Some message',$e->getMessage());
$e=null;
try {
$targetClassObj->doSomethingElse();
} catch ( Exception $e ) {
}
$this->assertInstanceOf(\Exception::class,$e);
$this->assertEquals('Another message',$e->getMessage());
}
function yourfunction($a,$z){
if($a<$z){ throw new <YOUR_EXCEPTION>; }
}
यहाँ परीक्षण है
class FunctionTest extends \PHPUnit_Framework_TestCase{
public function testException(){
$this->setExpectedException(<YOUR_EXCEPTION>::class);
yourfunction(1,2);//add vars that cause the exception
}
}
PhpUnit एक अद्भुत पुस्तकालय है, लेकिन यह विशिष्ट बिंदु थोड़ा निराशाजनक है। यही कारण है कि हम टर्बोटिंग-पीएचपी ओपनसोर्स लाइब्रेरी का उपयोग कर सकते हैं, जिसमें अपवादों के परीक्षण में मदद करने के लिए एक बहुत ही सुविधाजनक शोध पद्धति है। यह यहाँ पाया जाता है:
और इसका उपयोग करने के लिए, हम बस निम्नलिखित करेंगे:
AssertUtils::throwsException(function(){
// Some code that must throw an exception here
}, '/expected error message/');
अनाम फ़ंक्शन के अंदर हम जो कोड टाइप करते हैं, यदि वह अपवाद नहीं छोड़ता है, तो एक अपवाद फेंक दिया जाएगा।
यदि हम अनाम फ़ंक्शन के अंदर जो कोड टाइप करते हैं वह एक अपवाद को फेंक देता है, लेकिन इसका संदेश अपेक्षित रीजैक्स से मेल नहीं खाता है, एक अपवाद भी फेंक दिया जाएगा।