7

Do you know any open software projects that had particularly interesting / well written unit tests ?

Writing unit tests often feels odd to me, because it seems either too random, too dense, to sparse, .. It would be great to read some real world examples (rather than books, library example code etc)..

Jörg Haubrichs
  • 2,215
  • 3
  • 24
  • 26
  • try to look at Behaviour Driven Development, e.g. the Cucumber project is a great example of an external Domain Specific Language, which aims at creating acceptance tests in a language close to the natural language. – Gabriel Ščerbák Jun 10 '10 at 19:54
  • Yes, I appreciate the beauty of the various DSLs that are available for ruby projects and testing, and the different paradigms they illustrate. The critical point though are the parts where you need to be accurate and the complexity is high, rather than sequential testing like with cucumber. – Jörg Haubrichs Jun 10 '10 at 22:03

2 Answers2

2

I've found this blog post by Misko Hevery to be very useful, especially since he includes links to his actual source code with very well-written unit tests.

Update: Unfortunately, the links in his post are broken now. However, you can find the current source file with a search at http://code.google.com/p/testability-explorer/source/browse/trunk/testability-explorer/src/test/java/com/google/test/metric/collection/KeyedMultiStackTest.java.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
2

I recently had the problem of detecting overlapping date ranges in Python. I was deeply impressed by the elegance of this solution, and in particular the unit tests that accompanied it. They are short, clear, and exhaustive. Also, drawing the cases in ASCII art in the docstring is a stroke of genius, in my opinion.

The only change I made was to split each assert into a separate test, for improved isolation.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • The unit tests in the link you provided do look very nice and clear. However it must be said that writing unit tests for a function that returns a boolean value based on four parameters with no external dependencies is a joy but in real life I have been much more often faced with scenarios that are far more complex. – trendl Jun 10 '10 at 20:40
  • I agree, although this is really beautiful and compact, the hard parts are dealing with various dependencies (like the infamous database related tests) that exist in real applications. – Jörg Haubrichs Jun 10 '10 at 22:09