Chris Johnston

Web development and design with a little VFX thrown in for fun
  • Home
  • About Me
  • Contact Me
  • Projects
  • Resume

Naming unit tests

Published by Chris Johnston on September 3, 2008 09:47 pm under Agile, Test Driven Development

Sarah Taraporewalla has given some advice on naming tests on her blog,

My biggest problem with test names is that they may be perfectly fine when you write the code for the first time, but then…someone starts refactoring the code base. Tests start failing…and you fix the failing test…but not the test name.

My take home advice – think about what your test is doing before you begin to write the test. Perhaps you even write the names for 4 tests at once, before implementing any of them. That way you can focus on the test at hand, you don’t have to be thinking of testing a whole variety of cases in the one test and your tests become small enough that you can easily identify when the test name differs from the test at hand.

I completely agree with her, except in those instances where I am not sure what it is I am testing.

One habit that I picked up on my last project was naming a test after I had written it. While creating the unit test, I would simply name it foo(). Then, once I saw what the code was actually doing, I would go back and rename the test to something appropriate.

I find that names can get out of sync the code incredibly quickly. You create a name and by the time you finish writing the test, the code is testing something else. This is where naming tests after can be an advantage.

I am not sure if this highlights a deficiency in my understanding and implementation of TDD, but I find that even if I think about what I am testing before writing the test, the final unit test reflects something different. My understanding of what needs to be tested evolves right along with the code. And hence, writing test names after results in better test names.

2 Comments so far

  1. Sarah Taraporewalla on September 4th, 2008

    Perhaps if you don’t know how you want the code to behave, then you haven’t thought through the design of your story enough. If find if you test the behaviour, rather than what it is meant to do, then you usually know what you want to happen. Sometimes, I decide to push implementation down to other services, helpers, or lower down the stack, but I keep the original test inplace, and also have the test at the required unit level.

    Of course, there are always going to be those times when you really don’t know how the code will hang together. Pat Kua call these times experimentation http://www.thekua.com/atwork/2008/02/05/if-you-do-test-driven-development-all-the-time-youre-doing-something-wrong/. Take his advice – use TDD when you are focused on the problem, and know how to solve it; use experimentation to find out how to solve it (and then quite possible, revert all your changes and begin TDD)

  2. Alex Hung on December 9th, 2008

    I agree with Sarah. What I’ve found is that my unit test names are primarily driven by the story requirements. I extensively use the “Should…” paradigm to make sure I (and my pair) focus on the why, and not what or how. This way, the names tend to stay the same because I am implementing tests to confirm requirements are satisfied, and leave the implementation details to the code.

Posting your comment.

  • Search

  • Categories

    • .NET (2)
    • Agile (41)
    • Apple Mac (15)
    • Application Development (124)
    • Articles (4)
    • ColdFusion (2)
    • Demo/Tutorial (3)
    • Eclipse (1)
    • Flash (6)
    • General (567)
    • Git (1)
    • Google (1)
    • Hibernate (4)
    • J2EE (39)
    • Java (111)
    • Java Frameworks (5)
    • Links (1)
    • Linux (33)
    • Miscellanous (2)
    • NetBeans (3)
    • News (10)
    • Open Source (6)
    • Photography (2)
    • Programming (33)
    • Python (1)
    • Ruby (27)
    • Ruby on Rails (14)
    • Ruby on Rails Web Apps (1)
    • Software (14)
    • Spring (4)
    • Teaching (1)
    • TeamDocs (6)
    • Technology (2)
    • Test Driven Development (1)
    • Thoughts (33)
    • ThoughtWorks (8)
    • Tips and Tricks (1)
    • User Experience (1)
    • Web Design (7)
    • Web Development (37)
    • Wicket (1)
  • Archives

    • September 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (7)
    • March 2009 (2)
    • February 2009 (6)
    • January 2009 (4)
    • December 2008 (3)
    • October 2008 (1)
    • September 2008 (2)
    • August 2008 (6)
    • July 2008 (4)
    • June 2008 (1)
    • May 2008 (8)
    • April 2008 (7)
    • March 2008 (2)
    • February 2008 (1)
    • January 2008 (5)
    • December 2007 (3)
    • November 2007 (4)
    • October 2007 (5)
    • September 2007 (2)
    • August 2007 (3)
    • July 2007 (6)
    • June 2007 (5)
    • May 2007 (5)
    • April 2007 (5)
    • March 2007 (6)
    • February 2007 (9)
    • January 2007 (16)
    • December 2006 (6)
    • November 2006 (15)
    • October 2006 (17)
    • September 2006 (27)
    • August 2006 (22)
    • July 2006 (14)
    • June 2006 (10)
    • May 2006 (18)
    • April 2006 (3)
    • March 2006 (6)
    • February 2006 (15)
    • January 2006 (7)
    • December 2005 (11)
    • November 2005 (8)
    • October 2005 (18)
    • September 2005 (24)
    • August 2005 (18)
    • July 2005 (21)
    • June 2005 (14)
    • May 2005 (23)
    • April 2005 (18)
    • March 2005 (34)
    • February 2005 (27)
    • January 2005 (27)
    • December 2004 (15)
    • November 2004 (17)
    • October 2004 (20)
    • September 2004 (10)
    • August 2004 (21)
    • July 2004 (9)
    • June 2004 (11)
    • May 2004 (4)
    • April 2004 (15)
    • March 2004 (12)
    • February 2004 (7)
    • January 2004 (17)
    • December 2003 (11)
    • November 2003 (8)
    • October 2003 (12)
    • September 2003 (12)
    • August 2003 (12)
    • July 2003 (23)
    • June 2003 (22)
    • May 2003 (14)
    • April 2003 (9)
    • March 2003 (22)
    • February 2003 (24)
    • January 2003 (32)
    • December 2002 (11)
    • November 2002 (16)
    • October 2002 (10)
    • September 2002 (9)
    • August 2002 (13)
  • Pages

    • About Me
    • Contact Me
    • Projects
    • Resume

Copyright © 2010 Chris Johnston
WordPress Theme based on Light Theme