Timeline for PHPUnit assert that an exception was thrown?
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 21, 2022 at 21:56 | comment | added | spekulatius |
The annotation methods are depreciated as of 2022. Instead PHPUnit prefers $this->expectException() , $this->expectExceptionMessage() , etc. See github.com/sebastianbergmann/phpunit/issues/3332
|
|
Feb 6, 2020 at 0:52 | history | edited | David Harkness | CC BY-SA 4.0 |
more features removed
|
Feb 4, 2020 at 15:13 | review | Suggested edits | |||
Feb 4, 2020 at 19:55 | |||||
Apr 20, 2018 at 15:38 | comment | added | Vanja D. | The "don't test the message" rule sounds valid, unless you test a method which throws the same exception type in multiple parts of code, with the only difference being the error id, which is passed in the message. Your system might display a message to the user based on the Exception message (not the Exception type). In that case, it does matter which message the user sees, hence, you should test the error message. | |
May 23, 2017 at 11:55 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Jun 7, 2016 at 16:31 | comment | added | Levi Morrison | @Kalmar Usually the tests only assert that the correct exception was thrown and do not examine the code or message at all. This is either because those are not considered to be part of the API/Specification or simply because the writer decided not to test them. | |
Jun 7, 2016 at 14:02 | comment | added | Kalmar | @LeviMorrison Shouldn't the message be tested in the unit test of the Exception? | |
Feb 13, 2016 at 19:08 | history | edited | David Harkness | CC BY-SA 3.0 |
link to new answer for PHP 5.5+
|
Sep 24, 2015 at 16:50 | comment | added | ProfileTwist | Exceptions should be named specific enough that they hold merit on their own. The exception message helps and can be even more specific. | |
Mar 27, 2015 at 9:52 | comment | added | Kamafeather | I wouldn't watch in a doc-block to understand what it expected, but I'd look at the actual test code (regardless the kind of test). That's the standard for all the other tests; I don't see valid reasons for Exceptions to be (oh god) an exception to this convention. | |
Mar 20, 2015 at 12:28 | comment | added | workdreamer | I prefer this method, you will not loose on the code searching what is expected. | |
S Jun 19, 2014 at 19:55 | history | suggested | Justin | CC BY-SA 3.0 |
Link to documentation
|
Jun 19, 2014 at 19:54 | review | Suggested edits | |||
S Jun 19, 2014 at 19:55 | |||||
Sep 6, 2013 at 5:40 | comment | added | Levi Morrison | @DavidHarkness I figured someone would bring that up. Similarly I would agree that testing messages in general is too strict and tight. However it is that strictness and tight binding that may (emphasized purposefully) be what is wanted in some situations, such as the enforcement of a spec. | |
Sep 5, 2013 at 5:26 | comment | added | David Harkness |
@LeviMorrison - IMHO the exception message should not be tested, similarly to log messages. Both are considered extraneous, helpful information when performing manual forensics. The key point to test is the type of exception. Anything beyond that is binding too tightly to the implementation. IncorrectPasswordException should be enough--that the message equals "Wrong password for [email protected]" is ancillary. Add to that that you want to spend as little time writing tests as possible, and you begin to see how important simple tests become.
|
|
Sep 5, 2013 at 4:07 | comment | added | Levi Morrison |
This was my personal choice for testing these exceptions for a while. However, you cannot assert anything dynamic via a comment. For instance, I had a particular message I wanted to match a sprintf value; this cannot be done via a comment. Thus, the setExpectedException is now my preferred way to assert exceptions, messages and codes.
|
|
May 23, 2013 at 17:35 | comment | added | Mike Purcell | @Prof.Falken: Personal preference. Docblock annotation keeps the test method clean, and several frameworks are making use of the same practice; symfony 2, zf2. | |
Jun 15, 2012 at 20:16 | comment | added | Mike Purcell | IMO, this is the preferred method. | |
Apr 16, 2011 at 7:55 | history | answered | David Harkness | CC BY-SA 3.0 |