4

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;                                                 
}      
Equiphract
  • 107
  • 1
  • 11
  • 3
    If you already take the time to downvote my question please at least give an explanation as to why you did so. Otherwise I can't possibly improve my question and nobody gets anything out of it. – Equiphract Feb 21 '19 at 14:57
  • It looks like `MyClass::myFunction` leaks memory. It's hard to help you without knowing what's in that function, or how your test data looks like. – Jakub Zalas Feb 21 '19 at 15:13
  • @JakubZalas Edited my question according to your comment. – Equiphract Feb 21 '19 at 15:33
  • 1
    I just noticed: why does your test class extend the production class? `class MyClassTest extends MyClass` ? Your test classes should extend phpunit's `TestCase`. – Jakub Zalas Feb 22 '19 at 10:43
  • @JakubZalas Ah thanks for pointing it out, wrote this portion by hand instead of copy pasting, fixed it now. – Equiphract Feb 22 '19 at 10:56
  • I can't see why this code could generate memory problems. I also run it locally , and it's fine on my machine. Perhaps someone downvotes this question because "Why my code doesn't work?" does not really belong to SO. – Jakub Zalas Feb 22 '19 at 12:28
  • @JakubZalas I don't really understand,SO says "We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers a specific programming problem, or software tools commonly used by programmers [...] then you’re in the right place to ask your question!" I have provided source code, I have described the problem I have, I have provided plenty of information concerning the circumstances and if it works on your machine but not on mine it clearly can't be a "Why my code doesn't work?" type of question... why wouldn't this belong on SO? – Equiphract Feb 22 '19 at 12:46
  • 1
    Anyways, I will ask a colleague to install phpunit on his machine and then check if it works, update following. – Equiphract Feb 22 '19 at 12:49

1 Answers1

3

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
Aaron Belchamber
  • 1,480
  • 1
  • 16
  • 20