Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Every once and a while I find myself teaching others some techniques useful for writing effective unit tests (usually in Java where many people find writing tests challenging). However, I have yet to find a small-but-not-too-small problem to use to demonstrate the techniques. Likewise, I have not yet found a great problem we can use to practice writing tests. Some problems I have tried have felt completely unrealistic or irrelevant to real life.

Does anyone know of a great problem I can use which is small but complex enough and realistic enough to use to teach and practice with?

In this case small = low enough complexity to be a approachable for most developers, not necessarily easy to finish in a short amount of time. It is okay if we don't finish implementing a solution during the workshop.

share|improve this question
    
function that checks whether a string is a palindrome a man, a plan, a canal - Panama! –  gnat Jul 28 '11 at 9:33
add comment

closed as off-topic by gnat, GlenH7, MichaelT, Ozz, Jim G. Sep 26 '13 at 3:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about what language, technology, or project one should take up next are off topic on Programmers, as they can only attract subjective opinions for answers. There are too many individual factors behind the question to create answers that will have lasting value. You may be able to get help in The Whiteboard, our chat room." – GlenH7, MichaelT, Ozz
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

up vote 1 down vote accepted

I have once implemented a restaurant search and booking website in Ruby on Rails. Writing the tests for this was quite interesting, since it involved relative complex test data to make sure that the system would find the right restaurant and avoid overbooking.

I guess it could be simplified easy enough to fit in a workshop. For the first step you have restaurants which fit into several categories:

  • Name
  • Place (city where restaurant is located)
  • Cuisine (Asian, American, Chinese, French, Greek...)
  • Extras (Casual, Brunch, Garden, Terrace...)

Options could either be selected or let free. You need test data with several restaurants having different combinations of attributes and test cases for searching restaurants and make sure the right ones are selected.

It gets more difficult if you add bookings. In this case each restaurant has a number of free places for a given date (actually this was sliced in two hour pieces, but I would let this out).

Again you need test data for restaurants with no bookings, partially booked and fully booked. People can search and make bookings and the system must make sure that depending on how many places somebody wants to book no overbooking happens.

Another real life example would be a simple shop system, where people can search for items (groups, articles...) and order items and the system must ensure that only items can be ordered that are in stock.

These were examples from websites, but I think you can apply similar test problems to gui applications, for example a booking system for hotel rooms (number of beds, balcony, smoker...)

It can be made more complex with further details, like sending an affirmation email after a booking or order was placed by a customer, an automated billing system with calculating prices and taxes or similar tasks.

share|improve this answer
    
I like this problem. It can start out relatively simple, everyone can relate to it, and it is easy to add additional complexity as necessary. I'll give it a try and see how it goes! –  Jim Hurne Aug 7 '11 at 9:47
add comment

You might want to check out Jon Jagger's fantastic Cyber-Dojo.

It's a web based integrated environment for doing deliberate practice of Test Driven Development and learning about team dynamics. It has lots of small programming tasks (kata's) selected specifically to help people concentrate on the process of TDD and not the problem. It also supports a range of languages, from Python and Ruby to Java and C++.

The best thing is, after doing a kata you can then go back and look at the red/green progression (or maybe not *8') of each of the groups participating. It's traffic lights are a great way to visualise how the TDD process works.

If you want your own CyberDojo server, the whole project can be found at github and there is even a Turnkey Linux appliance virtual machine linked from there, which means that assuming you already have VMware player or VirtualBox installed, you can be up and running within a few minutes of downloading the appliance!

share|improve this answer
    
This sounds like a really fantastic tool. I'm definitely going to check it out. –  Jim Hurne Aug 7 '11 at 9:47
add comment

All Code Kata examples are good for this. There are lots of code kata. Pick one.

http://codekata.pragprog.com/

share|improve this answer
1  
Is there one that you have found is particularly well suited for teaching? –  Jim Hurne Aug 7 '11 at 9:41
add comment

I like to show the importance of Unit Tests with Refactoring. Just use a simple MVC Structure that has two Controllers using the same Model.

Then if both the Controllers and the Model have Tests and all works great, you let them make changes in the model to adjust to a new feature in on of the controllers. Then the unit tests should fail due to the model having changed.

This shows that with a growing complexity in a project, Unit Tests to save you tons of time with both debugging and testing since you always know where a test fails.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.