How to propagate setUp and tearDown across all tests in phpUnit?
I have the following tests / folder:
tests / ClassOne.test.php ClassTwo.test.php ClassThree.test.php
I need to copy the following methods setUp()
and tearDown()
into each of these files:
public function setUp () { $ this-> dbTestData (); } public function tearDown () { $ this-> dbClear (); } private function dbTestData () { // populate the database with a few entries Entities \ AutocompleteValue :: create (array ('field_name' => 'testing1', 'entry_value' => 'Aisforapple')); Entities \ AutocompleteValue :: create (array ('field_name' => 'testing2', 'entry_value' => 'Bisforball')); Entities \ AutocompleteValue :: create (array ('field_name' => 'testing3', 'entry_value' => 'Cisforcat')); Entities \ AutocompleteValue :: create (array ('field_name' => 'testing4', 'entry_value' => 'Disfordog')); } private function dbClear () { DB :: table ('autocomplete_values') -> delete (); }
I considered writing a separate separate class that contains these methods, require()
this file in each of the test files and propagates from that class instead PHPUnit_Framework_Testcase
. Is there an easier solution for this?
I don't have easy access to phpunit.xml
because my CLI Framework coding tool (Laravel, artisan) handles its creation. Hence, it would be better if there was a solution not related to this file.
source to share
Give and create a base class. This is the standard solution in this situation. This brings other benefits, such as good storage space for supported extensions static
.
I am trying to even come up with an alternative approach, not to mention that it is simpler. All I can suggest is to use a script that scans each test file and inserts setUp
, tearDown
and their dependencies if not found. But (IMHO), this is a much more complicated solution with no significant benefit.
source to share