views:

283

answers:

10

So, I've got this big project written in PHP and Javascript. The problem is that it's become so big and unmaintainable that changing some little portion of the code will upset and probably break a whole lot of other portions.

And I know for a fact that I'm really bad at testing my own code (as a matter of fact, others point this out daily). Which makes it even more difficult to maintain the project.

The project itself isn't that complicated or complex. It's more the way it's built now that makes it complex. We don't have any predefined rules or lists to follow when doing our testing. And this often results in lots of bugs and unhappy customers.

So, we started discussing this at the office, and came up with the idea of starting to use test driven development instead of the develop like hell and maybe test later (which almost always ends up being fix bugs all the time).

After that background, the things I need help with is the following

  1. How do you implement a test framework into a already existing project (that is 3 years in the making, and counting)?

  2. What kind of frameworks are there for testing? I figure I'll need one framework for the Javascript- and one for the PHP code.

  3. Whats the best approach to testing the gui?

Some notes. I've never used Unit Testing before, so this is really uncharted territory for me.

Thanks in advance Patrik

A: 

Implementing a framework is in most cases a complex task because you kinda start rebuilding your old code with some new solid framework parts. Those old parts must start to communicate with the framework. The old parts must receive some callbacks and returnstates, the old parts must then somehow point that out to the user and in fact you suddenly have 2 systems to test.

If you say that you application itself isn't that complex but it has become due to lack of testing it might be a better option to rebuild the application. Put some common frameworks like Zend to the test, gather your requirements, find out if the tested framework suits for the requirements and decide if it's usefull to start over.

I'm not very sure of unit testing, but NetBeans has a built-in unit testing suite.

Ben Fransen
+3  A: 

From a planning perspective, I think you have three basic choices:

  1. take a cycle to retrofit the code with unit tests
  2. designate part of the team to retrofit the code with unit tests
  3. introduce unit tests gradually as you work on the code

The first approach may well last a lot longer than you anticipate, and your visible productivity will take a hit. If you use it, you will need to get buy-in from all your stakeholders. However, you might use it to kickstart the process.

The problem with the second approach is that you create a distinction between coders and test writers. The coders will not feel any ownership for test maintenance. I think this approach is worth avoiding.

The third approach is the most organic, and it gets you into test-driven development from the get go. It may take some time for a useful body of unit tests to accumulate. The slow pace of test accumulation might actually be an advantage in that it gives you time to get good at writing tests.

All things considered, I think I'd opt for a modest sprint in the spirit of approach 1, followed by a commitment to approach 3.

For the general principles of unit testing I recommend the book xUnit Test Patterns: Refactoring Test Code by Gerard Meszaros.

Ewan Todd
I think this will be the most likely approach to my backend code. I don't know if we're able to dedicate a whole sprint to retrofitting. But I'm sure as we work with each class, we can write test cases for each class as we go.
Skoog
+2  A: 

Hi Patrik.

I've used PHPUnit with good results. PHPUnit, like other JUnit-derived projects, requires that code to be tested be organized into classes. If your project is not object-oriented, then you'll need to start refactoring non-procedural code into functions, and functions into classes.

I've not personally used a JavaScript framework, though I would image that these frameworks would also require that your code be structured into (at least) callable functions if not full-blown objects.

For testing GUI applications, you may benefit from using Selenium, though a checklist written by a programmer with good QA instincts might work just fine. I've found that using MediaWiki or your favorite Wiki engine is a good place to store checklists and related project documentation.

jkndrkn
As it happens 70-80% is class based and the rest is still procedural. Still working hard to get those remaining 20% into classes aswell.
Skoog
That's a good start :]
jkndrkn
+1 PHPUnit. I've heard mixed reports about Selenium because it tends to encourage brittle tests. I am unable, though, to suggest a more xUnit-y JS test framework. If the JS were generated, GWT style, then there's an xUnit for that.
Ewan Todd
A: 

If the code is really messy, it's possible that it will be very hard to do any unit testing. Only sufficiently loosely coupled and sufficiently well designed components can be unit tested easily. However, functional testing may be a lot easier to implement in your case. I would recommend taking a look at Selenium. With this framework, you will be able to test your GUI and the backend at the same time. However, most probably, it won't help you catch the bugs as well as you could with unit testing.

Ignas R
I would say about half of the bugs are in the GUI itself, so Selenium will probably be a really good option here. I will check if out. Thanks.
Skoog
+6  A: 

G'day,

Edit: I've just had a quick look through the first chapter of "The Art of Unit Testing" which is also available as a free PDF at the book's website. It'll give you a good overview of what you are trying to do with a unit test.

I'm assuming you're going to use an xUnit type framework. Some initial high-level thoughts are:

  1. Edit: make sure that everyone is is agreement as to what constitutes a good unit test. I'd suggest using the above overview chapter as a good starting point and if needed take it from there. Imagine having people run off enthusiastically to create lots of unit tests while having a different understanding of what a "good" unit test. It'd be terrible for you to out in the future that 25% of your unit tests aren't useful, repeatable, reliable, etc., etc..
  2. add tests to cover small chunks of code at a time. That is, don't create a single, monolithic task to add tests for the existing code base.
  3. modify any existing processes to make sure new tests are added for any new code written. Make it a part of the review process of the code that unit tests must be provided for the new functionality.
  4. extend any existing bugfix processes to make sure that new tests are created to show presence and prove the absence of the bug. N.B. Don't forget to rollback your candidate fix to introduce the bug again to verify that it is only that single patch that has corrected the problem and it is not being fixed by a combination of factors.
  5. Edit: as you start to build up the number of your tests, start running them as nightly regression tests to check nothing has been broken by new functionality.
  6. make a successful run of all existing tests and entry criterion for the review process of a candidate bugfix.
  7. Edit: start keeping a catalogue of test types, i.e. test code fragments, to make the creation of new tests easier. No sense in reinventing the wheel all the time. The unit test(s) written to test opening a file in one part of the code base is/are going to be similar to the unit test(s) written to test code that opens a different file in a different part of the code base. Catalogue these to make them easy to find.
  8. Edit: where you are only modifying a couple of methods for an existing class, create a test suite to hold the complete set of tests for the class. Then only add the individual tests for the methods you are modifying to this test suite. This uses xUnit termonology as I'm now assuming you'll be using an xUnit framework like PHPUnit.
  9. use a standard convention for the naming of your test suites and tests, e.g. testSuite_classA which will then contain individual tests like test__test_function. For example, test_fopen_bad_name and test_fopen_bad_perms, etc. This helps minimise the noise when moving around the code base and looking at other people's tests. It also has then benefit of helping people when they come to name their tests in the first place by freeing up their mind to work on the more interesting stuff like the tests themselves.
  10. Edit: i wouldn't use TDD at this stage. By definition, TDD will need all tests present before the changes are in place so you will have failing tests all over the place as you add new testSuites to cover classes that you are working on. Instead add the new testSuite and then add the individual tests as required so you don't get a lot of noise occurring in your test results for failing tests. And, as Yishai points out, adding the task of learning TDD at this point in time will really slow you down. Put learning TDD as a task to be done when you have some spare time. It's not that difficult.
  11. as a corollary of this you'll need a tool to keep track of the those existing classes where the testSuite exists but where tests have not yet been written to cover the other member functions in the class. This way you can keep track of where your test coverage has holes. I'm talking at a high level here where you can generate a list of classes and specific member functions where no tests currently exist. A standard naming convention for the tests and testSuites will greatly help you here.

I'll add more points as I think of them.

HTH

Rob Wells
Should I go about writing test cases for complete classes at once, or just methods? Remember, I'm new to this unit testing business :)
Skoog
@Skoog, just added a link to an excellent intro to unit testing.
Rob Wells
I thank you on behalf of our whole team. Lots of good stuff you've provided.
Skoog
@Skoog, no probs! Get them to vote me up! (-:
Rob Wells
+6  A: 

You should get yourself a copy Working Effectively with Legacy Code. This will give you good guidance in how to introduce tests into code that is not written to be tested.

TDD is great, but you do need to start with just putting existing code under test to make sure that changes you make don't change existing required behavior while introducing changes.

However, introducing TDD now will slow you down a lot before you get back going, because retrofitting tests, even only in the area you are changing, is going to get complicated before it gets simple.

Yishai
@Yishai, good point about the potential effects of late introduction of TDD. I think it could be added for completely new code though iff people know TDD already. Learning TDD, xUnit frameworks and unit testing basics all at once is **not** going to work well!
Rob Wells
A: 

Maybe this list will help you and your mates to re-structure everything:

  1. Use UML, to design and handle exceptions (http://en.wikipedia.org/wiki/Unified%5FModeling%5FLanguage)
  2. Use BPMS, to design your work-flow so you won't struggle (http://en.wikipedia.org/wiki/Business%5Fprocess%5Fmanagement)
  3. Get a list of php frameworks which also support javascript backends (e.g. Zend with jQuery)
  4. Compare these frameworks and take the one, which matches the most to your project desgin and the coding structure used before
  5. You should may be consider using things like ezComponents and Dtrace for debugging and testing
  6. Do not be afraid of changes ;)
daemonfire300
+5  A: 

Just to add to the other excellent answers, I'd agree that going from 0% to 100% coverage in one go is unrealistic - but that you should definitely add unit tests every time you fix a bug.

You say that there are quite a lot of bugs and unhappy customers - I'd be very positive about incorporating strict TDD into the bugfixing process, which is much easier than implementing it overall. After all, if there really is a bug there that needs to be fixed, then creating a test that reproduces it serves various goals:

  • It's likely to be a minimal test case to demonstrate that there really is an issue
  • With confidence that the (currently failing) test highlights the reported problem, you'll know for sure if your changes have fixed it
  • It will forever stand as a regression test that will prevent this same issue recurring in future.

Introducing tests to an existing project is difficult and likely to be a long process, but doing them at the same time as fixing bugs is such an ideal time to do so (parallel to introducing tests gradually in a "normal" sense) that it would be a shame not to take that chance and make lemonade from your bug reports. :-)

Andrzej Doyle
I've come to terms with it being a long process. So I'm prepared. The thing I was most concerned with, was, Where do I start? How do I proceed? But the things you bring up, I understand. I think this will work really good for us. I just have to read up on the subject of TTD and Unit testing.You would happen to have some good resources for beginners in this field?
Skoog
A: 

For GUI testing you may want to take a look at Selenium (as Ignas R pointed out already) OR you may wanna take a look at this tool as well: STIQ.

Best of luck!

KOHb
A: 

In some cases, doing automatic testing may not be such a good idea, especially when the code base is dirty and PHP mixes it's behavior with Javascript.

It could be better to start with a simple checklist (with links, to make it faster) of the tests that should be done (manually) on the thing before each delivery.

When coding in a 3 years old minefield, better protect yourself with many error checking. The 15 minutes spent writing THE proper error message for each case will not be lost.

Use the bridging method: bridge ugly lengthy function fold() with a call to fnew() which is a wrapper around some clean classes, call both fold and fnew and compare result, log differences, throw the code into production and wait for your fishes. When doing this, always use one cycle for refactoring, anOTHER cycle for changing result (do not even fix bugs in old behavior, just bridge it).

gb