8
votes

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.

2
I totally dont understand why would you want disable stacktrace? If an exception is thrown and it is uncatched - it means something is broken (and you want see what exactly it is). On the other hand if your logic throws exception - and you want test it, you will use PHPUnit's "setExpectedException" method and you wont get problem you're describing. Or maybe I missed something? - Cyprian
I'm getting this exception thrown even when doing a simple assertTrue(false), so I assumed PHPUnit's behaviour is to throw this exception when an assert fails. No exception is thrown with assertTrue(true). Are you suggesting this exception is indicating a bigger problem than just my test's assert not being asserted? - jontyc
hmm, yep, definitely something is wrong. You shouldnt see this exception at all. Which version PHPUnit you have? - Cyprian
PHPUnit 3.7.21 with PHP5.3.10-1. I see PHPUnit 3.7 requires PHP>=5.3.3 but highly recommends PHP>=5.4.7. I can't upgrade PHP that far yet, I'll try downgrading PHPUnit. - jontyc
I've posted as answer, because it's easier to present code snippet. Try with the test I've posted if you still have the problem. - Cyprian

2 Answers

2
votes

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?

0
votes

I downloaded PHPUnit 3.7.21. I've ran this test:

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

Everything looks good, this is my output:

./vendor/bin/phpunit ./test.php
PHPUnit 3.7.21 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 2.50Mb

There was 1 failure:

1) FooTest::testBar Failed asserting that false is true.

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

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

Did you extend your test with \PHPUnit_Framework_TestCase class?