Slashdot Mirror


Test Driven Development Examples?

esnyder queries: "I find the pragmatic/agile/XP hype about test driven development compelling, but find it hard to see how to test first (or even unit test at all) in some situations. I would like to explore some extended examples of it in a moderate to large scale real world codebase to improve my test design skills. Can anyone recommend some F/OSS software projects that consistently use test driven development processes that I could check out? Preferable over 50K lines of code, but I'd welcome pointers to anything that people think would be helpful."

74 comments

  1. eating your own dogfood by anomalous+cohort · · Score: 3, Insightful

    Nothing reality tests the usability of a proposed API then writing unit tests against it.

    For those who develop in Java, may I propose JUnit? If you want to test the GUI of a web server, then try HTTPUnit although the value of writing unit tests to this is less since GUI is usually subject to a lot of changes over time.

  2. Zope3 by eukreign · · Score: 3, Informative

    Take a look at Zope3 it has some 5,000 unit tests:
    http://cvs.zope.org/Zope3/

    --
    Free Talk Live: Talk Radio where YOU ar
    1. Re:Zope3 by axxackall · · Score: 3, Interesting

      Is it the reason it took forever to develop it? 3X was promised last summer, but it's still in its "early stage". E17 is being developed faster.

      --

      Less is more !
  3. Start Small And Move Up by Bloodshot · · Score: 4, Interesting

    Where I work we're coding a dating site. Currently, it's up to about 180K lines of scripts written in PHP. However, not a lot of it was written with unit testing (and test-driven development) in mind. I'm a big believer in unit testing but it's been hard to get the other members on board as they complain how hard it is to write unit tests for web pages. Bah.

    So, most of the library functions that I wrote (stuff like except an integer, return a text string from a list) have been unit tested by myself, and every time I change a function or a class, I try and write a unit test for it.

    Seriously, you just need to dive right in and think about how you make your code easy to test. I use the SimpleTest testing framework (it's PHP), and I always feel good when my array of tests all run correctly when I make a major change to the code that impacts a huge portion of the site.

    If test-driven development has done one thing, it's forced me to carefully examine my code to create a way to make sure it is actually working according to the business logic we've been asked to implement.

    1. Re:Start Small And Move Up by Anonymous Coward · · Score: 0

      Yeah, testing ends up being a design process.

  4. Darn good question. by Godeke · · Score: 3, Insightful

    None of the projects I have looked at appear to have test first design. Some (especially CPAN items) have good testing of the functionality in place, but these are all modules inside a programming language. I have never seen a full on stand alone project with such testing built into the core.

    I find that odd really, considering that the point behind "write tests first" is to create an executable specification for functionality. If a new piece is desired, surely it would be easier for someone else to pick up the module with the tests and write code to pass the tests than it is to try to pick up the flow of a project from out of date documents, etc, and just contribute. I look forward to those who know of such projects, because I plan to examine them for help on how to implement these techniques myself. (My current project uses regression tests driven by an external test application... I have had this same question for some time, especially in regards to web applications).

    --
    Sig under construction since 1998.
    1. Re:Darn good question. by crisco · · Score: 1

      POPFile is a Perl application that tries to maintain 100% test coverage.

      --

      Bleh!

  5. Python by mbrubeck · · Score: 5, Informative

    The Python programming language and all of its standard library modules make extensive use of unit tests and TDD.

  6. Twisted by eukreign · · Score: 1

    Twisted is a networking framework in Python which has a lot of unit tests:
    http://www.twistedmatrix.com

    --
    Free Talk Live: Talk Radio where YOU ar
  7. Java example by jtheory · · Score: 4, Informative

    It would have been helpful if you'd mentioned the language you're using, and the types of applications (since of course both of those make a huge difference).

    If you're using Java for web development, I'd suggest reviewing the Struts Applications Project on SourceForge.

    It's a collection of documentation and applications using Struts that are really "done right" -- with documentation, sensible and scalable design, fully implemented testing (unit level AND on the HTTP level). I'm currently roaming through the AppFuse source in there -- it's basically designed to give you a complete setup to start building your app on, with common functions already built-in.

    From the site:
    AppFuse
    An application for starting your Struts-based applications. Checkout from CVS and execute "ant -Dapp.name=yourApp -Ddb.name=database" to instantly be up and running with a Tomcat/MySQL app. Uses Ant, XDoclet, Hibernate, JUnit, Cactus, StrutsTestCase, Canoo's WebTest, Struts Menu, Display Tag Library, JSTL and Struts (including Validator and Tiles). Features include CMA, Remember Me, Self Registration, Password Hint. The fuse to start your apps.

    --
    There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
    1. Re:Java example by esnyder · · Score: 1

      Mostly I write C++, Java, PHP and Perl. But I didn't want to limit the question by language, since I'm interested in pretty much any project using the methodology regardless of implementation platform. Thanks for the Struts Applications pointer!

      --

      Emile Snyder
      www.talentcodeworks.com

    2. Re:Java example by slymole · · Score: 1

      Practically every subproject (and derivatives) of the Apache Jakarta project uses automatic unit test suites as part of their build systems, whether with the Ant, Maven or Forrest tools.

      --
      "We don't stop playing games because we grow old; we grow old because we stop playing games.."
  8. Good idea, but a time-sucker by Cranx · · Score: 0, Insightful

    My experience has been that "unit testing" slows development to a painfully slow crawl. I find that the "gain" in quality I get is offset heavily by the reduction (or slow implementation) of features (usability, requirements, etc.). I did unit testing (once upon a time), and even developed my own test suite for C++, but I find that it catches VERY few bugs and I end up spending time writing unit tests AS WELL AS hunting down bugs the same old ways I always have. I just stopped bothering; I personally got little or nothing out of them.

    They are, however, sort of fun to write. Like picking lint from your belly-button.

    1. Re:Good idea, but a time-sucker by eukreign · · Score: 3, Insightful

      Unit testing my code has saved me hundreds of hours of debugging time, unit tests are also great documentation. In a big system where you can't keep track of all the code and how it works in your head, unit tests act as little documentation beacons showing how some piece of code works.

      --
      Free Talk Live: Talk Radio where YOU ar
    2. Re:Good idea, but a time-sucker by Nevyn · · Score: 5, Insightful
      I did unit testing (once upon a time), and even developed my own test suite for C++, but I find that it catches VERY few bugs and I end up spending time writing unit tests AS WELL AS hunting down bugs the same old ways I always have.

      Sorry to let you know but, you didn't write good unit tests and probably did waste your time. I've found very close to 100% of the bugs in Vstr a network IO string library using unit tests. That includes a couple of ones that would have been damn hard to track down otherwise.

      However it's been over a year since 1.0.0 which had a unit test for every function and every function option, to the last release which had over 99% code coverage found a couple of weird corner case issues (not just bugs, but optimizations that could never be reached for some reason). And going from 98% coverage to 99% coverage took a significant time investment, and required significant thinking about how the test should be written.

      As with much software development, it's easy to write simple tests that don't show much and aren't very useful. It's much harder to write tests that find bugs (and you have to appraoch writing the tests with a very different mindset to how you approach writting the code you are testing. This is not even close to being "Like picking lint from your belly-button."

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    3. Re:Good idea, but a time-sucker by rlowe69 · · Score: 5, Insightful

      My experience has been that "unit testing" slows development to a painfully slow crawl.

      This is a common miconception about test-driven development. Sure, when you look at the speed at which new features go in, "development" seems to be slowing to a crawl.

      But this is a very short-sighted and limited view of the whole project lifecycle. I've found that having great unit tests for my code actually help in the long run for adding new features. And that's not just adding the features to the code base, that's all the way to the end -- making sure they work properly.

      Nevermind the benefit they give while refactoring in an agile development process: the unit tests become a sort contract you can guarantee will work for objects that use your code, no matter the internals of a function. For refactoring this is invaluable.

      In short, you're not alone. Very few developers think long term like this and it's a shame. But once you drink the unit testing/test driven design (TDD) kool-aid I guarantee you'll never go back. I wouldn't think of writing code without unit tests any more and quite often it's TDD.

      --
      ----- rL
    4. Re:Good idea, but a time-sucker by Anonymous Coward · · Score: 1, Insightful

      Then you're not writing the tests correctly. In my experience the quality of code that has been developed in conjunction with (or better, after the tests are written) is consistently better than code that is written and never tested except as part of the greater context.

      The real win for unit tests is when you modify the code later on. If the unit tests pass, then you can be pretty darn sure that any code that depends on the modified code will continue to work. This has saved me hundreds of times.

      Read Testing Extreme Programming and Fowler's Refactoring.

    5. Re:Good idea, but a time-sucker by Cranx · · Score: 0

      Clearly, if you applied unit testing to your own vstr project, then you have the luxury and authority to spend as much time as you see fit crafting out helpful unit tests. It is, nonetheless, a luxury, not an essential task. Pair programming is another luxury that people would do well with, but I wouldn't call it a requirement.

    6. Re:Good idea, but a time-sucker by Cranx · · Score: 2, Interesting

      I've given it enough of a chance that I wrote unit tests for about 3 small projects and developed my own framework for it.

      I had three primary problems with it.

      #1: My biggest problems always revolve around the user interface. Users encounter FAR more bugs than my internals do, and I never found a good way to unit test GUIs.

      #2: I spent many hours developing tests for things that ended up rarely/never choking my tests, meaning they rarely/never caught any bugs. Yet, I still had bugs to work out. I found that it just wasn't worth the time.

      #3: Whenever I re-factor, unless I'm Mr. Perfect and all my interfaces are perfect from the get-go, I end up having to edit a bunch of unit tests to match up with the new, re-factored code.

      I know the party line on this, but I think it's just the current fad.

    7. Re:Good idea, but a time-sucker by Cranx · · Score: 1

      No, my unit tests were pretty good. I even wrote a testing framework after studying one framework and improving on it. You're talking to a person who took it PRETTY DARN SERIOUSLY and set time aside to develop an entire project dedicated to unit testing.

      Despite the solid try, however, they never, or rarely, found any bugs.

      Pair programming, refactoring, code review, etc...all excellent ways to increase the quality of your code: provided you have the time and budget. I'm sure with enough time spent on the task, I could write unit tests that can spear bugs with one parameter tied behind its back. The problem is, I could never make the time spent on it come back to me in time saved later on locating bugs. If the time had come back to me later on down the line, I'd take a different stand, but as it is, the time spent is just wasted.

    8. Re:Good idea, but a time-sucker by Mr.+Shiny+And+New · · Score: 4, Insightful

      Where I work, unit tests are a requirement, not a luxury. WIthout fail, the projects with the most unit tests have the fewest bugs, and the projects with the fewest unit tests have the most bugs. I'm not exagerating.

      My question to you is, if you have no unit tests, how can you be sure that the code works at all? Manual testing? Do you cover all the code paths with manual testing? It must take forever, or your application is rather trivial.

    9. Re:Good idea, but a time-sucker by Cranx · · Score: 1, Insightful

      Well, let's just say that the OVERWHELMING MAJORITY of software you use right now was developed WITHOUT the benefit of unit testing.

      Have you never developed anything of significance without unit testing? I ask because it seems like you can't understand how it could be done. It not only CAN be done, it IS done, and some very solid, worthwhile code has been developed with absolutely no unit testing framework whatsoever.

      But in answer to your question, (please understand that whatever I say cannot be construed as a template for ALL cases) in general, my development goes along these lines:

      A rough blocking of the modules involved takes place. Basic interfaces between modules are planned tentatively. Development usually starts from the whole to the part, but sometimes modules are developed separately and integrated in later. At various points, re-factoring and code clean up. Interfaces are refined as things progress. Code is tested at intervals during development, and the tests are usually thrown away, but sometimes saved (depends on how stable the interface design is). At later stages, betas are delivered to the client and/or testers and the software is run through tests described on paper. After a certain period of time, when the software is deemed stable enough, it is considered "shipped" and checks are cut.

    10. Re:Good idea, but a time-sucker by sashang · · Score: 1

      That's been my experience with Unit Testing as well. The problem is it slows development down in general because there is more code to write. Also how do you write a unit test properly? The Java example given in the article has tests some tests but they only test specific instances of a game. Not good enough. What about TDD? In this case it's possible to write a function that will trivially satisfy your test. The only benefit of unit testing I've noticed is that I can go back and rework a function, maybe to optimize it, run the unit tests and have a fair amount of confidence that the algorithm still performs its original function.

    11. Re:Good idea, but a time-sucker by Mr.+Shiny+And+New · · Score: 2, Interesting

      I have developed code of significance without unit testing, and I can assure you that the quality of that code was far inferior to the quality of the code developed with more attention paid to unit tests. In the projects I've worked on, sometimes the manager has made the decision to skip unit tests in order to concentrate on features. It's true that those features can be worked on at a faster rate, and seemingly completed sooner. But inevitably, the number of bugs found in QA, and the amount of time spent revising the code eats up any savings. Even if we break even, in terms of time (which, in my experience, would be a generous assumption), then the non-unit-tested project finishes at the same time but has no suite of tests to carry forward to the next version.

      Furthermore, the benefits of testing are not just related to finding bugs sooner in the initial version. The tests can help influence the design of the code. I've written code where I ended up refactoring it completely while writing the tests, because the initial version, while it worked, didn't wasn't easily testable or had too many redundancies in the code. The quality of the run-time product was the same, but the source code improved dramatically just because I wrote tests for it.

      Also, having the unit tests is a good way to ensure that bugs aren't introduced later. In short, the benefits are so high that, while I can imagine (and have actually done it) working on a project with no tests, it's not a task that I would welcome. It's similar to asking me to work on a task with no debugger, or no source-code control system. Yes, it's possible, and heck, the Linux kernel was written without either of these things, but realistically these are vital tools for software development. To me, unit tests are another of those vital tools.

    12. Re:Good idea, but a time-sucker by rlowe69 · · Score: 3, Informative

      #1: My biggest problems always revolve around the user interface.

      But what would you rather debug: 1) A problem in the user interface you know has to be in the user interface because you have confidence that it's not below it or 2) the whole thing all the way down all of the method calls? If you have unit tests in the rest of the code, debugging the UI is much easier.

      #2: I spent many hours developing tests for things that ended up rarely/never choking my tests....

      Making tests for situations that work is still valuable. If you make such a test and it fails later on, you'll know you had a regression in the code somewhere. This is very handy to have when refactoring as well.

      #3: Whenever I re-factor, unless I'm Mr. Perfect and all my interfaces are perfect from the get-go...

      Interfaces can be refactored too, and of course you'll break tests and have to rewrite things because now you're expecting a new set of behaviour.

      I've found that this has the side benefit of forcing people to really think about changes they make to their code and not just hack away at it. If people know they have to write tests too they seem to put more thought into it ahead of time.

      Anyway, that's just my personal experience with unit testing.

      --
      ----- rL
    13. Re:Good idea, but a time-sucker by Cranx · · Score: 2, Interesting

      #1: No, the bugs aren't in the UI itself, the bugs originate through the UI. Users can do things in such a way that you simply can't predict.

      #2: But you know, if you have time, there are LOTS of things you can do that might help, or might not. I stopped writing unit tests thinking that they one day *might* catch a bug. If I don't have an immediate or at least imminent benefit from it, I don't do it. It's simply a waste of time.

      #3: I don't hack away at code, so I don't think unit tests will save me from something I don't do. My interfaces are developed exactly at the level of quality called for; no more and no less. So in the sense that unit tests can help me "think"...I never found myself lacking in that area. Perhaps it's something that helps less experienced programmers ramp up...I don't know. I've been programming for 20+ years and I'm alright in that regard.

    14. Re:Good idea, but a time-sucker by Cranx · · Score: 2, Insightful

      I wholeheartedly disbelieve that the time you saved skipping unit tests was eaten up at QA. I've done it both ways, and it's much, much faster developing in the requirements. Unit tests are good quality assurance, but they are NOT a time-saver.

      You also touched on another problem I have with unit testing: it influences the design pattern too much. I don't believe that the design which best fits with the unit testing framework is the best design pattern for the project. If your only goal is to make lots of happy unit tests, yeah, then alter your design to suit the tests...but that's WAY too restrictive for me. There are lots and lots of design patterns that are tough to write unit tests for, and the last thing I need is the wrong wheel squeaking for grease. I want my patterns to focus on the needs of the project, not the needs of a unit testing framework.

    15. Re:Good idea, but a time-sucker by Anonymous Coward · · Score: 0

      My biggest problems always revolve around the user interface. Users encounter FAR more bugs than my internals do, and I never found a good way to unit test GUIs.

      Push more of your code out of the UI. So that your UI just calls methods on your objects. Then you can unit-test the objects. When you find a bug in the UI, simulate it with method calls to the objects.

      Do you have an example of exactly what type of bugs are in the UI?

      I have pushed unit testing all the way up and down .. from SQL all the way up to the controller objects .. every time I find a bug, it's in the part that I didn't write a unit test for .. so these days I have bugs only in piddly things like screen redraws or HTML UI's (and those can be tested to if you're really patient).

      For example, if your UI has two buttons (maybe far apart in the workflow), and you should always push button 1 before button 2, but somehow the user managed to click around or open another window that ends up clicking button 2 before button 1, that might tickle a bug. After you get the bug report (or before, if you're thoughtful!) write a test that simulates these button presses, and verify that your controller calls the "error dialog" method. How do you do that? Use mock objects that stand in place of your real objects. I do that all the time and it's really powerful. Basically your mock object will record what methods where called on it, and you can get that log and compare it with expected results, etc.

      I spent many hours developing tests for things that ended up rarely/never choking my tests, meaning they rarely/never caught any bugs. Yet, I still had bugs to work out. I found that it just wasn't worth the time.

      Wow, that's on odd way to look at it.. having tests on one part of your code means you don't have to look for bugs there. And your tests should "choke" when you first write them (because you haven't written the code that passes them yet).

      Whenever I re-factor, unless I'm Mr. Perfect and all my interfaces are perfect from the get-go, I end up having to edit a bunch of unit tests to match up with the new, re-factored code.

      Exactly, first you change your tests to use the new interface. Run the tests. They fail of course. Now change your code until the tests pass. You're done. This is aa very powerful feeling and encourages you not to "over program".

      And don't forget about refactoring that doesn't change interfaces. I have production code that I have refactored "on a whim".. I.e., sunday afternoon, flipping through the CVS repository, I see a little inefficiency. Normally I wouldn't TOUCH the damn thing. But wiht my unit tests I can just concentrate on it, rewrite it a bit, as long as the tests pass I'm A-OK. It really encourages and supports "continuous refinement".

      I know the party line on this, but I think it's just the current fad.

      Nah, it's an OLD thing. LISPers did it, probably even punch-card folks did it. This is just a "rediscovery" and "repackaging".

      Like you I've been programming nearly 20 years.. Test-driven development has made programming MUCH less frantic and much more rewarding, I look back on my old code and think about how many projects could've been saved if I had simply followed the advice of TDD.

    16. Re:Good idea, but a time-sucker by Anonymous Coward · · Score: 0

      I want my patterns to focus on the needs of the project, not the needs of a unit testing framework.

      But those needs are the *same*! At least, they have been on every project I've ever worked on. If you're writing something that takes three pieces of data and returns a fourth, then write a test that expects that. Then write the code to pass the test. Then use it in your program. Whatever the class does, it should be able to test it in the framework. You've taken a "piece" of your app out of the app, and stuck it into the testing framework, in isolation, so you can concentrate on it. It should be used in the tests exactly like you'll use it in your program. Let the tests guide the programming, and write your tests like the code will be used in the program!

      It sounds like what you need to do is find somebody who uses TDD correctly and regularly, and sit down with him and pair program something. Anything. If you don't get completely hooked, then well, you're a better programmer than the rest of us. That's completely possible, and more power to you.

    17. Re:Good idea, but a time-sucker by rossifer · · Score: 1

      Despite the solid try, however, they never, or rarely, found any bugs.

      On our project (~200kLOC), the unit tests have helped find lots of stupid things during initial development, but have really made their money during the huge refactorings needed to follow program management's wildly changing product vision.

      We knew that the previous PM's were worthless, so we built a fairly broad solution against the requirements they gave us. The company eventually agreed with us, and hired a new marketing team after 9 months of development. The new team developed a fairly coherent vision of what the system should look like. About 80% of our "broad solution" code was usable, but that meant that 20% had to adapt to the new vision. One part of that adaptation was in the core model classes, and that change ended up touching about 60% of the files in the system (the 20% is a SWAG of the lines of code that ended up changing).

      Took me four days to do the full refactoring of that change, but once the unit tests ran again, I declared victory and have never needed to look back. That same story has been retold on our project a number of times. The unit tests make earlier features harder to add, but later features much, much easier.

      Oh, and automated UI testing does suck. We're still working out how to do something effective there. But even just on the backend stuff, they make a big difference.

      Regards,
      Ross

    18. Re:Good idea, but a time-sucker by Nevyn · · Score: 1
      #1: No, the bugs aren't in the UI itself, the bugs originate through the UI. Users can do things in such a way that you simply can't predict.

      Then the unit tests could/should have been better. Sorry, it really is that simple.

      #2: But you know, if you have time, there are LOTS of things you can do that might help, or might not. I stopped writing unit tests thinking that they one day *might* catch a bug.

      True, and if you can get a large amount of free debugging (like a prominent OSS project) then any personal testing (like writing unit tests) might not be worth it for you, because getting X hundred people to just use it is going to exercise the normal paths of code pretty well. Of course that isn't going to find subtle bugs or secutiry flaws (and unit testing isn't guaranteed to either, but IMO it's much more likely).

      I don't hack away at code, so I don't think unit tests will save me from something I don't do. My interfaces are developed exactly at the level of quality called for; no more and no less. So in the sense that unit tests can help me "think"...I never found myself lacking in that area. Perhaps it's something that helps less experienced programmers ramp up...I don't know. I've been programming for 20+ years and I'm alright in that regard.

      Of course you don't intentionally write bad code, and neither do I (and I'll admit I assumed I made much fewer mistakes before I tested it all). But I presume you wouldn't compile C without any warnings turned on, or not use prototypes in header files. Why? Because the computer is much better at telling you that... Duh! fprintf("%s\n", foo); isn't what my brain wanted my hands to type. This is how I think of unit tests, they aren't there to catch design mistakes because I'm good at that ... they are there to "prove" the implementation of the design "works".

      I'm also pretty sick of seeing/using other people's code and it having obvious bugs in it ... "testing is not having to say you're sorry". For instance, when testing Vstr I found 4 or 5 bugs in glibc with some of the more esoteric uses of the *printf() functions when using the double formatters. When I found them, half of me was happy ... the other half depressed.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    19. Re:Good idea, but a time-sucker by witlessbird · · Score: 1
      As others pointed out, your tests weren't good enough.

      From my personal experience (I use JUnit and PyUnit on a daily basis):

      Unit tests save a *lot* of time compared to manual testing (that is not to say that they are a complete replacement)

      Unit tests are a must for daily automatic build process

      Unit tests are indespensible for code refactoring, as there is no way to know if you broke anything in the process without them

      Unit tests help to design API (that is if you start with tests)

      --

      ... if you put into the machine wrong figures, will the right answers come out?
    20. Re:Good idea, but a time-sucker by Anonymous Coward · · Score: 0

      QA? We don't have the luxury of having any QA.

    21. Re:Good idea, but a time-sucker by Godeke · · Score: 1

      I'm curious about the "Then the unit tests could/should have been better. Sorry, it really is that simple."

      I have been working on a very large web based product. I use a test product that exercises the UI, but I find it difficult to test two components:

      1) ASP pages (or for that matter PHP or any other dynamic scripting code) that generate HTML. The test product can either check for exact matches (which means updates to XSTs break things) or it can look for things with names and IDs (which most elements don't have).

      2) Data driven processes. The entire system is backed by a SQL database, which is constantly updated to reflect our production environment. One portion of our product is *extremely data driven* (it writes code based on data in the database for our customers.) Testing SQL components is a bear because either a) you fake the incoming recordset, which means it fails to reflect schema and stored procedure changes or b) you work with the data, which may get modified and break the test.

      Sure, I can write unit tests for functions that take data and spit out data, but for functions that take sql and split out html, I just can't seem to find a good solution. I have tried to maintain a "testing" sql database with carefully selected data, but it quickly becomes out of synch with the real development databases.

      To get concrete about this, I can write a function that adds a user to the database by calling a function with predetermined values. Except, once I call it, I can't use it again because the system detects the existance of that user account and acts different. We don't delete accounts (by policy requirements), but just disable them, so I can't even just yank the account.

      Even worse, the actual process of creating a user spans five web pages, collecting data, testing it against conditions, asking for only needed data, etc. So I write web client test code that is as fragile as glass because HTML web client testers can't cope with even minute changes to the appearance of a page.

      If you know tools/methods/whatever that address this kind of programming, I'm eager to check them out!

      --
      Sig under construction since 1998.
    22. Re:Good idea, but a time-sucker by Cranx · · Score: 1

      Unit testing doesn't help me; it just doesn't. It sucked time away from other more important things, and it always will for me.

      My feeling is, when you are a neophyte, unit testing helps you get your code up to speed while you are learning about the pitfalls of programming. However, once you pass a certain point, unit testing is simply a wasteful phase.

      The bad code you see comes from neophytes. Unit testing won't help them much because by the time they become disciplined enough to actually use them regularly enough to be helpful, they'll probably be at, or close, to being disciplined enough to not load code with bugs.

      If you are dealing with programmers who continually pack their code with bugs, and it really eats up so much time that unit testing is saving you time, you might want to change out your coders; they don't sound very proficient to me. Code isn't supposed to be pressed through an extruder such as unit testing; it's an art, and it should be done by talented, intelligent people with enough experience to avoid obvious problems. Unit testing will only catch the most obvious of problems, and only in certain parts of your code, all the while slowing down development. If your team is operating at that level, I really feel your pain. I hate having to deal with programmers whose code is a constant source of problems.

    23. Re:Good idea, but a time-sucker by Cranx · · Score: 1

      Code robots to test interfaces in large systems is as ancient as programming itself; it's probably the grandfather of unit testing, but not unit testing itself. Also, good design makes features easy to add later on, not unit testing. Ascribing "new features were easy to add later on" to unit testing is disingenuous; that's something every project I've ever worked on strove for, and save for perhaps my first few years as a programmer, achieved, and generally early on in the project, not later.

    24. Re:Good idea, but a time-sucker by Nevyn · · Score: 2, Informative
      Sure, I can write unit tests for functions that take data and spit out data, but for functions that take sql and split out html, I just can't seem to find a good solution. I have tried to maintain a "testing" sql database with carefully selected data, but it quickly becomes out of synch with the real development databases.

      First, IMO, html is the UI ... and the best way to test that isn't with a unit test (that I've seen). You want all your UI functions to just glue sql/data commands to the UI. Then you can "easily" test the sql/data commands with a test database and a bunch of function calls ... then you'll know that any bugs you find in the html part must be the fault of the UI code (which should be fairly small).

      As far as the testing DB, I'd recommend having testing data outside the DB and use something to init. the database just before you run your unit tests. This should be easier to keep upto date, and solves problems of testing alterations to the DB.

      To get concrete about this, I can write a function that adds a user to the database by calling a function with predetermined values. Except, once I call it, I can't use it again because the system detects the existance of that user account and acts different. We don't delete accounts (by policy requirements), but just disable them, so I can't even just yank the account.

      Having an external representation of the DB that can be reloaded should solve this problem.

      If you know tools/methods/whatever that address this kind of programming, I'm eager to check them out!

      The only DB thing I've seen is for Java that was on testdriven.com.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    25. Re: Good idea, but a time-sucker by esnyder · · Score: 1

      Ditto for me: I'd love to see some more examples of large LAMP systems under test. This is one case where testing the right things, and the right way, seems non trivial.

      The other time I've had a really hard time figuring out the right way to do my testing was working on an aggressively multithreaded and distributed server application.

      --

      Emile Snyder
      www.talentcodeworks.com

    26. Re:Good idea, but a time-sucker by okock · · Score: 2, Insightful

      Others have already commented on their varying mileage with this topic, I too believe that TDD (done correct) will rather save time than cost more time. At least in the long run. (I'll leave the "done correct" part to somebody else, books and articles have been recommended in other comments).

      There's one misconception that I always see with TDD and Unittesting: Both are not the same.

      In my eyes, TDD is rather a design strategy than a testing strategy. It certainly gives a test suite as a very nice "byproduct", but first and foremost it forces you to think about what your code is supposed to do and write it down. Usually TDD code looks completely different from non-TDD code.

      So you'll have to add some portion of time taken for upfront design to the time you take to write code in order to compare writing testdriven with non-testdriven code.

      This doesn't mean, that the statement commented was wrong: I also believe that you have to manage some more or less steep learning curve in order to be able to really do testdriven development, just as you have to learn a lot about each process, language or framework you work with. I'd certainly be quicker writing a small java tool than writing equivalent five lines of PERL because I've never really (wanted to) manage the PERL learning curve.

  9. Here's a good example by nastyphil · · Score: 2, Funny

    http://www.kernel.org/

    --
    Dialectician. Archology.
  10. TDD vs. unit tests by Nevyn · · Score: 3, Informative

    I'm far from convinced that TDD is actually a good approach. Although it's pretty obvious that without testing the code is often trivially buggy, and unit testing is the cheapest way to perform testing. For instance this kind of thing is all too easy to do with TDD.

    For unit tests you want to write your code, and then look at the best set of unit tests to do complete code coverage. For an OSS e3xample of that you can look at Vstr string library and the code coverage for that project.

    --
    ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    1. Re:TDD vs. unit tests by CompVisGuy · · Score: 1

      If you look at the code coverage report for the parent example, you'll see that a common cause of less than 100% code coverage is multiple return points in functions.

      Multiple return points usually occur because the algorithm computes the return value 'early' (often via some special condition) or because an error condition is encountered and the return mechanism is used to propagate errors up the call hierarchy.

      If you write your function to have only a single return point at the end of the function, you can increase the code coverage, while making the code easier to read. You then have to ensure your function's control structures send control to the end of the function (no gotos, you understand!). If you try to achieve this, your programs become more logically structured, easier to test and maintain.

      On one project where we set ourselves a 100% coverage target, and followed the above coding standard (along with others), we produced some excellent code.

      Of course, it's really hard to exercise some exceptional states, and 100% coverage doesn't mean you have bullet-proof code, but it's another metric to use to assess your code quality.

      --


      "The noble art of losing face will one day save the human race"---Hans Blix
    2. Re:TDD vs. unit tests by Nevyn · · Score: 1
      If you look at the code coverage report for the parent example, you'll see that a common cause of less than 100% code coverage is multiple return points in functions.

      That's a simplification, a significant portion of it now seems to be gcc coverage bugs (Ie. lines marked not covered I can step through in a debugger), but yes a lot of the difficult areas are to do branches.

      If you write your function to have only a single return point at the end of the function, you can increase the code coverage, while making the code easier to read. You then have to ensure your function's control structures send control to the end of the function (no gotos, you understand!). If you try to achieve this, your programs become more logically structured, easier to test and maintain.

      And I'd agrue that this isn't the same thing. Moving from "if (!foo1()) return NULL; if (!foo2()) return NULL; return bar();" to "if (ret && !foo1()) ret = FALSE; if (ret && !foo2()) ret = FALSE; if (ret) return bar(); return NULL;" doesn't buy you anything IMO. But if you can move to "foo1(); foo2(); return bar();" then yes, that's much more likely to be working code (as you've divided the number of possible paths by 3).

      On one project where we set ourselves a 100% coverage target, and followed the above coding standard (along with others), we produced some excellent code.

      Did you hit 100%? If so then I'm impressed, out of curiosity what was the ratio of lines of code to lines of tests?

      Of course, it's really hard to exercise some exceptional states, and 100% coverage doesn't mean you have bullet-proof code, but it's another metric to use to assess your code quality.

      Sure because there's that single (sic) path through the code that goes to all the same places but has a different state when it gets to the end :(. It'd be interesting to see something with 100% code path coverage.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    3. Re:TDD vs. unit tests by CompVisGuy · · Score: 1

      If one was aiming for readability, one obviously wouldn't write the gibberish in your example :-)

      We didn't hit 100%, because there was code that would only be run if hardware-centric exceptional cases were encountered. In such a case, exception code was executed, which we could test via a test harness, but the

      if(exceptional_state) do_exception();
      code did not get covered. I think we managed above 95%, which were we were pretty happy with. I don't recall the ratio of lines of code to lines of tests.
      --


      "The noble art of losing face will one day save the human race"---Hans Blix
  11. some projects are hard by Shaleh · · Score: 1

    My main OSS project is a window manager. How do you unit test that? It is nearly impossible without writing your own test X server and the like. Just not worth it.

    We write or bring in small programs which test how we handle applications or events. But it is still really hard to test the whole thing.

    1. Re:some projects are hard by Anonymous Coward · · Score: 0
      Well, yeah, you have to test in isolation the classes/functions of your software. I guess you could test a lot, and perhaps write fake (Xlib?) or perhaps check the status of some pixels.

      There are some unit test software for databases (dbunit) that save you a lot of drudgery.

      I guess the question is what kind of things were you wanting to verify?

    2. Re:some projects are hard by Nevyn · · Score: 2, Informative
      My main OSS project is a window manager. How do you unit test that? It is nearly impossible without writing your own test X server and the like. Just not worth it.

      Creating mock objects is much simpler than creating a "text X server", although admitedly a wm is slightly harder than normal as you can't take the easy route of running a seperate version on your main display.

      Howewver taking a quick look at blackbox, textPropertyToString() is the only thing in Util.cc that couldn't trivially be unit tested and at least all of i18n.cc and Timer.cc. That's 3% with basically no changes.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    3. Re:some projects are hard by Mr.+Shiny+And+New · · Score: 1

      Isn't there an X server that's designed for testing? Or, perhaps you could write a simple application that interacts with your window manager, and your application can ask the window manager to do something, and then verify the result.

      But you're in the arena of gui testing, which is hard. Everyone admits that it's hard. But for many applications, you can separate the gui from the parts that do the real work, and then test those parts. In some cases this can even give you a huge benefit in terms of program architecture, making it easier to maintain, upgrade and port.

      I think if you're creative you'll find a way to test your application. And if not, you could consider writing a fake X server. It might be worthwhile looking at one that can be easily modified or controlled. Perhaps one of the Java X servers could do the trick, since it's easy to integrate one java program into another.

    4. Re:some projects are hard by Shaleh · · Score: 1

      yeah, Util.cc and to some degree the rest of lib/ is easy. I tend to think more about the wm (which is where we have most of the problems).

      This kinds of pushes me to try testing more of the lib though ......

  12. A more positive veiw by oo_waratah · · Score: 5, Insightful

    I wrote a series of tests for code I have written for what I would term a trivial application. Took me about 3 months to develop. The series of tests for about 50% of the code and missed 50%. The vast majority of the bugs came from the second half of the code.

    Second. The tests themselves act as documentation. I am anoyed that I pick up generic routines like a CSV file reader and there is subroutines but no way to figure out how to use them. A test suite acts as sample code as well.

    This process is balance, you don't test every detail but you do codify some tests. When you find a bug you codify a test and then you NEVER reintroduce the same bug. I find that in general I reintroduce bugs about once for every month of coding. Since this application only gets worked on in random blocks of a week here and there I am not focussed on the whole application and I simple forget. Use the computer to validate that you have not made a mistake.

    These sort of tests saves my reputation, does not frustrate my users.

  13. I keep junit around by ReciprocityProject · · Score: 5, Insightful

    I'm sure a lot of people have grounds to object, but I suggest, depending on the nature of the project, it's a good idea to be intelligently selective about what does and does not need to be unit tested. Consider:

    Do write a test case if:

    + a failure could introduce data propagation issues
    + it performs some intricate mathematical or logical function whose result must be precise
    + you're writing test cases to hunt down bugs that you know are in you're code; keep those test cases
    + you're uncomfortable about the quality of your code
    + an error might kill someone, or otherwise be Really Bad

    Don't bother writing a test case if:

    - you can use a guard clause or assertion instead
    - a failure in the code will otherwise be immediately obvious
    - the code generates massive amounts of data which need not be mathematically precise (i.e. graphical output)
    - you don't feel it

    I should probably write more test cases, according to my own guidelines. ::sigh::

  14. The Bowling Game by rlowe69 · · Score: 3, Interesting

    The first wow, that's cool moment I had with test driven development was from an article on Object Mentor called The Bowling Game. It also highlights pair programming a bit.

    The unique thing about the article was that it was presented as a discussion between two developers pair programming doing agile test driven development of the game. It was like watching over their shoulders.

    If you want to get an idea of what extreme programming is like, I suggest reading this article AND writing the code and tests along with it, either in Java (and JUnit) or C#/VB.NET (and NUnit) or another language with a xUnit unit testing framework. Most object oriented languages have them now so you don't have to roll your own framework.

    --
    ----- rL
  15. Try the Portland Pattern Repository by MountainLogic · · Score: 1
  16. ncurses, termcap, etc. by Anonymous Coward · · Score: 0

    Any ideas? I'd like to write tests for top.

    1. Re:ncurses, termcap, etc. by eukreign · · Score: 1

      Sure! Granted the code is modular and can be tested independently. Just make sure that your input is as thin of a layer as possible. For example all the code that deals with actually calling the system to get processes should be a couple of lines long (ie, bullet proof since you can't really test that). Than you create a mock version of that function that returns some predefined data. In your tests make sure that your formatting functions and any calculation functions use this mock function and do those tests that way. For the actual gui, I believe you can do screen dumps. Although you can't test the dynamic nature of the gui being updated, you can at least capture one slice of the gui displaying so that if you ever refactor the display code it will at least produce an identical looking screen.

      Remember, the best unit test writers are the laziest ones. This is very important because if you write more tests than you need (out of being a perfectionist for example) you'll end up with a lot of code that will need to be changed if the requirements change.

      Refactoring unit tests is very dangerous, but sometimes it's better to refactor your tests so that you have the desire to continue writing them. Than to end up with some crappy tests that you hate so much that you just stop writing new ones or even delete the old ones because requirements changed and you don't feel like updating them.

      --
      Free Talk Live: Talk Radio where YOU ar
    2. Re:ncurses, termcap, etc. by Anonymous Coward · · Score: 0

      Calling the system to get processes is tested
      via the ps program, which uses the same code.
      If by "screen dumps" you mean top's batch mode,
      the ncurses/termcap stuff wouldn't be involved.

      I've had numerous bugs with unusual terminals.
      The end-of-line and end-of-screen behavior can
      be especially troublesome.

  17. SCons by scons · · Score: 3, Informative
    On the SCons project, we've been heavy users of a Test-Drive Development methodology from day one of the project (coming up on three years now).

    SCons is a next-generation build tool, or make tool, written in Python with strong cross-platform support, integrated autoconf-like functionality, a lot of stable features, and a growing user community. We're currently at 14K+ lines of non-comment, non-blank source code, and 32K+ lines of non-comment, non-blank test code.

    We use a combination of two different testing methodologies: 1) Individual modules all have PyUnit unit tests (similar to JUnit, but in Python, of course). 2) The SCons application itself is tested using a custom testing module that manages creation of temporary directories and files, execution of the application, and checking against expected results. This custom module is actually a wrapper around a generic "test any script/command" infrastructure module that could be easily used to test other scripts and/or commands. (The command under test could be implemented in any language, not just in Python.)

    I use the Aegis change management system to manage the SCons development and testing cycle. Aegis' primary value add (for me) is its management of the test cases and the testing methodology it enforces. By default, all Aegis changes must have one or more modified or new tests. The new/modified tests must not only pass when run against the new code, but must (by default) fail when run against the old code. This helps guarantee that your tests are good, and that your code isn't passing because you made a mistake in your test and forgot to call the new feature.

    By testing in this fashion from day one, we've built up a very strong regression test base--284 test scripts at last count, each script containing multiple individual tests. This test base has become crucial to our ability to refactor (and refactor and refactor...) the internals as we add more features. Sometimes it takes longer, of course, to make a rewrite satisfy all of the regression tests, but when you're done, you can be pretty sure you haven't broken anything. And if you did break something, then you have to add or modify a test when you fix it, and that becomes another part of the regression test base.

    The key to getting going with this kind of test-driven development (in my opinion) is making writing and executing the tests as simple as possible (but no simpler!). If writing a test is too difficult, then a lot of developers will simply avoid it. But if you can get them over the initial hump by making it easy to write tests, it gets downright addictive because you get all of this positive feedback when your tests show you that your new code works.

    We'd be glad to have you check out the testing infrastructure we've developed for SCons, either for code you can actually use, or simply as a source of ideas. Feel free to contact the SCons development team if you have any questions.

    1. Re:SCons by esnyder · · Score: 1

      Thanks for the pointer! I hadn't thought about either (S)Cons or Aegis in quite awhile; it's good to be reminded. I will definitely spend some time reviewing the tests that come with SCons, and try using it for some of my projects. Nothing like build scripts in python to make you appreciate how painful make files can be ;)

      It's been interesting how most of the examples people have come up with are Java and Python; seems to be a language culture kind of thing.

      Thanks again.

      --

      Emile Snyder
      www.talentcodeworks.com

    2. Re:SCons by axxackall · · Score: 1
      By the way, Scons, as Python, is cross-platform. But Aegis is not: AFAIK it's only for systems with Unix-like file permissions and all other ports are too unstable. So, here is my quastion:

      Have you ever consider Subversion instead of Aegis for change management in Scons? If so, does it has same level of support for testing? If so, can you recommend a combination of Subversion+Scons for the process automation in mid-size teams?

      --

      Less is more !
  18. Boost by Screaming+Lunatic · · Score: 2, Informative
    The best example that I know of. Every library is tested and thoroughly review. There are regression tests for every significant C++ compiler out there. http://boost.sourceforge.net/regression-logs/

    And check out the Boost unit test libraries. http://boost.org/libs/test/doc/index.html

  19. Looking for an example of Test-Driven Development? by Anonymous Coward · · Score: 3, Informative

    Try the book called "Test Driven Development: By Example" by Kent Beck!

    It's a neat little book. The first third demonstrates writing a Money class in Java using test-driven development. It's kinda like you're sitting next to him and he is demonstrating. In that example he shows a lot of what comes up in an average TDD session. If you're new to it, you might not realize how fluid it is, and how tiny the steps can be. Even if you *think* you know what TDD is about from reading a description, this will really show you how it works.

    Then the next chapter is really cool.. here he writes a unit test framework in Python .. using the framework he is writing to write the framework! Here he shows you how to bootstrap a new testing framework, which you probably won't ever need to do, but it was cool. He starts with a single test that prints "1" or "0" on the screen for pass/fail, and builds on that.

    Final third of the book is patterns, example, theory, philosphy, somewhat interesting.

    This is a book you'll only read once or twice, honestly, but you will benefit greatly from it. In my opinion.

  20. Re:Looking for an example of Test-Driven Developme by esnyder · · Score: 1

    Thanks for the input; I'll give it a look. But what I'm really after is larger scale real world systems. I've read quite a few books that walk you through the process, and I've written my share of tests on various projects, but there's nothing like full scale examples to point out the pitfalls.

    --

    Emile Snyder
    www.talentcodeworks.com

  21. Tcl and Tk by DavidNWelton · · Score: 1

    Add Tcl and Tk to the list:-) They have a big test suite which pokes and prods at as many nooks and crannies of the language as possible.

  22. The Craftsman by Anml4ixoye · · Score: 2, Informative

    Software Development Magazine has been running a series for about the past year called The Craftsman written by Robert C. Martin. It focuses on a young apprentice writing a java app with his mentors. Nothing is done that doesn't involve a test first.

    While the series itself is kind of slow, it is a good introduction to TDD, and I really enjoy Robert's writing style. Might be able to lead you to some more examples.

  23. Re:Good idea, but a time-sucker - maybe.. by manavendra · · Score: 1

    I have been a proponent of unit testing, facing a lot of flak from the team members, project managers, etc for the added time in the development cycle. Over time, following are the observations that I've made about unit testing:

    #1. Don't test the obvious - enthusiastic new developers (or naive old developers) tend to write miniscule tests (like testing the accessor methods of a class), which are equivalent to testing the compiler. Avoid them at all costs!

    #2. Test for the obvious - if you wrote a method that expects a number and uses it to divide something else, don't write tests to catch divide by zero, not a number exception, etc. Test the boundary conditions of your unit and what it is expected to do!

    #3. Remember you are testing the program unit - dont let loose your imagination and spend time thinking of new ways to break your code. Leave that for the testing stage.

    #4. And finally update the tests along with the code - its tedious, we all hate it, but someone else wouldnt have to dig through your code to find out what was the change you did that broke the framework!

    --
    http://efil.blogspot.com/
  24. Most OSS projects don't use XP by mpcooke3 · · Score: 1
    OSS are unlikely to use XP or other agile development systems.

    Therefore most test first projects are probably closed source. I can't show you our whole code base but we are a TestFirst XP place with 1654 unit tests currently in place.

    For large scale testing mock objects are invaluable. See www.mockobjects.com and www.mockmaker.org

    Here is an example of what a test looks like using Junit and MockObject testing. It might give you a feel for how easy the tests are to write.
    public void testBlowWholeCache() throws Exception {
    myMockRequest.setupParameter(ResinCacheManager.COM MAND_PARAM, ResinCacheManager.CLEAR_ALL);
    myMockResinApplicationAdmin.setExpectedClearCacheC alls(1);

    myServlet.processRequest(myMockRequest, myMockResponse);

    myMockResinApplicationAdmin.verify();
    assertEquals("should get message", ResinCacheManager.CLEAR_ALL_MESSAGE, myMockServletOutputStream.getContents());
    }
    It's a simple test - most tests have more mockobjects. Once you've written that test and it fails you modify the myServlet.processRequest(..) method to pass the test. Then you run AllTests get a green bar, sync with the repository system and go have a cup of tea and some marmite toast.

    Matt.
    1. Re:Most OSS projects don't use XP by mpcooke3 · · Score: 1
      Here is the setup method for that test suite. Just in case you wonder how all the objects are hooked up.
      public void setUp() {
      myMockResinApplicationAdmin = new MockResinApplicationAdmin();
      myMockResponse = new MockHttpServletResponse();
      myMockRequest = new MockHttpServletRequest();
      myMockServletOutputStream = new MockServletOutputStream();
      myMockResponse.setupOutputStream(myMockServletOutp utStream);
      myServlet = new ResinCacheManager(myMockResinApplicationAdmin);
      }
  25. gcc by IainHere · · Score: 2, Informative

    It's not *exactly* test driven, but gcc has a very extensive test suite, and adds regression tests for every new bug that is uncovered. The project certainly passes your 50kloc specification!

    http://gcc.gnu.org/install/test.html

  26. Re:Looking for an example of Test-Driven Developme by DukeyToo · · Score: 2, Informative

    I bought this book, and it was really useful in the sense of basic training, but it does not tackle any of the difficult questions, such as mock objects, user interfaces, etc.

    I am left still wanting a nice non-trivial working example to look at.

    --
    Most writers regard truth as their most valuable possession, and therefore are most economical in its use - Mark Twain
  27. TDD with web development by ajw1976 · · Score: 1

    I am very curious about TDD and would like to get in deeper, but all of the tutorials that I have seen are writing OO type applications. How would you make use of TDD if you were writing web applications using ASP/VBScript or PHP? Is it possible.

    Thanks in advance for your replies.

    --
    1. Bad signature
    2. ?????
    3. Profit
    1. Re:TDD with web development by eukreign · · Score: 1

      Try google!

      Search for "php unit testing" ;-)

      If you're too lazy, here are some links:

      Unit Testing in PHP
      PHPUnit

      Happy unit testing!

      --
      Free Talk Live: Talk Radio where YOU ar
  28. Take a look at Eclipse by vtavares · · Score: 1

    Besides being an excellent development environment with support for XP (unit testing, refactoring, etc), it's developed with Unit testing galore. See http://www.eclipse.org and look at the downloads section. There are source downloads as well as complete tests. Excellent examples of Java development with patterns, testing, and refactoring.

  29. My limited experience with unit testing by Anonymous Coward · · Score: 0

    I know this thread is a bit old, but I'll chime in anyway...

    I generally do not use unit testing. However, I can think of a few occasions where I have used unit testing. I remember thinking, "I've written all this code, but how do I know it works?". Those situations demanded some test code.

    The code to be tested was low-level stuff, but non-trivial. Several hundred to several thousand lines of code that was very tightly inter-dependent. One of the smaller ones was a collection class, one of the larger ones was a persistence layer. Individually, each of the bits of code looked fine on review, but whether or not the bits would all work together in concert was too complex to verify by looking. So rather than immediately writing other code that used this low-ish level stuff as a base and tracking down the bugs later, test code was written.

    In these cases, it was obvious that unit testing was required. The test code itself was in all cases not more than a few dozen to a couple hundred lines, or somewhere around 2-10 % the size of the code to be tested. Not a lot of code, and well worth it in those instances.

    For simpler code, I think the tests would be quite a lot of code relative to the amount of code to be tested, and so the (whatever the coder equivalent of ROI is) would be less, though maybe still worth it in some cases. For higher-level code, the behavior would be so complex that writing meaningful tests would require a hell of a lot of work, and again the (coder ROI) would have been less, though again maybe still worth it in some cases.

    It seems to me that there is a "sweet spot" where unit testing is a must, and other cases where it is worth it but not really important, and still other cases where the code is so trivial or so complex that unit tests are more effort than they're worth.

  30. Junit has its own unit tests by PinglePongle · · Score: 1

    And the TDD book by Kent Beck shows how the evolution of an application can be driven by unit tests.
    Fwiw - I'd suggest that a large project is _not_ the best way to get to understand how unit testing works - there is likely to be way more detail than you need or want. Matt Raible shares some code (esp. AppFuse) which includes unit tests, and describes them in his wiki on Appfuse tutorials.
    His "Struts Resume" demo application contains some pretty interesting unit tests of a real-world application.
    Now, here's the skinny for me : using testing as an integral part of your development cycle helps you to separate the layers and components in your application. Every time you find yourself thinking "how am I going to test this - it's too difficult/intertwingled/dependent on other stuff", you have identified a design deficiency. Making the software easy to test will make it easy to understand, and easy to work with in future. By architecting the application to make it easy to test independently of the database, you make it resilient against changes in the underlying database technology, and - oh, yeah, you have some self-documenting code to show others how to call your api.
    Good place to start reading is Robert Martin's "Agile Software Development".

    --
    It's all very well in practice, but it will never work in theory.