Ian Cooper

Sponsors

The Lounge

Advertisement

Making your code easier to understand context/specification style unit tests

When we started our current project we did not use context/specification style testing, instead we used testcase-per-class with a four-phase test model (also known as arrange-act-assert). Although we followed story-test driven development (STDD) we were not explicitly Behavior-Driven Development BDD when we set out. Over time I began to see the path between STDD and BDD we shifted toward a BDD approach.

That is really background for what comes next. As part of this shift we started writing more context/specification style tests. We did not re-write our existing testcase-per-class fixtures (then and still now I would see the possibility of using multiple test organisational patterns within a project; some of our tests still seem better with testcase-per-class). Recently we have had new developers join the team. One interesting, if anecdotal, observation has been that those developers find it easier to use tests as a source of documentation for what the software is doing when they are in the context/specification style. This is a useful observation, because the rest of the team is able to supply much of the context for testcase-per-class fixtures by virtue of having worked on the codebase and so do not see the lack so easily.

To build a maintainable system you have to build one where the behavior can be understood. Tests offer this promise, but often we have found that tests failed us, because the tests were no easier to comprehend than the code under test. Context/specification style tests seem to be better on this account. Of course there is no silver bullet in software. You still have to put the effort into making your context/specification tests readable, but the form seems helpful toward achieving that goal.

I'll try to post more on my experiences with BDD style approaches as they come out. I would also recommend that anyone interested in the topic look at the RSpec book from the Pragmatic Programmers which offers a great overview of the topic.

 

 

 


Posted Mon, Jun 29 2009 5:08 AM by Ian Cooper

[Advertisement]

Comments

David wrote re: Making your code easier to understand context/specification style unit tests
on Mon, Jun 29 2009 7:46 AM

We've been through a similar thing at my work -- starting out with testcase class per class, then switching to a more scenario-based testing style. We haven't gone full context/spec or BDD, but I feel we're at a pretty decent intermediate step at the moment.

Here's my write up of our approach:

www.davesquared.net/.../moving-to-scenario-based-unit-testing.html

</shameless blog plug>

Mike Murray wrote re: Making your code easier to understand context/specification style unit tests
on Mon, Jun 29 2009 12:34 PM

Have you looked into Aaron Jensen's MSpec here on CodeBetter?  I'm trying it out on my own and like the direction MSpec is heading.

For me, it makes the use of unit testing frameworks feel more natural as part of the behavior and specifications that frequently come down from the suits.  It is a more high-level testing (yet more readable) that can be supplemented with regular class-driven unit testing when business requirements for a certain module are more vague.

Daniel Fernandes wrote re: Making your code easier to understand context/specification style unit tests
on Tue, Jun 30 2009 7:43 AM

Agreed at 100%.

Traditional TDD style tests are fine for driving design of a purely technical piece of code where you can probably guarantee that you're always within the realm of unit testing and not diverging too much into integration testing.

I think that the different forms of tests organization as advocated in BDD are very useful in that most often than not the organisation will closely match the various specifications a system/component is supposedly implementing.

The only issue in BDD is to find the various core contexts that are worth setting up and then testing.

But those contexts are really the state parts of the application can be in and if you can do that properly you will have highly tested AND maintainable code.

Daniel

Ian Cooper wrote re: Making your code easier to understand context/specification style unit tests
on Tue, Jun 30 2009 1:20 PM

@Mike

I have not looked at MSpec but noting that someone else has posted about MSpec and Boo might well take a look

Larry Battle wrote re: Making your code easier to understand context/specification style unit tests
on Sat, Jul 4 2009 11:37 PM

When you release your code, what happens to the test functions?

Does it go into your documentation and ship it or do you erease it because it's a one time thing?

I'm new to the unit testing.

Scott wrote re: Making your code easier to understand context/specification style unit tests
on Sun, Jul 5 2009 10:12 PM

Agreed on multiple test organizational patterns per project. c/s lends itself very well to business-value-driven design, and feels forced when you drop into "internals" or integration-style tests, where class-per-fixture begins to make more sense. I think it's a shifting target, and ou have to constantly evaluate what you are getting out of the practice and how you can improve it.

nursing jobs wrote re: Making your code easier to understand context/specification style unit tests
on Mon, Jul 13 2009 11:15 AM

I am not really sure if best practices have emerged around things like that, but I am sure that your great job is clearly identifed. I was wondering if you offer any subscription to your RSS feeds as I would be very interested and can’t find any link to subscribe here. Please come visit my site  <a "href=http://www.nursingnurse.com ">nursing programs</a> when you got time.

notaries wrote re: Making your code easier to understand context/specification style unit tests
on Mon, Jul 13 2009 11:21 AM

This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site  <a href="http://www.americanotaries.com">notary public</a>  and give me any valuable feedbacks.

security companies wrote re: Making your code easier to understand context/specification style unit tests
on Mon, Jul 13 2009 11:23 AM

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me. Thanks for all your help and wishing you all the success in your business.  Please come visit my site  <a href="http://www.neurologistlisting.com">pediatric neurology</a> when you got time.

pool cleaner wrote re: Making your code easier to understand context/specification style unit tests
on Tue, Jul 14 2009 3:56 AM

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me. Thanks for all your help and wishing you all the success in your business.  Please come visit my site  <a href="http://www.supplypools.com">swimming pool equipment</a> when you got time.

replacement window wrote re: Making your code easier to understand context/specification style unit tests
on Tue, Jul 14 2009 4:13 AM

You got a really useful blog I have been here reading for about an hour. I am a newbee and your success is very much an inspiration for me. I would love some feedback on my site<a href="http://www.treatmentwindows.com">window repair</a> when you got time.

Public Relations wrote re: Making your code easier to understand context/specification style unit tests
on Fri, Nov 13 2009 2:44 PM

thanks for the post! everyone could use better coding.

Public Relations wrote re: Making your code easier to understand context/specification style unit tests
on Fri, Nov 13 2009 2:48 PM

Finally! Someone who actually knows what they are talking about! Thanks for the info.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?