Disable stack trace when phpunit exception

I don't need the stack trace below when the PHPUnit assert fails, just my message ("Type: R Expected: 333.33333333333 Actual: 345") and the PHPUnit error message ("Failed assert that false is true") .

Is there a way to distinguish all my tests in try / catch blocks and remove the stack trace from the exception message before displaying it?

I really do not want the stack trace to disappear for any exceptions other than PHPUnit_Framework_ExpectationFailedException, however, if that is not possible, I could handle the loss of stack trace during all PHPUnit tests.

Other posts on SO seem to offer solutions to the opposite problem, returning a stack trace when xdebug disables it.

 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

This problem seems to be caused by the IDE (IntelliJ Idea and possibly PHPStorm), which does not call PHPUnit directly during unit testing, but through its own script, ide_phpunit.php. The problem does not occur when directly calling PHPUnit from the command line. This ide_phpunit.PHP script is created each time using the IDE, so the change is not so simple and does not look like writing protection from being overwritten. It may be a simple solution, but I put it in the β€œnot worth the effort” basket.

+7
source share
2 answers

I downloaded PHPUnit 3.7.21. I checked this test:

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

Everything looks good, this is my conclusion:

./vendor/bin/phpunit./test.php
PHPUnit 3.7.21 from Sebastian Bergman.

R

Time: 0 seconds, Memory: 2.50Mb

There was 1 failure:

1) FooTest :: testBar Failed to state that false is true.

/home/cypis/devel/phpunit/test.php:7

FAILURES! Tests: 1, Claims: 1, Failures: 1.

Did you extend your test with the \PHPUnit_Framework_TestCase class?

0
source

This will only happen when you turn on xdebug and xdebug.show_exception_trace set to 1 to cancel the effects of this behavior, you can disable xdebug (only if you do not xdebug.show_exception_trace 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); } } ?> 

Link: Why is PHPUnit hiding my backtrace xdebug?

0
source

All Articles