At the Elevate presentation yesterday I learned a lot about C#3/4 by Magnus Lidbom.
But as a side-effect I also picked up a nifty syntax for NUnit assertions. It’s called Constraint-based Assertion Model and has been around since NUnit 2.4. Which shows that I am a slow adopter… Sad.
OK – what’s the deal with it? It gives you a almost fluent interface to assertions. Here is an example on how to do a simple assertion in the old style:
And here is the same assertion in the Constraint-based version:
Now read it out loud; Assert… That … i … is equal to 10. Nice, isn’t it? I like that a lot.
3 comments:
I get cobol feelings. Too many words.
I prefer:
assert i == 10
/christer
Disagree - even though I don't prefer COBOL...
But it reads so nice...
But I also get what you mean, I sometimes use the "keep it short and concise" argument to get out of VB.NET.
Is "assert i == 10" a known syntax for any testing framework?
Groovy example:
http://groovy.codehaus.org/Quick+Start
Cobra example:
http://cobra-language.com/docs/python/
(see Unit tests one page down)
Boo example:
http://boo.codehaus.org/Slicing
These are NOT framework examples, but integrated in the languages,
as assert should be.
Please note, expected value, actual value, file name and line number can still be seen if the test breaks.
I much enjoyed the groovy book, as it was the first ever to use asserts everywhere, instead of old school comments and printouts.
/christer
Post a Comment