9

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.

jontyc
  • 3,445
  • 6
  • 29
  • 36
  • 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 May 29 '13 at 14:25
  • 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 May 29 '13 at 16:18
  • hmm, yep, definitely something is wrong. You shouldnt see this exception at all. Which version PHPUnit you have? – Cyprian May 29 '13 at 16:25
  • 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 May 29 '13 at 16:38
  • 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 May 29 '13 at 16:53
  • Trying with 3.6.0 and 3.6.12 didn't fix any issues. – jontyc May 29 '13 at 17:19
  • @jontyc: Hi, sorry for asking.. did you manage to fix this problem? I have the same: running from console shows just result(as I need), running from PHPStorm gives me useless stacktrace. Maybe some hints? – Ruslan Polutsygan Jun 03 '13 at 21:14
  • No, it's one of those things which is annoying but not worth the predicted hours, even days tracking it down. I'll update the question with some findings. – jontyc Jun 05 '13 at 03:11

2 Answers2

2

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?

Community
  • 1
  • 1
Saket Patel
  • 6,573
  • 1
  • 27
  • 36
  • The trace appears to be coming from PHP rather than xdebug. I did read the linked post before and explicitly turned off show_exception_trace (even though it's off by default) but the trace remained. It remained even removing xdebug completely. – jontyc Jun 03 '13 at 08:40
0

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?

Cyprian
  • 11,174
  • 1
  • 48
  • 45
  • 1
    This test also fails with the same issue, however I have discovered it (and my test) are only failing from my IDE (which is parsing a ide-phpunit.php script. Running both your test and mine both work fine with the PHPUnit testrunner (phpunit FooTest FooTest.php). I'll look into that script. – jontyc May 29 '13 at 17:22
  • So there is some progress :) Which IDE? – Cyprian May 29 '13 at 18:31
  • 1
    IDE is Idea 11, which creates this ide-phpunit.php script on the fly each time a test is done. Planning on upgrading to 13 when it comes out so will just tolerate this for the time being. Thx for your help. – jontyc May 30 '13 at 13:13
  • I'll post a bounty when I can and award it to you for the help. – jontyc May 30 '13 at 14:23
  • There is no need for the bounty - really :) But thanks. Unfortunately I can't help more, because I don't have Idea, so I just wish luck with solving the problem. Cheers! – Cyprian May 31 '13 at 08:31