Monday, April 3, 2017

Have setup method run only once

I have:

1. IntegrationTestCase extends TestCase  
2. UnitTestCase extends TestCase
3. AcceptanceTestCase extends TestCase  

In these I have quite a lot of non-static methods which are used in a lot of tests. All of my Test classes extend one of these 3 classes.

Now in a lot of Test classes I have a setUp method which preps the data and services needed and assigns them to class variables:

class SomeTestClass extends IntegrationTestCase
{
    private $foo;

    public function setUp()
    {
        parent::setUp();

        $bar = $this->createBar(...);
        $this->foo = new Foo($bar);
    }

    public function testA() { $this->foo...; }  
    public function testB() { $this->foo...; }
}

Problem is setUp is ran for each test defeating what I wanted to do and if what setUp method does takes a long time this is multiplied by the number of test methods.

Using public function __construct(...) { parent::__construct(..); ... } creates a problem because now lower level methods and classes from Laravel are not available.



via Sterling Duchess

Advertisement