Loading ...
Sorry, an error occurred while loading the content.

RE: [XP] Writing Unit test to existing code

Expand Messages
  • Jason Rogers
    ... Yes. Do you have a more specific question?
    Message 1 of 3 , May 1, 2001
    • 0 Attachment
      > I'm working on a project where we are considering using
      > XP. Out project
      > manager one day asked: 'How do you use uni test with our
      > old code?' A
      > good question. Has anyone any experience with writing
      > unit test for
      > existing code.

      Yes. Do you have a more specific question?
    • Morris, Chris
      ... There s been a lot of threads on this in the past, so first thing I d recommend is search the archives and then search the Wiki. There s no easy answer.
      Message 2 of 3 , May 1, 2001
      • 0 Attachment
        > I'm working on a project where we are considering using XP.
        > Out project
        > manager one day asked: 'How do you use uni test with our old code?' A
        > good question. Has anyone any experience with writing unit test for
        > existing code.

        There's been a lot of threads on this in the past, so first thing I'd
        recommend is search the archives and then search the Wiki.

        There's no easy answer. Chances are good the existing code was not designed
        to be unit tested, so it'll be a challenge.

        You may consider starting with acceptance tests that exercise the system as
        a whole. These can be hard to write even on a pure XP project, but worth
        doing. If you have those in place, they'll help cover the code that's not
        unit tested.

        With or without acceptance tests, as you maintain existing code, start
        refactoring out the code being worked on into structures that are eas[y|ier]
        to unit test. It can be a long road ahead, but some tests are better than no
        tests.

        You may face difficulties getting used to unit testing with existing code.
        That's how I got into the game and I had to climb two mountains at once --
        one was unit testing itself, the other was unit testing existing code. Bleh.
        <g> You'll probably have to invest some time writing some good fixture
        (setup/teardown) code. My advice is stick with it and just know that your
        early attempts at unit testing may be less than ideal until you get the hang
        of it. But the view on this side is wonderful.

        Chris
      • Arrizza, John
        ... Yes, a fair amount; last year or so on a few small projects and one larger project. My experience has been one of a catch-22: unit tests are easier to
        Message 3 of 3 , May 1, 2001
        • 0 Attachment
          > -----Original Message-----
          > From: Anders Bendtsen [mailto:anb@...]
          > I'm working on a project where we are considering using XP.
          > Out project manager one day asked: 'How do you use uni test with our
          > old code?' A good question. Has anyone any experience with writing
          > unit test for existing code.

          Yes, a fair amount; last year or so on a few small projects and one larger
          project.

          My experience has been one of a catch-22: unit tests are easier to write on
          good refactored code, good refactored code can be safely derived from legacy
          code only when you have unit tests in place. One strategy I've taken to
          break the cycle is to use a combination of functional tests and "safe"
          refactorings. I can then get the code in a somewhat reasonable state and
          then add UTs. Even then I add UTs judiciously because of the cost of writing
          them.

          Another strategy that has been proposed is:
          - for a bug:
          - the first time through, just fix the bug
          - the second time in that same area:
          - add UTs (make sure a UT exists that shows the bug failing)
          - refactor
          - fix the bug

          - for an enhancement:
          - add UTs only around the code you're about to change
          - refactor
          - add your enhancement (with UTs and refactorings)

          John
        Your message has been successfully submitted and would be delivered to recipients shortly.