|
From: Marcus B. <ma...@la...> - 2003-09-23 23:03:25
|
Hi... McKenna, Simon (RGH) wrote: > In your experience (I'm a unit test neophyte) is it advisable to retrofit > existing classes with unit test classes? This is a common problem and a difficult situation. The short answer is no. Testing as you go slows development slightly even when you start out with all the other components tested and you are skilled at test first. Of course you more than make up the time later, but you will take a tremendous hit if you go back and write tests for all the preceding classes. And it's pretty boring. The usual advice is to test only your new stuff plus anything it comes into contact with. This is a double hit, but tolerable. Unfortunately you will be taking another short term hit because you are new to the tools as well. My feeling is to find a pretty isolated bit of code and write that test first. Once you have spent a couple of days feeling your way you will start to reap the benefits. Once you get that positive feedback then you will feel happier about the double hit of slowly adding tests on a need basis. > > It appears preferable to write tests as you are developing classes, but for > a particular open source project I've been working on for a while > (flashPash.sshnug.com), I've come to the conclusion that as it has grown in > complexity and scope, a robust test framework is needed. You will need tests so as to safely make changes later. By way of encouragement the "test infection" will kick in shortly and you will actually start to like writing tests because of the freedom it gives whilst coding. You can try things, watch the screen go red and then back out safely. Now even the big projects are fun. > > So now I have decision to make...I started writing my own test scripts, but > now realise this isn't the best solution, and should use an existing > framework (i.e.. simple_test / pear::phpunit) so do I wait for my PHP 5 > rewrite of flashPash and develop the new classes in tandem with test > classes, or will it be worth my while to create test classes for the > existing classes? As I said I would refactor the old tests on a need basis or you will slow the development of features too much. If it's any consolation I have the same problem with large a codebase as well. About 10000 lines of test code, 3000 tests in about 80 test scripts! My workaround at the moment is to treat the old test scripts as a web page to be tested so that I can integrate them with the cases so far ported to SimpleTest. Something like... class OldTests extends WebTestCase { function OldTests() { $this->WebTestCase(); } function testOldScripts() { $this->get('http://localhost/projects/tests/old_tests.php'); $this->assertNoUnwantedPattern('/fail/i'); } } At least that way all the tests run together and I can refactor gradually. > > I'm unsure, so if you could give any guidance, it would certainly be > appreciated. Really just start with a small section of code. The rewards will come quickly (trust me please). Also you will be amazed just how many bugs will show up in that old code :). > > Thanks for your time and thoughts. > > Regards, > Simon McKenna > Senior Analyst / Programmer > Information Services - extension 1409 > Repatriation General Hospital - 8276 9666 > No problem. Let us know how you get on. yours, Marcus -- Marcus Baker, ma...@la..., no...@ap... |