JenuelDev's avatar

How to Run setup once, and all methods inside Test Class can access the variables?

So I have this test where I like to setup it once and setup the global variables that way I can use the same data on all methods inside my test class.

I am creating something like this, in a certain directly tests/Unit/Subscription/WithCard.php

class WithCard extends TestCase
{
    protected $patient;
    protected $plan;
    protected $patientSubscription;

    // i like to run this once, and use the variables patient, plan, and patientSubscription from all the methods.
    public function setup(): void {
        $this->patient = PatientFactory::new()->create();
        $this->plan = SubscriptionPlan::new()->create();
        $this->patientSubscription = new Subscription($this->patient, $this->plan)->subscribe();
    }

    public function testCheckIfSubscriptionWasSuccessfullyCreated()
    {
        $this->assertNotNull($this->patientSubscription);
    }

    public function testCheckIfSuccessfullyPay() {
        $isPaid = Subscription::isPatientPaid($this->patient, $this->plan);
        $this->assertTrue($isPaid);
    }
    // a lot of code
}

I tried using setUpBeforeClass but Im not successfull either.

0 likes
7 replies
Sinnbeck's avatar

setUp will be run for every test in the class, but that should be fine I would assume? If you are using the RefreshDatabase trait, laravel will automatically roll back data after each test. Can you explain why your current code is a problem?

1 like
JenuelDev's avatar

@Sinnbeck I am trying to use the same data on other test methods. Specially that we are using a certain API that needs to be run only once, I dont want to fetch that API on every method.

JenuelDev's avatar

@Sinnbeck yup thats what I was actually trying to achieve, I like to fetch the data from a test API only once, and use that same data on every test.

JenuelDev's avatar

@Sinnbeck I did a work around, I created a class to store my data. This works but I dont know if its good


class MyData {
    public static $patient;
    public static $plan;
    public static $patientSubscription;
}

class WithCard extends TestCase
{
    public function setupOnFirstTest () {
        MyData::$patient = PatientFactory::new()->create();
        MyData::$plan = SubscriptionPlan::new()->create();
        MyData::$patientSubscription = new Subscription($this->patient, $this->plan)->subscribe();
    }

    public function testCheckIfSubscriptionWasSuccessfullyCreated()
    {
        $this->setupOnFirstTest(); // run setup on first test to setup the data
        $this->assertNotNull(MyData::$patientSubscription);
    }

    public function testCheckIfSuccessfullyPay() {
        $isPaid = Subscription::isPatientPaid(MyData::$patient, MyData::$plan);
        $this->assertTrue($isPaid);
    }
    // a lot of code
}
Sinnbeck's avatar

@BroJenuel tests should never be dependent on each other. You should be able to run each test by itself or in random order

So here a static property to check if it had run in setup would be better

1 like

Please or to participate in this conversation.