Optional custom fail messages in asserts#133
Conversation
|
There is failure in Travis build: Reason is that in test Dumper.dumpException is used call to Assert:same with three parameters instead of two. line 23 | ... Assert::same(str_repeat('x', 100), str_repeat('x', 120), new stdClass); },I'm not completely sure about purpose of this test so I didn't fixed it yet. Why is there call to same with three parameters? It there third parameter just by mistake or is there any reason for that? |
|
It is typo, fixed 4d68141, you can rebase it |
|
Related #48 |
There was a problem hiding this comment.
there should be a space after if, and if the != is intentionally not !== you should write it in a comment
|
Rebased and conding standard fixed |
|
We do not use abbreviations, so |
|
Renamed failMsg to failMessage |
There was a problem hiding this comment.
typo I will fix it
|
Few notes: missing phpdoc param - only where there are some defined :) and also we use |
|
@hrach Fixed |
|
@MartinMystikJonas My opinion is negative on this (#48) :) Imho it just blocks position for another more useful parameters. I'm solving it in my tests as asserting arrays like: $actual = [];
foreach (...) {
$actual[] = ....;
}
Assert::same([TRUE, TRUE, TRUE], $actual); |
|
@milo In my opinion basic assert method doesn't need any more parameters. And if need for such parameter arises then we can just create mew method. Assert::same($expected, $actual, "My message")
Assert::someWithParam($expected, $actual, $param, "My message")Have custom message as optional parameter is common convetion in other testing frameworks. For example: |
|
@MartinMystikJonas I know this convention but still I don't like it. Few more reasons:
If so, I would rather to see something crazy like: Assert::section('Testing object A')->true(....);I'll accept it if @dg merge this, but I'll not vote fot it. |
|
@milo this is not about clean code, etc, this is about readability and not inventing the wheel. Proposed attitude is well known. Again, please, do not invent the wheel. |
|
Exception message is compared with expected message. It is an alone assertion. Fail message is only the message. @MartinMystikJonas Do you use own fail message for all assertion types or only for some e.g. |
|
@milo It was meant bit different with exception message. I meant usage in application code. For program purposes only exception type is relevant. But we still use descriptive exception messages as help for developers examinig these exceptions. The same reasoning should apply to Assertion failures in tests. I use own fail messages if there is more than one assertion in test. If there is only one assertion the test name should be enough description of problem. Also I do not use own fail messages when default fail message is good enough to identify the problem. For example assertion of thrown exception is usually descriptive enough - there is not much more information you can provide other than different than expected exception was thrown. But for most basic assertions you need aditional information to know what exactly failed. In my opinion test output should be descriptive enough so you doesn't need to check test code to know what failed. |
|
@MartinMystikJonas Oh, I understand now. At all, I guess I'm using different techniques than you but I understand to your needs. |
|
Yes, you probably use tests only for regression testing. Am I right? In this use case you are basically only interested if test fails or not and test fails only exceptionally. But if you use TDD techniques (or something similar like me) you start by failing tests which run many times during development and you need quick feedback what changes in code changed in tests results. |
|
I heard TDD is dead |
|
@hrach Heard that too 😄 And with most points of DHH I agree. Luckily I'm using only something inspired by TDD not it's strict fundamentalist version. |
|
@MartinMystikJonas Time to time I use kind of TDD but usually with one or two tests at the moment and running them by browser with Tracy. |
|
@milo I usually run them directly from console in IDE. |
|
Current solution when you see In my opinion is ideal when test |
|
@dg Seeing which assertion caused the failure is good enough for many cases. Solution in #134 is perfect for these cases. But there are still cases when you need include additional information about failure context. For example Assertions executed inside cycle or recursive function. Or simply when you need to know value of some other variables not only the one you checked in assertion. |
|
It this PR still considered as merge candidate? |
|
This feature is very usefull, I would like to see this merged. |
|
@fprochazka If there is real chance for this to be merged I will surely rebase it and write tests. |
|
I think it is useful. |
There was a problem hiding this comment.
I know that message is commonly used, but would not it be more accurate to use description?
Btw, use please single quotes where possible. For optional arguments is the best to use NULL.
There was a problem hiding this comment.
I can rename it but message is common name in this context.
I will change it to single quotes
043a930 to
a36e3d1
Compare
|
Rebased, fixed and tests added. |
|
👍 One more thing, update commit message please to be consistent. I'm thinking, if a longer backtrace has some influence for somebody, but probably not. |
|
failMessage is imho bad name. Is new method really needed? |
|
Idea: what about passing |
|
From my POW separate this to method is better. It is only private so it will not change signature of class and I think it improves readability. Maybe rename it to @milo: This solution expects all parts of message to be equivalent. But I think they are not and in future there could be different processing for mail reason and fail description (f.e. different colors in output). |
|
Maybe stupid idea, but if some method is necessary, what about |
|
@milo: What change in commit message do you mean? |
|
Conversation about failMessage is here #133 (comment) but is hidden in outdated diff. |
|
@dg I see! @MartinMystikJonas |
|
Verb is better. What about describe() ? |
0f58284 to
740455f
Compare
|
👍 |
|
@MartinMystikJonas please, add tests for |
740455f to
425e1d1
Compare
|
done |
|
Thanks |
Optional custom fail messages in asserts
|
@MartinMystikJonas Thank you! |
Sometimes test results can be hard to understand without detail examination of test code.
For example:
You just get something like:
Only information about what caused tha failure is line number on last line.
Also there are some cases when assertion is executed multiple times (for example asserting something for every item in collection) where we need to know for which item test failed.
Therefore it is usefull to have option to add custom fail messafe to asserts.
And then you get:
You jsut get something like:
It could save you lot of time in examining test results.