I have installed the PHPUnit framework to perform unit testing. I have written the test cases and also used the existing PHPUnit libraries for my testing.
Is it necessary to configure the PHPUnit framework in staging and production environment? If so, the phpunit test framework and the related files took memory in staging and production which is unnecessary.
Is it enough to use the PHPUnit testing framework in local environment?
No. As GordonM mentioned, it's bad for security to do so. Also, you should test stuff before you put it into a production environment so why would you want to. Some debugging tools (not PHPUnit specifically, but some others) also make applications less efficient.
If you keep your unit tests in their own top-level subdirectory (e.g. see How do you manage the unit test files in projects? do you add them in git? ) then you can simply delete the tests
directory after a git checkout. Or if using ftp, then ftp all other directories except that one. Or if using rsync, then --exclude=tests/
.
But, I find I disagree with the other people who've replied so far. It can be very useful, for peace of mind, to run the unit tests on staging and production servers. If tests pass on your development server but fail on either staging or production you have a Big Red Flag. Better to have your unit test tell you one of your dependencies on the live server is a different version, than have your customers discover it for you!
However this needs care. If any of your unit tests are not self-contained they must not be run. The obvious case is if they use a database, and the unit test does not start by creating that database (with a name that can never clash with a production DB) and finish by removing it. Another case is any test that directly, or indirectly, causes disk files to be updated. Especially think about any functions that do logging. The other type of test you should take care with are those that take a long time to finish, or use a lot of CPU or memory. Make sure these are never run when the production server is live and experiencing load.
One idea to make a copy of phpunit.xml.dist
that explicitly lists those tests that are safe and have no side-effects. Then run it with phpunit --configuration production_tests.xml
. Or, inside the tests, using @group
to flag either safe or unsafe test functions, and then something like phpunit --group safe_for_production
or phpunit --exclude-group modifies_db
I guess the ideal scenario is where you have 3 levels: development, staging and production.
Development is where you keep phpunit and debuggers, but this machine can be totally different then the production. Here you can develop on windows for example, even if you are going to deploy on ubuntu
The staging machine can also keep phpunit and debuggers but it has to have an architecture very close if not identical with the server and maybe access to a copy of the real database. It has to have the same flavor of linux, the same version of apache/php/mysql/libraries as the production server. Having access to real data can also make a difference sometimes.
The production machine should not have phpunit or debugger tools. That one should not even be under your control as a developer. Maybe a lead developer and sysadmins are deploying code there and fine tune the apps as required, and are prepared to rollback or for other contingencies.