Phpunit Reset environment between tests

I have a symfony2 application and I am using phpunit.

I have several unit tests in which I use mocks to mock AppKernel and functional tests that make "real" requests to the application. When performing single tests or only in functional tests, everything works fine.

This is unpleasant when I want to run all the tests at once. Once the final tests are complete, phpunit stops telling me:

Fatal error: Cannot redeclare class AppKernel in C:\Users\sebastian\workspace\ppInterface\app\AppKernel.php on line 35

I do not understand this, because I thought that phpunit would run each test in its own environment. This does not seem to be the case. What can I do to ensure that everything is in order and "reset" the environment in which the tests were run?

+4
source share
1 answer

PHPUnit does not reset everything by default, although this is possible.

It includes - the problem in your case - is not reset (and cannot be in one process). The solution would be to use require_once instead of require or use process isolation either in your phpunit.xml file, or in a test case ( @runTestsInSeparateProcesses ), or in a test method ( @runInSeparateProcess ).

You can also influence what is dropped between tests:

  • @backupGlobals
  • @backupStaticAttributes
+8
source

All Articles