I have a PHPUnit test case class (consisting of some test functions). I would like to write a oneTimeSetUp()
function to be called once for all my tests in the clas
Expanding on accepted answer:
None of the setUpBeforeClass()
, tearDownAfterClass()
, @beforeClass
, @afterClass
runs in the object context (static methods). You can work around that restriction by guarding any @before
code with a static property instead, like so:
class MyTest extends PHPUnit\Framework\TestCase
{
private static $ready = false;
/**
* @before
*/
protected function firstSetUp()
{
if (static::$ready))
return;
/* your one time setUp here */
static::$ready = true;
}
}
It can't be used for @after
, however, because there's no way of saying when the last test was called.