Unit test names describe features
I have been working with unit testing for several years and I always had problems in finding the best names for my tests. I have been trying different conventions and I ended up with the practice of naming tests based on the behavior they test.
For the following class:
public class SamePasswordsValidator { public boolean isValid(PasswordAware value) {...} }I would create a test:
public class SamePasswordsValidatorTest { @Test public void shouldReturnTrueWhenPasswordsMatch() {...} }Or:
public class SamePasswordsValidatorTest { @Test public void shouldBeValidWhenPasswordsMatch() {...} }When creating the names I think of the scenario and the expected behavior. One thing that still I am not sure about is the word "should", that is repeated (prefix) in all test names (DRY?). So maybe we "should" try to remove it:
public class SamePasswordsValidatorTest { @Test public void isValidWhenPasswordsMatch() {...} }
References
- Growing Object-Oriented Software, Guided by Tests (http://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627/ref=sr_1_1?ie=UTF8&qid=1373800858&sr=8-1&keywords=guided+by+tests)
- BDD (http://en.wikipedia.org/wiki/Behavior-driven_development)
Hi saw your comment from another blog, where I was directed here.
ReplyDeleteWhy not append Should to the end of the test class name, that way you won't need to prepend should to every test methods.
Thanks for the comment.
DeleteSounds like an idea. It is just a convention. If you like the one you describe and it makes your code better to read I would say go for it. On the other hand, I prefer tests end with *Test (kind a convention), so for me it would not work. But if you like it, as I said, go for it!
I prefer the last one, so not should at all.