Jesse has written a great blog post on using test builders to improve the expressiveness of unit tests. He has convinced me that builders are useful and that they provide an easy way to construct objects that are in a valid state. Testing can be hard; I find defining a method for each of the object’s properties to be tedious and repetitive. This is especially problematic if you’re working with an object with dozens of properties.

To balance expressive with conciseness, I’ve refactored the builder to use Python dictionaries

I find this code to be just as expressive, easier to use and most importantly not repetitive.

Share and Enjoy:
  • del.icio.us
  • Reddit
  • Facebook
  • Identi.ca
  • TwitThis

Related posts:

  1. Introducing Cushion, a simple python wrapper for CouchDB
  2. Navision and Convergence 2007
  3. Dependency Injection
  4. First the Resume, now the Interview

2 Comments on 'Simple, Expressive Builders in Python' read them below or add one

Subscribe to comments with RSS or TrackBack to 'Simple, Expressive Builders in Python'.

  1. with_()? Ew.

    I do like builders though, I use them for more complicated tests in .NET as well.

    James Thigpen - 11 Aug 10 at 4:10 pm

  2. I think I can refactor that away, introspect the method name.

    shey - 13 Aug 10 at 6:37 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Search