If you're using Laravel Herd on macOS and have tried running test coverage with the following command:

php artisan test --coverage

…only to be met with an error like this:

Error while running artisan test with coverage flag

You're not alone. The issue is that Herd does not come with PCOV as a pre-installed extension.

To verify whether pcov is installed, run:

php -m | grep pcov

If nothing shows up, pcov is not currently enabled on your machine.

Step 1: Install pcov using Homebrew

Follow this guide from Laravel News, and run:

brew install shivammathur/extensions/pcov@8.4

Replace 8.4 with the PHP version you're using in Herd.

However, note that while this installs pcov, it does not automatically enable it in Laravel Herd’s PHP environment. We'll need to enable it manually.

Step 2: Locate the pcov.so file

After installation, you’ll find the pcov.so file at a path like:

/opt/homebrew/Cellar/pcov@8.4/1.0.12/pcov.so

Adjust this path based on your PHP version and the exact version of pcov that was installed.

Step 3: Locate your php.ini file in Herd

To enable the extension in Herd:

  1. Click the Herd icon in your macOS menu bar.
  2. Choose "Open Configuration Files".
  3. This will open the relevant php.ini file in Finder.

Alternatively, you can locate it via the terminal:

php --ini

This will return a path like:

/Users/<your-username>/Library/Application Support/Herd/config/php/84/php.ini

Step 4: Enable pcov in php.ini

Open the php.ini file in your favorite editor and add the following line at the end:

extension=/opt/homebrew/Cellar/pcov@8.4/1.0.12/pcov.so

Save the file and close it.

Step 5: Restart Herd

Now restart Herd to apply the changes:

herd restart

After restarting, confirm that PCOV is enabled:

php -m | grep pcov

If you see pcov listed, you're all set!

Step 6: Run your tests with coverage

Now you can successfully run:

php artisan test --coverage

Laravel will execute your tests and display a coverage report.