When we upgraded a Laravel app to PHPUnit 12, the suite stayed green. It was green partly because some tests no longer ran. PHPUnit 12 does not read doc-comment metadata. Test methods that were marked only with /** @test */ were no longer found, and tests that relied on @group or @dataProvider lost that metadata. Nothing failed. They were just not executed. In the pull request that fixed it, the change was to “convert doc-comment @test / @group metadata to attributes so tests remain discovered”.
How to spot it
A green result tells you nothing about tests that did not run. Compare counts.
- Before you upgrade PHPUnit, record the number of tests it runs. The summary line at the end of a normal run shows it (for example “Tests: 267, Assertions: 760”).
- Upgrade PHPUnit.
- Run again and compare. If the number of tests dropped, tests were lost.
- Search for the old style:
grep -rn "@test\|@group\|@dataProvider" tests/. Any hit that is not also an attribute is a test that PHPUnit 12 ignores or treats differently.
A method named test_something still runs, because PHPUnit finds those by name. The tests at risk are the ones that depend on a doc comment, usually written as public function it_does_something() with /** @test */ above it. The Laravel community used this style a lot.
The fix: attributes
+use PHPUnit\Framework\Attributes\Test;
...
- /** @test */
+ #[Test]
public function the_image_upload_endpoint_is_not_accessible_to_guest_users()
@group becomes #[Group('uploads')] and @dataProvider sizes becomes #[DataProvider('sizes')], with matching use statements for PHPUnit\Framework\Attributes\Group and \DataProvider.
A script for the bulk change
We converted the affected files in a single pass. Here is a version of that kind of script. We tested it on sample files (a @test one-liner, a multi-line block with @test and @group plus a normal description, a @dataProvider block, and a plain comment that must be left alone), and running it twice changes nothing the second time. We did not run it against a PHPUnit install for this article, so treat the output as a diff to review, then run your suite.
#!/usr/bin/env python3
"""Convert PHPUnit doc-comment metadata to attributes in test files.
Handles @test, @group x, @dataProvider x. Run on a clean git tree and review the diff."""
import re, sys, pathlib
def convert(src):
uses = set()
def sub_block(m):
indent, body = m.group(1), m.group(2)
attrs, kept = [], []
for line in body.splitlines():
t = line.strip().lstrip('*').strip()
if t == '@test':
attrs.append('#[Test]'); uses.add('Test')
elif (g := re.fullmatch(r'@group\s+(\S+)', t)):
attrs.append(f"#[Group('{g.group(1)}')]"); uses.add('Group')
elif (d := re.fullmatch(r'@dataProvider\s+(\w+)', t)):
attrs.append(f"#[DataProvider('{d.group(1)}')]"); uses.add('DataProvider')
else:
kept.append(line)
if not attrs:
return m.group(0)
rest = [l for l in kept if l.strip().lstrip('*').strip() not in ('', '/**', '/')]
out = ''
if rest:
out += f"{indent}/**\n" + ''.join(f"{l}\n" for l in rest) + f"{indent} */\n"
out += ''.join(f"{indent}{a}\n" for a in attrs)
return out
pattern = r'^([ \t]*)/\*\*(.*?)\*/[ \t]*\n(?=[ \t]*(?:public|protected|private)\s+function)'
new = re.sub(pattern, sub_block, src, flags=re.S | re.M)
if uses:
stmts = ''.join(f"use PHPUnit\\Framework\\Attributes\\{u};\n" for u in sorted(uses) if f"Attributes\\{u};" not in new)
new = re.sub(r'(namespace [^;]+;\n)', lambda m: m.group(1) + '\n' + stmts, new, count=1)
return new
for p in sys.argv[1:]:
f = pathlib.Path(p); s = f.read_text(); n = convert(s)
if n != s: f.write_text(n); print('converted', p)
Run it on a clean git tree so you can read the diff:
python3 phpunit-annotations-to-attributes.py $(grep -rl "@test\|@group\|@dataProvider" tests/)
git diff --stat
vendor/bin/phpunit
Check the count again
After the conversion, the number of tests should be back to (or above) what you recorded before the upgrade. In our case the full suite finished at 267 tests and 760 assertions, green. If your count is still lower than before, search for other doc-comment tags you used, such as @depends, @covers or @requires.
Mixed Pest and PHPUnit files
A related trap in the same suite: files written with Pest’s it() syntax break vendor/bin/phpunit with “Call to undefined function it()”. If you run both, convert or exclude those files, and sweep the whole tests/ tree, not just one folder.