Timeline for PHPUnit assert that an exception was thrown?
Current License: CC BY-SA 4.0
21 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 16, 2023 at 17:24 | history | edited | Sᴀᴍ Onᴇᴌᴀ | CC BY-SA 4.0 |
update links - could have used archive link for second since it had more detailed headers: https://web.archive.org/web/20210512182032/https://thephp.cc/news/2016/02/questioning-phpunit-best-practices
|
Apr 18, 2021 at 20:22 | comment | added | Jimmix | @laurent It is possible to test multiple exceptions with the same test case, test method see how | |
Aug 6, 2020 at 0:14 | comment | added | bdsl |
You can designate the precise line of the test code that is expected to throw. Put the call to expectException immediately before that line. It should be the last line in the test method, since execution of the method stops when the exception is thrown anyway.
|
|
May 12, 2020 at 18:04 | comment | added | user11995521 | The exception gets thrown in the terminal fine but the test still does not pass | |
Oct 15, 2019 at 19:44 | history | edited | Josh Kelley | CC BY-SA 4.0 |
Current link
|
Sep 11, 2018 at 1:14 | comment | added | CJ Dennis | @mindplay.dk Codeception (which is a wrapper for PHPUnit) allows testing an exact line for the expected exception. | |
S Nov 1, 2017 at 13:14 | history | suggested | Y. E. | CC BY-SA 3.0 |
Improve documentation link formatting. Add a link to PHPUnit author article answering the question in more details.
|
Nov 1, 2017 at 12:25 | review | Suggested edits | |||
S Nov 1, 2017 at 13:14 | |||||
May 30, 2017 at 15:58 | comment | added | laurent | 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. | |
Mar 7, 2017 at 0:07 | history | edited | Frank Farmer | CC BY-SA 3.0 |
added 126 characters in body
|
Nov 13, 2016 at 18:14 | comment | added | Jason McCreary |
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.
|
|
Aug 8, 2016 at 13:54 | history | edited | NDM | CC BY-SA 3.0 |
answered method is deprecated, string classnames are oldskool
|
Mar 1, 2016 at 15:05 | comment | added | hejdav |
FYI: as of phpunit 5.2.0 setExpectedException method is deprecated, replaced with the expectException one. :)
|
|
Aug 24, 2015 at 19:08 | comment | added | mindplay.dk | 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. | |
Jun 23, 2015 at 14:53 | comment | added | Christian Maioli M. | Note that there doesn't seem to be a way to explicitly expect no exceptions. You just have to call your method and if an exception is thrown, the test will automatically fail. | |
Nov 25, 2014 at 15:50 | comment | added | Alcalyn |
If you use namespaces, well you need to enter the full namespace: $this->setExpectedException('\My\Name\Space\MyCustomException');
|
|
S Jun 3, 2014 at 20:17 | history | suggested | twiz | CC BY-SA 3.0 |
Link was broken. I fixed it.
|
Jun 3, 2014 at 20:16 | review | Suggested edits | |||
S Jun 3, 2014 at 20:17 | |||||
May 23, 2013 at 7:04 | history | edited | Prof. Falken | CC BY-SA 3.0 |
[Edit removed during grace period]
|
Apr 16, 2011 at 0:40 | vote | accept | Felipe | ||
Apr 16, 2011 at 0:26 | history | answered | Frank Farmer | CC BY-SA 3.0 |