Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling stack trace on PHPUnit exception

I have no need for the stack trace below when an PHPUnit assert fails, just my custom message ("Type: R Expected: 333.33333333333 Actual: 345") and PHPUnit's fail message ("Failed assert that false is true").

Is there a way other than putting all my tests in try/catch blocks and stripping the stack trace from the exception message before displaying it?

I don't really wish the stack trace to disappear for any exceptions other than PHPUnit_Framework_ExpectationFailedException, however if this is not possible, I could handle losing the stack trace during all PHPUnit tests.

Other posts on SO seem to suggest solutions for the opposite problem, getting the stack trace back when xdebug turns it off.

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}

Update

It appears this issue is caused by IDE (IntelliJ Idea and possibly PHPStorm) not calling PHPUnit directly when unit testing but via its own script, ide_phpunit.php. The problem doesn't occur directly invoking PHPUnit from a command line. This ide_phpunit.php script is created each time by the IDE so modification isn't easy, nor does it like being write protected against overwrite. There may be an easy solution but I put this one in the "not worth the effort" basket.

like image 332
jontyc Avatar asked May 29 '13 05:05

jontyc


1 Answers

This will only happen when you have xdebug enabled and xdebug.show_exception_trace is set to 1, to reverse effects of this behavior either you can disable xdebug (only when you are not generating coverage report) or set xdebug.show_exception_trace to 0.

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

so adding above code will disable stack traces for you.

Code Example:

// 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);
    }
}
?>

Ref: Why does PHPUnit hide my xdebug backtrace?

like image 87
Saket Patel Avatar answered Sep 19 '22 12:09

Saket Patel