Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

founders's avatar

"Use of undefined constant LARAVEL_START" when running a Test

I'm starting to learn Tests now, this simple method bellow is returning error 500 on my homepage, even though I can see it just fine in the browser:

public function testExample()
{
    $response = $this->get('/');
    $response->assertStatus(200);
}

The error is that the constant LARAVEL_START which I use to show processing time of page isn't defined in the moment of testing.

Am I doing something wrong?

0 likes
6 replies
shez1983's avatar

i just consulted my crystal ball but was unable to see anything about your code.. so could you please tell us where LARAVEL_START is defined & relevant code for route '/' ie the controller function it calls

1 like
YazanStash's avatar

@founders the LARAVEL_START constant is defined at the start of either an artisan run or the the index.php used for web requests, and because PHPUnit uses neither the constant is not defined when you try to access it. The easiest solution is to add something like this to your parent TestCase in the setUp() method

// Right after `parent::setUp();
if (! defined('LARAVEL_START')) {
    define('LARAVEL_START', microtime(true));
}
2 likes
YazanStash's avatar

@Snapey I had the same issue, and while searching the web came across this question. Then later I figured it out so came back to comment the solution so future people (also me) find a solution ready.

2 likes

Please or to participate in this conversation.