The behavior you're seeing with different locales being set when running all tests vs. a specific test file is often related to parallel testing, and/or the way that PHPUnit/Pest or Laravel handles environment setup for the test runner.
What's happening?
- When you run
php artisan test, Laravel (via Pest or PHPUnit) may run tests in parallel using multiple processes (check if your Pest or PHPUnit setup has parallel enabled). - When you run
php artisan test tests/Feature/Something/Repository/SpecificTest.php, generally only a single process is started, unless you specifically configure otherwise. - The parent process environment variables (including locale) may differ from what the worker processes inherit, or the parallel runner may intentionally set or "normalize" some settings for consistency.
- Some tools (e.g., Symfony Process, used by Laravel for parallel testing) can unset or change locales to ensure tests run consistently regardless of your shell locale.
Locale in PHP
The locale used by PHP (setlocale(LC_NUMERIC, 0)) can come from your OS, user settings, the shell you launch commands from, or specific overrides.
Why does the locale differ?
- Running tests in parallel may spawn processes with a clean environment (
LC_ALL,LANG, etc., may be different, leading to a 'C' locale). - Running a single test file might just inherit your current terminal's environment — if you have a shell with
LANG=nb_NO.UTF-8, that's passed to PHP. - Sometimes, test runners or IDEs launch tests with stripped down or standardized environment variables (e.g., just in English for error message parsing).
How to Fix or Normalize Locale in Tests
If locale affects your tests (especially stuff like decimal separators, string formatting, or image processing), you should standardize locale at the start of your test suite.
Add this to your tests/TestCase.php (or in Pest's tests/Pest.php):
public function setUp(): void
{
parent::setUp();
// Standardize locale
setlocale(LC_ALL, 'C');
}
Or, for Pest, in tests/Pest.php:
beforeEach(function () {
setlocale(LC_ALL, 'C');
});
Recommendation:
- Always explicitly set locale in your test bootstrap to ensure reproducible results across environments and when running with parallelization.
Summary:
The difference is due to how your environment variables are handled by Laravel, Pest, or parallel runners. Standardize the locale at the start of your tests to avoid such problems.
References: