Reputation: 115
When I run my tests, PHPUnit 8 crashes and generates a Fatal error: Allowed memory size of x bytes exhausted. I think it only crashes when some tests fail.
When I used it yesterday for the first time I set up 2 very simple tests comparing 2 strings. All worked fine when both tests passed. When I deliberately let 1 test fail it still worked. When both tests failed I got the specified error.
I found out that --process-isolation
when executing PHPUnit 8 fixes the problem, however now it takes an incredible amount of time to execute the tests (5 minutes for about 30 simple string comparisons), increasing with every new test, which is obviously not feasible in the long run.
Also increasing the allowed memory really shouldn't be an answer to this problem because a) it would probably only be a temporary fix and b) it can't be that PHPUnit 8 uses all the available memory for only 2 simple failing tests.
I already uninstalled and reinstalled PHPUnit 8 which didn't affect the problem in any way.
Maybe important: I installed PHPUnit with Composer.
Update: Ok I just tried it out on my colleague's machine and it worked perfectly fine there with the same code base. This is getting weird...
This is the code I'm using to test my method:
class MyClassTest extends TestCase
{
public function myFunctionProvider()
{
// horizontal tab
$ht = "\u{0009}";
...
return [
'descr_1' => [' abc ', 'abc' ],
'descr_2' => ['d e f' , 'd e f'],
'descr_3' => ['ghi' , 'ghi' ],
'descr_4' => [$ht . $ht . 'jkl' . $ht , 'jkl' ],
'descr_5' => ['m' . $ht . $ht , 'n' . $ht . 'o' , 'm n o' ],
...
];
}
/**
* @dataProvider myFunctionProvider
*/
public function testMyFunction(string $dirtyString, string $expectedResult): void
{
$actualResult = MyClass::myFunction($dirtyString);
$this->assertEquals(
$expectedResult,
$actualResult
);
}
}
How can I get PHPUnit to not eat all my resources and function properly?
Edit: Based on Jakub Zalas' comment, here is the implementation of myFunction():
public static function myFunction(string $dirtyString): string
{
$regex = '/\s+| +/';
$dirtyString = trim($dirtyString, $regex);
$cleanedString = preg_replace($regex, ' ', $dirtyString);
return $cleanedString;
}
Upvotes: 4
Views: 2803
Reputation: 1698
I know this is an old question but I just ran into this same problem with the dreaded "memory exhausted" fatal error whilte running PHP Unit with Symfony phpunit-bridge. For me, adding the "--process-isolation" flag ran the tests just fine. From previous experience, I found this often mitigates PHP Unit tests that throw that memory error.
For Symfony specifically the command is:
$ ./vendor/bin/simple-phpunit --process-isolation
Upvotes: 4