Summer Sale! All accounts are 50% off this week.

connorm's avatar

Pest - Question about "sequential" tests

Forgive me if this question isn't formed that well, as I'm not sure if I'm thinking about this properly.

I'm trying to figure out what the convention is to write one test in Pest that relies on data from a previous one (within the same Feature).

This might make more sense:

it('can post to endpoint and get response', function () {
    $intermediateResponse = $this->post(
        route('intermediate-route'),
            [
                'array_1' => $this->data_1->toArray(),
                'array_2' => $this->data_2->toArray(),
            ])
    );

    $intermediateResponse->assertStatus(200);
});

it('can post response data to next endpoint ', function () {

/** Ideally, I would like to be able to use the same data from $intermediateResponse above to then pass to this next request */
		
    $response = $this->post(
		route('next-route'), $intermediateResponse->toArray()
	);	
			
	// $response-> some other test
											
    $response->assertRedirect(route('destination-route'));
});

What's not represented here is some of the business logic validation that I need to do after this second test, which is why I'd really like to preserve the response data from the first test.

Ultimately, if I do both of these things within the same test, it's no problem at all, but I have other cases that I'd like to cover which would adopt a similar pattern for what I'm going for here, and it would also be nice to have them separated out by their request while still being able to share data.

Is my mental model here flawed?

0 likes
1 reply
Sinnbeck's avatar

A test should never rely on other tests. You can have a common set up for multiple tests so they have the same base. But any test should be able to run by itself, and your test suite should be able to run in random order

So here you need to get that same response in both tests, if you need it in both :)

1 like

Please or to participate in this conversation.