php - PHPUnit setup before running first test and tear down after running last test -
i trying implement django test utility php application using phpunit. django like, mean separate test db created main database before running first test , it's dropped after running last test. test db needs created once if many test cases run @ time.
for this, took following approach -
i defined custom test suite class can write code creating , dropping db in it's setup , teardown methods , use class run tests follows
$ phpunit mytestsuite mytestsuite defines static method named suite use glob , add tests testsuite follows
public static function suite() { $suite = new mytestsuite(); foreach (glob('./tests/*test.php') $tc) { require_once $tc; $suite->addtestsuite(basename($tc, '.php')); } return $suite; } all test case classes extend subclass of phpunit_framework_testcase , setup , teardown methods of class take care of loading , clearing initial data json fixture files.
now no. of tests increasing, need run selected tests @ time. since loading tests using test suite, --filter option cannot used. makes me feel approach may not have been correct one.
so question is, correct approach before running first test , after running last test irrespective of how phpunit finds them ?
ps: not using phpunit_extensions_database_testcase own implementation of creating, populating , dropping db.
my 2 spontaneous ideas don't use "test suites". 1 @ bottom.
test listener
using phpunits test listeners
public function starttestsuite(phpunit_framework_testsuite $suite) { if($suite->getname() == "yourdbtests") { // set db } public function endtestsuite(phpunit_framework_testsuite $suite) { if($suite->getname() == "yourdbtests") { // tear down db } you can define db tests in testsuite in xml configuration file shown in docs
<phpunit> <testsuites> <testsuite name="db"> <dir>/tests/db/</dir> </testsuite> <testsuite name="unit"> <dir>/tests/unit/</dir> </testsuite> </testsuites> </phpunit> bootstrap
using phpunits bootstrap file create class creates db , tears down in it's own __destruct method when process ends.
putting reference object in global scope ensure object gets destructured @ end off tests. ( @beanland pointed out: using register_shutdown_function() makes whole lot more sense!)
using test suites:
http://www.phpunit.de/manual/3.2/en/organizing-test-suites.html shows:
<?php class mysuite extends phpunit_framework_testsuite { public static function suite() { return new mysuite('mytest'); } protected function setup() { print "\nmysuite::setup()"; } protected function teardown() { print "\nmysuite::teardown()"; } } class mytest extends phpunit_framework_testcase { public function testworks() { $this->asserttrue(true); } } this works in phpunit 3.6 , work in 3.7. it's not in current docs "test suite classes" deprecated/discouraged going around quite time.
note tearing down , setting whole db each test case can quite useful fight inter-test-dependencies if don't run tests in memory (like sqlite memory) speed might not worth it.
Comments
Post a Comment