In this blog post, I'd like to show the easiest way to skip tests conditionally in tests. Using the simple technique, which can be used for other things besides skipping tests, you can make your tests much more flexible.

When this feature is complete, Flare users will get AI-powered solutions out of the box with their Flare subscription.

it('it can get a response from open ai', function () { $response = app(OpenAiClient::class) ->useApiKey(config('services.open_ai.api_key')) ->ask('How much is 5 + 5?'); expect($response)->toContain('10'); })->skip( fn() => config('services.open_ai.api_key') === null, 'Skipping test because config key `services.open_ai.api_key` is not set' ); When running the test now, the reason why the test is skipped is displayed.

it('it can get a response from open ai', function () { $response = app(OpenAiClient::class) ->useApiKey(config('services.open_ai.api_key')) ->ask('How much is 5 + 5?'); expect($response)->toContain('10'); })->whenConfig('services.open_ai.api_key'); The package offers these similar functions that can be tacked on to any test: whenEnvVar($envVarName): only run the test when the given environment variable is set whenWindows: the test will be skipped unless running on Windows whenMac: the test will be skipped unless running on macOS whenLinux: the test will be skipped unless running on Linux whenGitHubActions(): the test will be skipped unless running on GitHub Actions skipOnGitHubActions(): the test will be skipped when running on GitHub Actions whenPhpVersion($version): the test will be skipped unless running on the given PHP version, or higher.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners