Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit Reset environment between tests

I have a symfony2 application and I'm using phpunit.

I have some unit tests where I use mocks to mock the AppKernel and functional tests which make "real" requests to the application. When running the unit tests or the functional tests alone, everything works fine.

It get's nasty when I want to run all tests at once. As soon as the unit tests are finished, phpunit stops, telling me:

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

I don't understand this, as I thought phpunit would run each test within it's own environment. This seems to be not the case. What can I do to get things right and "reset" the environment in which the tests run?

like image 205
Sgoettschkes Avatar asked Sep 01 '11 10:09

Sgoettschkes


1 Answers

PHPUnit does not reset everything by default, although it's possible.

Includes - the problem in your case - are not resetted (and cannot in one single process). A solution would be to use require_once instead of require, or to use process isolation either in your phpunit.xml file or in the test case (@runTestsInSeparateProcesses) or test method (@runInSeparateProcess).

You can also influence the what is resetted between tests:

  • @backupGlobals
  • @backupStaticAttributes
like image 79
cweiske Avatar answered Oct 15 '22 04:10

cweiske