Upgrading PHPUnit - "metadata in doc-comments is deprecated [...] use attributes instead"

> composer test

Metadata found in doc-comment for method
MyTest::it_loads_user_ok(). Metadata in
doc-comments is deprecated and will no
longer be supported in PHPUnit 12. Update
your test code to use attributes instead.

Recently I was working on some package updates for Laravel 11 compatibility. As part of this, the version of PHPUnit I was using was bumped from 9 to 11. I ran the tests, and then started getting warnings like the above. The tests all passed, but a lot of notices were clogging up my terminal.

This is happening because our test code previously looked something like this:

class MyTest extends TestCase
{
    /** @test */
    public function it_loads_the_user_ok()
    {
        ...
    }

The @test doc comment there meaning we don't need to add test into our function names for PHPUnit to pick them up and run them. However, support for these doc comments is being removed in PHPUnit 12, with deprecation warnings being shown in current versions.

From the PR covering this update:

Support for attributes in PHPUnit will be implemented like so:

  • PHPUnit 10 will first look for metadata in attributes before it looks at comments
  • When metadata is found in attributes, metadata in comments is ignored
  • Support for metadata in comments is closed for further development (bugs will be fixed, but no new functionality will be implemented based on annotations)
  • Support for metadata in comments will be deprecated in PHPUnit 11
  • Support for metadata in comments will be removed in PHPUnit 12

The solution is to use attributes to replace these comments. Our example above becomes:

use PHPUnit\Framework\Attributes\Test;

class MyTest extends TestCase
{
    #[Test]
    public function it_loads_the_user_ok()
    {
        ...
    }

In this case, the @test comment is replaced by a Test attribute, loaded from PHPUnit\Framework\Attributes\Test. Once the comments get swapped out for attributes, the warnings go away, and the test suite is nice and clean again!

A much cleaner test run!
A much cleaner test run!

The full list of replacements, and how they map from comment to attributes, is available on the PHPUnit PR.

Before and after
Before and after

Share This Article

Related Articles


ConFoo 2026, Montréal

My experience speaking at ConFoo 2026 in Montréal — two talks, a brilliant technical crowd, some excellent sessions, and a city that is genuinely something else in winter.

Redirecting Test Emails Safely in Laravel

A small Laravel feature that prevents a big headache: how Mail::alwaysTo() keeps staging and demo environments from accidentally emailing real users.

International PHP Conference, Berlin 2025

In June 2025, I spoke at IPC Berlin about idempotency - what it is, how it protects our APIs, and what goes wrong when we forget about it! It was a great conference, in a fascinating city.

Dutch PHP Conference, Amsterdam 2025

The Dutch PHP Conference in March 2025 was a great couple of days, where I got the opportunity to talk all about idempotency, and do it in a unique conference setting.

More