I’m working on a couple of minor personal projects at the moment, and I’m using them as an opportunity to try and hone my skills at TDD/BDD style development. Even though I first heard about TDD some years ago, I haven’t yet made the breakthrough where it becomes automatic. I guess I’m still at the conscious competence stage of skill acquisition. I figured it would be useful to share a couple of things that I’ve found to be useful.

The first is a naming convention from JP Boodhoo, where my test method names start with the name of the primary method that is being tested.

The second is to write out the specifications for the class I’m writing as a series of declarative statements, each of which becomes a test method in turn.

For example, I’m working on a library to construct data entry forms which includes a PanelBuilder object. The specifications for the PanelBuilder read something like this:

  • Constructor requires IControlFactory
  • Constructor initialises ControlFactory property
  • Constructor initialises IControlFactory implementation
  • AddStringField requires BindingExpression
  • AddStringField returns IStringFieldBuilder
  • AddDateField requires BindingExpression
  • AddDateField returns IDateFieldBuilder
  • and so on …

In turn, the specifications for StringFieldBuilder read like this:

  • Constructor requires IControlFactory
  • Constructor requires BindingExpression
  • Constructor creates StringField using IControlFactory
  • WithLabel requires Caption
  • WithLabel creates Label using IControlFactory
  • ReadOnly configures StringField using IControlFactory
  • ReadOnly configures existing Label using IControlFactory
  • WithLabel configures Label as ReadOnly using IControlFactory
  • and so on …

One of the things I’ve found with this approach is that sometimes writing a simple statement to capture the required behaviour is difficult. Usually when this happens, it’s an indication that the class is doing too much - that I’ve violated the Single Responsibility Principle and the class needs to be refactored.

How do you name your test methods?

Comments

blog comments powered by Disqus
Next Post
The Best RSS Reader  11 Aug 2008
Prior Post
Tool Proficiency  04 Aug 2008
Related Posts
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
August 2008
2008