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

TerjeNesthus's avatar

Different locales with artisan test and artisan test testfile

Hello! I found something that boggles myh mind and I would like to understand better.

I found a test was failing when i did php artisan test but not when did php artisan test tests/Feature/Something/Repository/SpecificTest.php

After some debugging, I found that if i echo'ed out this in the test:

setlocale(LC_NUMERIC, 0)

It was 'nb_NO.UTF-8' when i did php artisan test

And it was 'C' when i did php artisan test tests/Feature/Something/Repository/SpecificTest.php

Apparently this is why my test failed, because with nb_NO, decimals use commas, and ImageMagick’s MVG/draw parser expects dots.

But how does this differ, where/how does it set the different locales for these two ways of testing?

Laravel 11.30 and pest 3.5, on a debian 12 using apache and php 8.3

0 likes
1 reply
LaryAI's avatar
Level 58

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:

Please or to participate in this conversation.