Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With PHPUnit how does one run initialization code before any tests are run?

Tags:

phpunit

Currently we run code to setup the database in setUpBeforeClass. However this runs before every test class is tested. Is it possible to have code run once before any tests are run, and maybe run some code when all tests are complete too?

like image 875
SubOne Avatar asked Nov 10 '11 20:11

SubOne


1 Answers

This is precisely what the bootstrap file is designed to handle. By default PHPUnit will execute the code in bootstrap.php in the current directory. You can use the phpunit.xml configuration file or the --bootstrap command-line switch to point to a different file.

This file is executed exactly once before trying to locate the tests to be run. It allows you to set up an include path, autoloader, constants, etc. before instantiating or running any tests.

like image 88
David Harkness Avatar answered Jan 14 '23 16:01

David Harkness