18

Are there any open source applications that are developed using test driven development that serve as models of how good unit testing should work?

I'd prefer to see examples in C# and .NET. (Note that I mentioned applications, not just libraries.)

I'm a mid-tier programmer who really wants to believe in and practice TDD. The app I work on in my day job is pretty complicated--about 1 million lines of code--and I would love to introduce more unit testing. We have some unit tests in place, but my efforts at TDD and with working on code that's already under test have not been encouraging.

In my admittedly limited experience, TDD seems to encourage a lot of complexity in the name of decoupling. The bits of the app that are hard to test--and which coincidentally tend to be critical--get pushed out to the periphery, into the realm of integration tests that may or may not ever get written. (I'm thinking of the usual suspects here, file system access, hydrating objects from a database, asynchronous web calls, etc.)

The code that's under test tends to involve lots of collaboration among objects, and maybe some simple flow logic, all of which happens in memory and which could arguably be written in a simpler, more understandable way if everything didn't have to be totally decoupled for testing.

I understand the techniques for mocking dependencies and such, but in my experience heavy use of mocking leads to very brittle tests. If my first instinct upon seeing a bunch of tests go red is, "Great, now I have to fix all the mocks," then my tests have become a drag rather than a safety net.

I'm trying to get past this mental barrier, and as part of that I'm reading Michael Feathers' book, Working Effectively with Legacy Code. I it hope will show me some of what I'm missing.

I'd also like to study some nontrivial .NET applications with good code coverage, maybe a content management system, or a CRUD app. The FitNesse testing framework that Uncle Bob talks about is something I'll probably look at, but it would be nice to see something written in the language I'm most familiar with.

Any suggestions or words of wisdom would be appreciated.

Josh Earl
  • 553
  • 3
  • 12
  • 1
    Duplicate of http://programmers.stackexchange.com/questions/21791/good-example-of-complex-code-using-tdd – liori Apr 18 '12 at 17:53
  • 2
    Not quite... I'm interested in seeing examples of real world applications. The accepted answer in that post recommends a testing framework. I've seen some examples of frameworks and libraries before, but that doesn't address my question. – Josh Earl Apr 18 '12 at 18:00
  • @JoshEarl - I agree.. I dont think my answer below would have been relevant to the other post – hanzolo Apr 18 '12 at 18:02

3 Answers3

14

I don't know whether TDD was used, but a stellar example of testing is sqlite which has a remarkable 100% branch coverage, and has more than 1000 times more test code and scripts than product code.

Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
  • 4
    that sounds like a remarkable waste of effort if their ratio of code to test code is that high – Ryathal Apr 18 '12 at 20:35
  • 6
    @Ryathal: The cost of failure dictates the testing effort, not the length of the tested code. Given wide enough usage in sufficiently mission-critical approaches, the testing might have been worth it. I'm not really sure, though, if SQLite didn't overdo it. – thiton Apr 18 '12 at 20:44
  • 3
    sqlite is definitely mission-critical for a lot of people. You might be surprised how often it's used (OSX, iOS, Android OS for example). And you have to realize, a lot of those lines of test code and data were probably machine generated. An industrial strength database has a *lot* of edge cases. – Bryan Oakley Apr 18 '12 at 22:17
  • 10
    SQLite is *the* most widely used SQL database on the planet, it's installation base is an order of magnitude bigger than MySQL, PostgreSQL, SQL Server, Oracle, DB2 *combined*. It's probably one of the most widely used pieces of software, period. If there were a bug in SQLite, every single smartphone on the planet would stop working. I think that justifies a little paranoia. – Jörg W Mittag Apr 19 '12 at 14:50
  • @JörgWMittag wow, I never knew that about SQLite. Amusing. Thanks. But 1000 times more test code. That is incredible. – mike rodent Apr 24 '16 at 17:44
4

The Orchard project might be what you're looking for.. It's a .NET / MVC / TDD app that is fairly complex, but not too bad and it demonstrates some good practices and patterns..

http://orchard.codeplex.com/wikipage?title=solution

and I guess the updated link:

http://docs.orchardproject.net/

hanzolo
  • 3,111
  • 19
  • 21
0

A lot of third-party, open-source libraries are TDDed. Hibernating Rhinos' libraries, such as RhinoMocks and NHibernate, are TDDed by their developers using XUnit as the unit-testing framework.

Now, with OSS, you usually can't guarantee that EVERYTHING in the product has been TDDed. The community may have standards including this practice, and they may even use a build-bot that performs code coverage metrics, but TDD is a mentality that must be bought into by the developer, and with the public at large able to commit to open-source VCS trunks like those on GitHub, anyone can commit any change with any amount of test coverage (or if coverage is required the tests could have been written after the fact, which is a violation of the spirit of TDD but there really isn't a good way to catch it on commit).

KeithS
  • 21,994
  • 6
  • 52
  • 79