Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • 14
    If you use namespaces, well you need to enter the full namespace: $this->setExpectedException('\My\Name\Space\MyCustomException');
    – Alcalyn
    Commented Nov 25, 2014 at 15:50
  • 21
    The fact that you can't designate the precise line of code that is expected to throw, is an error IMO. And the inability to test for more than one exception in the same test, makes testing for many expected exceptions a really clunky affair. I wrote an actual assertion to try to solve those problems. Commented Aug 24, 2015 at 19:08
  • 22
    FYI: as of phpunit 5.2.0 setExpectedException method is deprecated, replaced with the expectException one. :)
    – hejdav
    Commented Mar 1, 2016 at 15:05
  • 58
    What's not mentioned in the docs or here, but the code expected to throw an exception needs to be called after expectException(). While it might have been obvious to some, it was a gotcha for me. Commented Nov 13, 2016 at 18:14
  • 20
    It's not obvious from the doc, but no code after your function that throws an exception will be executed. So if you want to test multiple exceptions in the same test case you cannot.
    – laurent
    Commented May 30, 2017 at 15:58