My Pensieve RSS

If you are wondering what pensieve means watch Goblet of fire

Archive

Jun
14th
Sun
permalink

Why we should not name tests as tests

Conventions… I have always been keen on asking why we need to follow a convention and is there a better way to do it. This is test name convention is something I have been noticing people following for a long time. When we write test people always tend to name class names and method names sometimes as test and I don’t think that is the right thing to do and here are my reasons why.

I have always been careful in naming things in code. As uncle bob says name is everywhere. We name variables, class names, method names, file names, namespace names but for me test names are really special as I work with them a lot.

As I work I have started to imagine tests as conversation with application or SUT. Lets say we are writing tests for a printer. Normally I have seen tests like

PrinterTest -> shouldPrintOnBothSides

(Now with new NUnit and JUnit the test in method name is dropped and also should is a BDD convention). But the problem with this way of writing tests for me is I am not communicating my expectations on what I am trying to write in the production class. Rather I am expecting the test to do something for me.

Instead of this how I really like to name test is

Printer -> use_both_sides_of_paper_when_configured

I tend to use snake casing rather than camel casing when possible even in C# or Java as it is more readable. Also I use the same production class name for the test class name as I feel this shows my intent of expecting and conversing with the production class (I also feel there will be high cohesion inside a test). I don’t think naming the two classes as they are in two different namespaces or packages. So if I need to use Printer class here I use it as Impl.Printer. It is a small trade off I need to make for the intention revealing tests I can write. I also don’t use lots of should in test names as it becomes too imperative for me.

Sometimes when I am writing a class and I expect it to return or do something something I tend to write it as

Printer -> give_me_no_printout_when_I_give_print_and_you_run_out_of_paper

That’s my way of writing tests. Let me know if you like it or otherwise and please discuss the reasons. For the last set of posts I have been people replying I don’t agree with you but missing why. I would love to hear the reasons from you as well :).

Comments (View)
blog comments powered by Disqus