Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add tests to existing application?

I'm currently working on an existing PHP project. The current code design is not that good, and bugs are starting to pile up. So what I want is to add a comprehensive test suite to make it more reliable.

There are already a few unit tests which are written in PHP Unit, however, the test coverage is not that good. The project does not really follow TDD or any sort of test-intensive process. As much as possible I want to have everything tested. But the problem is that I am unsure if I could just add tests for the existing code. As I mentioned, the code structure is not that good, so adding in tests might become very troublesome and difficult.

Is there a better method to add unit tests in projects such as this? Or should I just rewrite the project? Maybe module by module?

like image 800
gerky Avatar asked Nov 11 '12 08:11

gerky


People also ask

How do I add unit tests to an existing Xcode project?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.


1 Answers

Most of the problems that occur when trying to add unit tests to an existing application appear because, usually, that app hasn't divided its functionality good enough into classes and methods that can be tested effectively.

That's actually one of the main advantages of not just TDD, but the good practice of writing tests as you develop the application. The tests compel you into writing concise methods with a clear purpose and as few side-effects as possible.

A full re-write would be the ultimate solution, but I doubt you have the time and energy to do that; and besides, such an action might not me completely necessary!

Honestly, in my experience, the best approach is to just start writing tests for each module at once, refactoring where needed. Start with the core elements such as, for instance, user registration and management bits, and then go further.

It might seem like a daunting task, it might make you feel like your work it in vain, but in the long run, those tests will drastically remove the time spent debugging the application!

Further reading:

  • A great article I recommend on AltDevBlogADay (mirrored by bitsquid as the old url 404'ed). It's not solely about writing tests, but it explains a lot of concepts related to coping with bad (or in your case, maybe just hard to test) code.
  • More about PHP unit testing
like image 78
Andrei Bârsan Avatar answered Nov 13 '22 21:11

Andrei Bârsan