PHPunit忽略设置方法中的异常
我注意到PHPUnit忽略了PHPunit忽略设置方法中的异常,php,exception,installation,phpunit,Php,Exception,Installation,Phpunit,我注意到PHPUnit忽略了setUp()方法中抛出的异常,甚至在setUp函数抛出异常时也只运行测试 在下面的代码中,异常将被忽略,它下面的代码将不会运行,test\u方法将失败,因为它使用的是未定义的变量 protected $a; public function setUp() { parent:setUp(); throw new Exception(); // setup now exits silently. $this->a = new A(); /
setUp()
方法中抛出的异常,甚至在setUp函数抛出异常时也只运行测试
在下面的代码中,异常将被忽略,它下面的代码将不会运行,test\u方法
将失败,因为它使用的是未定义的变量
protected $a;
public function setUp() {
parent:setUp();
throw new Exception(); // setup now exits silently.
$this->a = new A(); // will never run
}
public function testA() {
$this->assertTrue($this->a->something()); // will exit tests with PHP error, because $this->a === null
}
我使用phpunit.xml配置文件通过CLI运行phpunit
有人知道一种方法可以让PHPunit报告异常,然后停止testCase的执行吗?抛出异常不是正确的方法,在PHPunit中有一种特殊的方法:
<?php
class DatabaseTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!extension_loaded('mysqli')) {
$this->markTestSkipped(
'The MySQLi extension is not available.'
);
}
}
public function testConnection()
{
// ...
}
}
?>
无法重新生成 运行脚本(下面的完整示例)会生成一个错误输出,但出现异常 我想你在别的地方有问题,或者是旧版本的phpunit?即使如此,我也不知道这段代码有任何变化 您可能也在从主干运行phpunit?(“3.6”)在这种情况下,
“Exception”
类的处理本身发生了变化,现在无法测试这种情况,但如果这种情况适用于您,请尝试使用InvalidArgumentException()(仅用于测试),看看这是否会改变情况
phpunit test.php
PHPUnit 3.5.13 by Sebastian Bergmann.
E
Time: 0 seconds, Memory: 3.00Mb
There was 1 error:
1) FooTest::testA
Exception: hi
/home/.../test.php:10
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
您的代码可运行:
<?php
class FooTest extends PHPUnit_Framework_TestCase {
protected $a;
public function setUp(){
parent::setUp();
throw new Exception('hi'); //setup now exits silently.
$this->a = new A(); //will never run
}
public function testA(){
$this->assertTrue($this->a->something()); //will exit tests with PHP error, because $this->a === null
}
}
是的,我知道,这只是一个例子,这里调用了一些数据库连接代码,可能会在失败的连接上引发异常。这对您有帮助吗?还有问题吗?哦,非常抱歉反应太晚了。无论如何,它还没有完全解决,但是我们发现问题不在phpunit,而是在其他方面(不太确定什么是借口)。无论如何,非常感谢你的时间和帮助!