views:

76

answers:

2

I'm in charge of at least one large body of existing PHP code, that desperately needs tests, and as well I need some method of checking the production site for errors.

I've been working with PHP for many years, but am unfortunately new to testing. (Sorry!).

While writing tests for code that has predictable outcomes seems easy enough, I'm having trouble wrapping my head around just how I can test the live site, to ensure proper output.

I know that in a test environment, I could set up the database in a known state... but are there proper methods or techniques for testing a live site? Where should I begin?

[I am aware of PHPUnit and SimpleTest, but haven't chosen one over the other yet]

+1  A: 

Unit testing frameworks like PHPUnit are built more for testing the functionality of separate, logical units (i.e. classes), not so much the behaviour of entire live sites. One household name for that is Selenium, a web application testing system. It is my understanding that Selenium tests can in turn be integrated with PHPUnit.

As for the choice between Simpletest and PHPUnit, check out my recent question about PHPUnit vs. SimpleTest - the answers to that question, notably @Gordon's, managed to convince me of PHPUnit.

Pekka
@Gordon naah, I was just lucky with the timing :)
Pekka
Is it commonly acceptable that tests are run alongside production code? I suppose that running tests against the active code-base would at least prove that things are in working order... is that how it's normally done?
anonymous coward
@anon I'm new to the field myself but no, to my understanding the general good practice is to test code in isolated testing conditions (even with mockups to simulate file accesses instead of working with live files), and then update the live environment with the tested (and working) code. What you are asking for may go into the direction of this question: http://stackoverflow.com/questions/2405825/do-you-have-health-checks-in-your-web-app-or-web-site which is *not* Unit testing in the pure sense of the word.
Pekka
@pekka you might want to check out XRefresh an add-on for firefox. You mentioned a desire for a web interface for running PHPUnit. Did you find one or make one for yourself. I would be interested.
Gutzofter
Accepting this answer as *the* answer, with the suggestion to future "googlers" to see the other, very informative answer: http://stackoverflow.com/questions/2700911/php-how-to-begin-testing-large-existing-codebase-and-test-for-regression-on-pr/2700956#2700956
anonymous coward
This is one of the cases where it would be really nice to be able to "co-author" one post.
Pekka
+2  A: 

I second Pekka's suggestion. Also, I strongly suggest using PHPUnit, as it is the de-facto Standard in UnitTesting frameworks in the PHP World.

Another thing you can do is head over to phpqatools.org and use the given tools to analyze your codebase, find dead code, copy and paste, code violations, etc.

Also profile your code with XDebug or Zend Debugger to find out what it is actually running how often. This way you will not only get an idea of which code you should test first (those that runs most often), but also how it performs, which is a good starting point when you wil optimize it after you have written the Unit-Tests.

In addition, check out:

Gordon