laravel/pail

Logs issued from tests do not show up

tristanjahier opened this issue · 4 comments

Laravel Package Version

1.0.0-beta.2

Laravel Version

10.28.0

PHP Version

8.2.9

Database Driver & Version

No response

Description

Hello! I have just installed Laravel Pail as a development dependency.

The logs written from inside PHPUnit tests do not show up in Pail output.
Even though it works fine when issued from an HTTP controller or Tinker.
The logs are correctly appended to my log file (storage/logs/laravel.log).

Steps To Reproduce

  1. Add logger()->info('this is a test'); at the beginning of a test.
  2. Start Pail.
  3. Run the tests with php artisan test.
  4. "this is a test" does NOT appear in Pail's output.

I thought that it might be a problem of environment, but I can't run Pail in 'testing' env unfortunately because the pail command is not registered.

When running your reproduce steps I get the following error in my terminal:

1) Tests\Unit\ExampleTest::test_that_true_is_true
Illuminate\Contracts\Container\BindingResolutionException: Target class [log] does not exist.

/Users/bas/Sites/ami-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:912
/Users/bas/Sites/ami-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:795
/Users/bas/Sites/ami-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:731
/Users/bas/Sites/ami-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:120
/Users/bas/Sites/ami-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:487
/Users/bas/Sites/ami-app/tests/Unit/ExampleTest.php:16

Caused by
ReflectionException: Class "log" does not exist

Test file:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_that_true_is_true()
    {
        logger()->info('test_that_true_is_true');
        $this->assertTrue(true);
    }
}

I did a quick Google search and found this:

... The PHPUnit parent class does not boot the framework, so logging is not available.

So it seems like it's not available when testing by default. I also don't see the logs in my storage/logs/laravel.log.

That's not a bug. It was intentional back then, because typically tests don't use any log driver at all.

OK, too bad. :( That could be sometimes useful to debug tests and not have the output lost in the test results. In particular when you are confused in which order things like truncation, seeders, set up and tear down occur.