Slashdot Mirror


Writing Unit Tests for Existing Code?

out-of-order asks: "I recently became a member of a large software organization which has placed me in the role of preparing the Unit Test effort for a component of software. Problem is that everything that I've read about Unit Testing pertains to 'test-driven' design, writing test cases first, etc. What if the opposite situation is true? This organization was writing code before I walked in the door and now I need to test it. What methodology is there for writing test cases for code that already exists?"

9 of 86 comments (clear)

  1. Unit test the bugs you need to fix by aurelianito · · Score: 2, Insightful

    Before you actually fix it.

    1. Re:Unit test the bugs you need to fix by Dan-DAFC · · Score: 2, Insightful

      I also find that once I get going, I end up throwing away or heavily refactoring a lot of legacy code anyway. So if I had written tests, I'd be throwing them out, too.

      If you are heavily refactoring, it's probably worth putting in the effort to write the tests beforehand. Otherwise how can you be confident that your refactoring hasn't broken anything?

      --
      Suck figs.
  2. Start With the Documented Requirements by north.coaster · · Score: 3, Insightful

    I spent several years managing a test team in a Fortune 100 company, and I have seen this situation many times (it's probably the norm, rather than the exception, in industry today).

    Let the documented requirements for the code (or product) be your guide. Use those requirements to develop test cases, then design one or more tests that hit all of the test cases.

    If there are no documented requirements, then you should ask yourself why you are working there. This situation usually leads to many arguments about what the code/product is really suppose to do, and you'll just become frustrated while you waste lots of time. It's not worth it.

  3. Do you have automated testing tools available? by Richard+Steiner · · Score: 3, Insightful

    Such tools can make after-the-fact testing quite a bit easier.

    We used automated regression testing scripts in the mainframe environment I worked in 12 years ago, and that made some aspects of unit testing relatively easy.

    Unisys had a tool (TTS1100) which allowed us to record each online transaction entry and computer response and then play it back later, and that made it possible to perform the exact same tests dozens or hundreds of times if needed. We used to run them after each set of changes was applied to make sure nothing broke. :-)

    One could also record a single occurrence of a lengthy interactive sequence and then add things like variables and looping structures into the recorded script to automate the handling of various test cases using different values.

    Such a tool makes after-the-fact test design a little bit easier because you can sit down and methodically address each and every variation of each and every input field on a given screen.

    Of course, the nature of the software you're using might make that sort of thing more difficult, or perhaps even easier.

    I've never been able to do up-front unit test design -- specifications can change rather quickly when doing in-house software development, and the overall environment is a lot more dynamic than a typical "software house" environment would be where one always has formal detailed product specs to code to. We're often writing code based on an e-mail or on a couple of phone conversations.

    --
    Mainframe/UNIX Bit Twiddler and long time Windows/Linux Hobbyist.
    The Theorem Theorem: If If, Then Then.
  4. Whhhaaaa! by LouCifer · · Score: 4, Insightful

    Gimme a fucking break.

    Every testing job I've ever had we've had ZERO documentation. NADA. ZIP.

    How do we survive? WE TEST. We put down the book (like we had one to begin with) and we test. Surely you have a server somewhere running dev-level code (at least) and you start poking around. Sure, its less than ideal, but you deal with it. And you bitch about how crappy it is and how it goes against all the principals of so-called 'real world' methodologies.

    The thing is, this is how the real world does it.

    Sure, in a perfect world, everyone has their shit in order. But in a perfect world we're not all competing against code monkeys working for 1/10th of what we make and that live in a 3rd world country.

    --
    Religion is for people afraid of going to hell.
    1. Re:Whhhaaaa! by RomulusNR · · Score: 3, Insightful

      Except when you're expected to have a test plan. You can't come up with a test plan without a functional spec. A design doc helps even more.

      You can't possibly ensure that the application does what it's supposed to if no one can communicate to you what that entails. Imagine testing a house by spraying it with water, banging on the windows, and tromping on the lawn. Those all sound like good things, until the future owner tries to open the front door, and can't.

      --
      Terrorists can attack freedom, but only Congress can destroy it.
  5. Make sure anything changed is tested by Chris_Jefferson · · Score: 3, Insightful

    Trying to write a test case for all the code you have will be very difficult, very long and to be honest not buy you a lot.

    A few open source projects have found themselves in the same situation as you, and they seem to work by 3 rules:

    1) If you change any code at all which doesn't have a test, add a test

    2) If you find a bug, make sure you add a test that fails before, and works now

    3) If you are ever wandering around trying to understand some code, then feel free to write some tests :)

    One thing I will say is to try very hard to keep your tests organised. Keeping them in a very similar directory structure to the actual code is helpful. Without this it's very hard to tell what has and hasn't got a test.

    --
    Combination - fun iPhone puzzling
  6. You are so screwed by HalWasRight · · Score: 2, Insightful
    Hah!

    You are so screwed. Writing tests for untested code is a thankless job. You are going to find so many bugs, and everyone is going to get really pissed off about that new hire that is rocking the boat complaining about "quality problems".

    You are in a no win situation. They will tell you your tests are too picky, that no one will use it like that. Unit testing is thankless, you can't argue. Given that there was no test plan, I bet there isn't even a spec! Where there is smoke there is fire.

    I'd start looking for a new job right away.

    --
    "This mission is too important to allow you to jeopardize it." -- HAL
  7. Read "Working Effectively with Legacy Code" by James+Thatcher · · Score: 2, Insightful

    ...By Michael Feathers. The scenario that you may find is "we can't refactor until we have unit tests and we can't have unit tests until we refactor". The book has some strategies for getting around that paradox. You may find, however, that some code is essentially un-testable as written.