PHPUnit has setup
and tearDown
events that run, respectively, before and after each test within a test case. In my specific scenario, I also want to run something like a testCaseSetup
and testCaseTearDown
. Is that possible?
Current solution looks like this:
<?php
class MyTestCase extends \PHPUnit_Framework_TestCase
{
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
// My test case setup logic
parent::__construct($name, $data, $dataName);
}
public function __destruct()
{
// My test case tear down logic
}
}
But it seems far from optimal for the following reasons:
PHPUnit_Framework_TestCase
construct and redirect any arguments. IF PHPUnit constructor is changed on a version update, my test case will stop.PHPUnit_Framework_TestCase
was not declared to be used like this.I would like to know if there are better solutions. Any ideas?
Yes, there are special methods for that purpose: setUpBeforeClass
and tearDownAfterClass
.
class TemplateMethodsTest extends PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
// do sth before the first test
}
public static function tearDownAfterClass()
{
// do sth after the last test
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With