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