[Bug]: A test file that another test file require_once's reports no test cases of its own
What Happened
When a Pest test file is require_onced by another test file, the required file's test() calls are
registered against the requiring file. The required file itself contributes no test cases, and nothing in
the output says so. Under --parallel the same tests then execute once per requiring file, so the serial
and parallel case counts disagree.
PHPUnit's own loader throws ClassCannotBeFoundException when a file it was asked to load declares no
matching test class instead of ignoring the file, and it attributes a require_onced class-based test file
correctly. Class-based tests inside Pest also keep their own file. This affects the functional API only.
How to Reproduce
composer require --dev pestphp/pest, a phpunit.xml pointing at tests, and two files:
<?php
// tests/AlphaTest.php
require_once __DIR__.'/BetaTest.php';
test('alpha one', function (): void {
expect(true)->toBeTrue();
});
<?php
// tests/BetaTest.php
test('beta one', function (): void {
expect(true)->toBeTrue();
});
test('beta two', function (): void {
expect(true)->toBeTrue();
});
Expected: three test cases, two of them belonging to tests/BetaTest.php, which is what
./vendor/bin/pest tests/BetaTest.php reports when the file is run on its own.
Actual:
PASS Tests\AlphaTest
✓ beta one
✓ beta two
✓ alpha one
Tests: 3 passed (3 assertions)
--log-junit agrees:
file="tests/AlphaTest.php"
file="tests/AlphaTest.php::alpha one"
file="tests/AlphaTest.php::beta one"
file="tests/AlphaTest.php::beta two"
tests/BetaTest.php appears nowhere. Deleting the require_once line gives the same three passing tests
with file="tests/BetaTest.php" on two of them.
Add a second file that also requires BetaTest.php and the serial and parallel counts stop matching:
<?php
// tests/A2Test.php
require_once __DIR__.'/BetaTest.php';
test('a2 one', function (): void {
expect(true)->toBeTrue();
});
$ ./vendor/bin/pest
Tests: 4 passed (4 assertions)
$ ./vendor/bin/pest --parallel
Tests: 6 passed (6 assertions)
Serially only the first require_once has any effect, so beta one and beta two run once. Each parallel
worker loads one file, so both requirers pull them in and they run twice. The parallel JUnit log has two
beta one cases and two beta two cases.
Pest Version
4.7.8 (with phpunit/phpunit 12.5.33 and brianium/paratest 7.20.0)
PHP Version
8.4.25
Operation System
macOS 27.0.1
Notes
Pest\Support\Backtrace::testFile() (src/Support/Backtrace.php) walks the backtrace until it reaches
overrides/Runner/TestSuiteLoader.php and keeps the outermost frame below it. A test() call inside a
required file therefore resolves to the requiring file, and the TestCaseFactory lands in
TestRepository::$testCases under that key.
When the loader later reaches the required file, include_once is a no-op because the file is already
included, and TestRepository::makeIfNeeded() (src/Repositories/TestRepository.php:161) returns early
because no entry is keyed by that filename. No class is declared, so TestSuiteLoader::load() falls through
to exceptionFor(), which returns ReflectionClass(IgnorableTestCase::class) without emitting a message.
The file is dropped with no warning, no skip and no risky mark.
Anything that works per file then stops agreeing with the suite: JUnit file attribution, per-file coverage,
CI shards selected by path, and any check that compares test files on disk against the files a run reported.
An exit code of 0 with a case count that changes between serial and parallel is the only external sign.
Supporting require_once between test files may not be worth doing. A diagnostic would be enough: if a file
Pest was asked to load produces no test case and declares no class, say so rather than returning an
IgnorableTestCase.
One more consequence of the same mechanism. It can drop tests from a group-filtered run, not just misattribute them.
Add pest()->group('beta'); to the top of tests/BetaTest.php and keep the require_once in tests/AlphaTest.php:
$ ./vendor/bin/pest
Tests: 3 passed (3 assertions)
$ ./vendor/bin/pest --exclude-group=beta
INFO No tests found.
alpha one was never in the beta group, but it doesn't run. The group call in the required file is applied to the requiring file's factory, so it tags alpha one as well. Remove the require_once and --exclude-group=beta runs alpha one (1 passed), as expected.
This matters for suites that split CI lanes with --group / --exclude-group. A test can drop out of every lane while each lane stays green.
Same versions as above: Pest 4.7.8, PHPUnit 12.5.33, PHP 8.4.25.
Reproduced on current 5.x (1b3ca5ba): with AlphaTest.php and A2Test.php both requiring BetaTest.php, a serial run reports 4 tests and --parallel reports 7, and BetaTest.php never appears in the JUnit log.
I'd like to work on this. Two possible directions, and I'd like to know which one you'd accept before opening a PR:
- Diagnostic only (as suggested above): when
TestSuiteLoaderreaches a file that produced no test case and declares no class, report it (e.g. a test runner warning naming the file) instead of silently returningIgnorableTestCase. Behaviour of existing suites is unchanged; they just get told. - Attribute tests to the required file: make
Backtrace::testFile()stop at the innermostrequire/includeframe, sotest()calls register under the file they're written in. That fixes the JUnit attribution and the parallel duplication, but it would break anyone who intentionallyrequires a shared file of tests (e.g. a non-*Test.phpfile) into several test files, since those tests would no longer run.
Option 1 seems the safer fit. Happy to send a PR with a test for whichever you prefer.