Thursday, July 14, 2005

Running Tests to Watch Them Fail

I was reminded again today by a fellow ThoughtWorker why in Java it's a good idea to write the test, run it to watch the test fail, then make it pass: what if you mis-spell the testing method, i.e., "tsetMyMethodToSeeWhatItDoes()"? Because JUnit uses reflection to find the methods to run as tests, it won't pick up "tset".

Which is why I think that NUnit v2 got it righter (is that a word?) by using annotations to mark the test methods instead of naming patterns. If you misspell the annotation, the compiler complains, which, in this case, is A Good Thing. Improvements are already afoot in the Java world -- TestNG already supports annotations (and JavaDoc tags if you are using 1.4) and several other features, and rumor has it that JUnit 4 will support annotations and some of the other TestNG features as well.

2 comments:

Alistair Jones said...

A lot of people (me included) have their IntelliJ "test" intention set to generate a test method containing just fail(). Running this test and watching it fail seems good discipline before replacing fail() with something more useful. It verfies a lot more than just spelling "test" right: all sorts of infrastructure, and also that your hot key (often Shift-F10 for me) is pointing to the right class.

Neal Ford said...

That's a great IDEA.