Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Unit testing question

Back to Forum: Tech Off
  • User profile image
    spivonious

    I posted this on the preview site as well, but figured I'd get a larger audience over here.

     

    I feel that I have a good understanding of unit testing. I would love to implement it in the legacy apps I maintain, but most of them were written with no semblance of good design. For example, I'm making changes to an app now that has over 10,000 lines of code in its user interface class. It holds interface components, data components, business logic, data access logic, security logic, etc.

    Does anyone have any tips on how to ease this class into a testing framework without disturbing too much?

  • User profile image
    Dr Herbie

    First thing you do is buy and read Working Effectively with Legacy Code in which 'Legacy Code' is code with no unit tests.  Talk about obscuring the title ...

     

    Anyway -- the main issue with retro-fitting unit tests is that you have to change the code to add the tests, but you don't have existing tests to ensure that you didn't break it.

    The 'Working with ...' book contains a load of recipes for handling the kinds of situations you are likely to encounter.

     

    It's much harder work than writing with unit tests from the start ...

     

    Herbie

     

  • User profile image
    spivonious

    Dr Herbie said:

    First thing you do is buy and read Working Effectively with Legacy Code in which 'Legacy Code' is code with no unit tests.  Talk about obscuring the title ...

     

    Anyway -- the main issue with retro-fitting unit tests is that you have to change the code to add the tests, but you don't have existing tests to ensure that you didn't break it.

    The 'Working with ...' book contains a load of recipes for handling the kinds of situations you are likely to encounter.

     

    It's much harder work than writing with unit tests from the start ...

     

    Herbie

     

    I have and have read that book. It's very good, but his solutions always assume that the application has some sense of design at one point. The apps I have to maintain have zero structure. Need something from the database? Put inline SQL in the event handler. Need to modify a property of an item? Change it in the grid and manually call the "After Edit" event to update the recordset. It's pretty bad. I also don't think I can get unit tests in there before some major refactoring.

  • User profile image
    Dr Herbie

    spivonious said:
    Dr Herbie said:
    *snip*

    I have and have read that book. It's very good, but his solutions always assume that the application has some sense of design at one point. The apps I have to maintain have zero structure. Need something from the database? Put inline SQL in the event handler. Need to modify a property of an item? Change it in the grid and manually call the "After Edit" event to update the recordset. It's pretty bad. I also don't think I can get unit tests in there before some major refactoring.

    Then it comes down to good old-fashioned hard work Smiley.

     

    If you find points in the code that perform distinct, atomic functions then you could set up unit tests that are really integration tests; that would help you ensure you haven't broken anything at a high level (e.g. when user clicks 'Calculate VAT' that the VAT calculation uses the VAT rates in stored in the database without worrying about testing each discrete step).

     

    Herbie

     

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.