gpt4 book ai didi

php - 在 PHPUnit 异常上禁用堆栈跟踪

转载 作者:可可西里 更新时间:2023-11-01 13:00:12 26 4
gpt4 key购买 nike

当 PHPUnit 断言失败时,我不需要下面的堆栈跟踪,只需要我的自定义消息 ("Type: R Expected: 333.33333333333 Actual: 345") 和 PHPUnit 的失败消息 ( “断言假为真失败”)

除了将所有测试放在 try/catch block 中并在显示之前从异常消息中剥离堆栈跟踪之外,还有其他方法吗?

除了 PHPUnit_Framework_ExpectationFailedException 之外,我真的不希望堆栈跟踪消失,但是如果这不可能,我可以处理在所有 PHPUnit 测试期间丢失堆栈跟踪。

SO 上的其他帖子似乎针对相反的问题提出了解决方案,即在 xdebug 关闭时返回堆栈跟踪。

PHPUnit_Framework_ExpectationFailedException : Type: R Expected: 333.33333333333 Actual: 345
Failed asserting that false is true.
#0 /usr/share/php/PHPUnit/Framework/Constraint.php(91): PHPUnit_Framework_Constraint->fail(false, 'Type: R Expecte...')
#1 /usr/share/php/PHPUnit/Framework/Assert.php(2134): PHPUnit_Framework_Constraint->evaluate(false, 'Type: R Expecte...')
#2 /usr/share/php/PHPUnit/Framework/Assert.php(888): PHPUnit_Framework_Assert::assertThat(false, Object(PHPUnit_Framework_Constraint_IsTrue), 'Type: R Expecte...')
#3 /home/simon/Development/golfants/website/unit_tests/PostTest.php(33): PHPUnit_Framework_Assert::assertTrue(false, 'Type: R Expecte...')
#4 [internal function]: PostTest->testRandomAntTypeSelected()
#5 /usr/share/php/PHPUnit/Framework/TestCase.php(976): ReflectionMethod->invokeArgs(Object(PostTest), Array)
#6 /usr/share/php/PHPUnit/Framework/TestCase.php(831): PHPUnit_Framework_TestCase->runTest()
#7 /usr/share/php/PHPUnit/Framework/TestResult.php(648): PHPUnit_Framework_TestCase->runBare()
#8 /usr/share/php/PHPUnit/Framework/TestCase.php(776): PHPUnit_Framework_TestResult->run(Object(PostTest))
#9 /usr/share/php/PHPUnit/Framework/TestSuite.php(775): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#10 /usr/share/php/PHPUnit/Framework/TestSuite.php(745): PHPUnit_Framework_TestSuite->runTest(Object(PostTest), Object(PHPUnit_Framework_TestResult))
#11 /usr/share/php/PHPUnit/TextUI/TestRunner.php(349): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#12 /usr/share/php/PHPUnit/TextUI/Command.php(176): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#13 /tmp/ide-phpunit.php(95): PHPUnit_TextUI_Command->run(Array, true)
#14 /tmp/ide-phpunit.php(434): IDE_PHPUnit_TextUI_Command::main()
#15 {main}

更新

看来这个问题是由 IDE(IntelliJ Idea 和可能的 PHPStorm)在单元测试时没有直接调用 PHPUnit 而是通过它自己的脚本 ide_phpunit.php 引起的。从命令行直接调用 PHPUnit 不会出现此问题。这个 ide_phpunit.php 脚本每次都是由 IDE 创建的,所以修改起来并不容易,也不像写保护一样防止覆盖。可能有一个简单的解决方案,但我把这个放在“不值得努力”的篮子里。

最佳答案

这只会在您启用 xdebug 并且 xdebug.show_exception_trace 设置为 1 时发生,要扭转这种行为的影响,您可以禁用 xdebug(仅当您不生成覆盖率报告时)或将 xdebug.show_exception_trace 设置为 0。

xdebug_enable();
ini_set('xdebug.show_exception_trace', 0);

所以添加上面的代码将为您禁用堆栈跟踪。

代码示例:

// below statements are given here just for illustration this should be added in
// proper places
xdebug_enable();
ini_set('xdebug.show_exception_trace', 0);

class FooTest extends PHPUnit_Framework_TestCase
{
public function testBar()
{
$this->assertTrue(false);
}
}
?>

引用:Why does PHPUnit hide my xdebug backtrace?

关于php - 在 PHPUnit 异常上禁用堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16806074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com