Jump to content

phpunit: testing legacy code


dennis-fedco

Recommended Posts

say I have a function called load() in a huge legacy codebase.  I supply it variables A, B, and C, and it produces variables X, Y, and Z.

Aside from those variables, there class itself where load() is located has a whole set of existing variables, call them V1, V2, ... Vn.  Some of these may be used in computation of X, Y, Z, but aside from they stay unchanged.

When it comes to testing, there are two ways I can go about it:

  1. Test with assert that X, Y, Z do not exist yet, then call load() and verify that X, Y, Z now have values.  This requires more work -- tracking down what is actually being changed and plucking out the actual changes and putting them into tests.
  2. Dump entire variable set into test, i.e. print_r($this) before the test, and print_r($this) after the test.  Compare those huge print-outs before and after.  Benefit:  less work - just lots of copy and paste and still gets the work done.  It also tests for any side-effects that may be introduced somehow with the legacy code base, so this may have extra benefits over the first approach in that this will catch any out-of-band errors as well.  i.e. if variables V do change due to a side-effect, this will be more easily caught.

Is there a school of thought on this?  Which way/method do I use?

Link to comment
Share on other sites

Test with assert that X, Y, Z do not exist yet, then call load() and verify that X, Y, Z now have values.

functions can not dump variables into the global namespace so that makes statement doesn't make any sense to me.

 

Really, without seeing some example of what your talking about, I'm finding your post pretty hard to follow. Unless the code base was written with tests in mind it is highly unlikely that it is going to be very "testable". Your already talking about dependencies on variables from outside. This alone breaks the "unit" in "unit test".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.