Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interesting / well written unit tests to read (literate programming)

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)..

like image 691
Jörg Haubrichs Avatar asked Jun 10 '10 19:06

Jörg Haubrichs


People also ask

What are good unit tests?

Good unit tests should be reproducible and independent from external factors such as the environment or running order. Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

Should you write unit tests before code?

It often makes sense to write the test first and then write as much code as needed to allow the test to pass. Doing this moves towards a practice known as Test-Driven Development (TDD). Bluefruit uses a lot of TDD because it helps us to build the right product without waste and redundancies.

What are the three essential A's related to unit testing?

The AAA (Arrange-Act-Assert) pattern has become almost a standard across the industry. It suggests that you should divide your test method into three sections: arrange, act and assert.


2 Answers

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.

like image 38
ire_and_curses Avatar answered Sep 23 '22 01:09

ire_and_curses


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.

like image 167
JSBձոգչ Avatar answered Sep 27 '22 01:09

JSBձոգչ