Hacker News new | ask | show | jobs
by ZeroGravitas 4479 days ago
PHPUnit has assertions for matching against CSS selectors, assertSelectCount and friends.

http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.ht....

Which is basically turning them into AssertTag calls behind the scenes, which is good as they can be cumbersome to write if not very simple.

The main problem I had with it was the poor error messages it provides on failure (basically "false != true" rather than printing out what it was looking for and what it had found to provide context).

It doesn't look like Nette is going to give any better error messages since it's just passing true or false into a simple Assert.

1 comments

You're supposed to provide the context message yourself if necessary, I don't think PHP has the capabilities to do this automatically for you anyway. PHPUnit makes this really easy.

    $this->assertTrue($something, '$something called from WhateverClass returned false')
If there's a specific assert that fits you should always use it instead of assert true, as well as saving typing and typos, one of the key benefits is better error messages e.g assertContains()

Failed asserting that 'foobar' contains "baz".

If that string "foobar" was generated by your programming then seeing it in that message might be all you need to diagnose the problem, maybe it was just a typo, maybe there's several words missing, whatever tyhe problem is this gives you vital context

If it just said "assertion failed" or "true != false" or even a witty message written by hand with no actual knowledge of what has gone wrong in this particulainstance you'd have to spend time establishing that the string got as far as the test and what it contained.

Rather than typing out a message each time you can even write your own assertions that do it for you based on the values you pass in.

You also get a chance to document it with the function and class name, which again can be automatically shown, e.g WhateverTest::

Your last point there has been how I've tackled it. My test methods/classes are all well named and broken up into particular chunks so that even if the test failed message isn't very useful, the context it failed in answers it 90% of the time :)