2

So I have a very peculiar problem with my unit tests in laravel. What I am trying to do is some basic unit testing, and so I have a single class set up with my tests. The tests worked perfectly when I had a single method that ran tests. The class looked like so:

class Pixel_BasicTest extends Illuminate\Foundation\Testing\TestCase {


public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    return $app;
}

public function testRoutes(){
    echo "Start of testRoutes\n";

    //Test all the GET routes we have
    $routeCollection = Route::getRoutes();
    $num_404s = 0;
    $path_404s = [];
    ...various other code that does some tests, these all work right
}
}

Now, the problem occurs when I try to add another function. My class now looks like this:

class Pixel_BasicTest extends Illuminate\Foundation\Testing\TestCase {


public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    return $app;
}

public function testRoutes(){
    echo "Start of testRoutes\n";

    //Test all the GET routes we have
    $routeCollection = Route::getRoutes();
    $num_404s = 0;
    $path_404s = [];
    ...various other code that does some tests, these all work right
}

public function testSecondThing() {
    echo "Start of testSecondThing";
    ...some code testing something else
}


}

Now, I know that the code from the 2nd function works, because when I put that code in the first function, and remove the 2nd function entirely, the tests run perfectly. However, when I put the code in a second function, it fails, and my terminal window looks like this:

Mikes-MacBook-Air-96:hq pixelmember$ ./vendor/bin/phpunit -v --debug
PHPUnit 4.8.29 by Sebastian Bergmann and contributors.

Runtime:    PHP 5.6.10
Configuration:  /Users/pixelmember/Documents/hq/phpunit.xml


Starting test 'Pixel_BasicTest::testRoutes'.
.Start of testRoutes
End of testRoutes

Starting test 'Pixel_BasicTest::testAfterschool'.
Mikes-MacBook-Air-96:hq pixelmember$ 

As you can see, it gets to the 2nd function, and just stops or fails or something. There is no error message, and I also don't see the message I wrote in the code at the start of the function (The echo "Starting testSecondThing()" part)

I'm honestly at a loss for what to do. I can make do without the unit tests for now, and can work on some other parts of my application, but I really want to be able to use unit tests without having to stick every single one of my tests in a single function.

In case it helps, here is my phpunit.xml file. Perhaps someone can see something in here:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false">
<testsuites>
    <testsuite name="Application Test Suite">
        <file>./tests/Pixel_BasicTest.php</file>
    </testsuite>
</testsuites>
<filter>
    <blacklist>
        <directory>./vendor</directory>
    </blacklist>
</filter>
<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="DB_DATABASE" value="test"/>
    <env name="DB_USERNAME" value="root"/>
    <env name="DB_PASSWORD" value=""/>
</php>

I didn't really edit or change much of this file, besides adding some database related environment variables.

EDIT: I FIXED MY ISSUE, SEE BELOW FOR FIX

1 Answer 1

4

Wow I actually just figured it out. Sometimes it just takes taking a break then haha.

In case someone needs help with a similar problem, what I did to fix my issue was change my phpunit.xml file. I changed the setting "processIsolation" to true, so my phpunit.xml file now looks like:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
      backupStaticAttributes="false"
      bootstrap="bootstrap/autoload.php"
      colors="true"
      convertErrorsToExceptions="true"
      convertNoticesToExceptions="true"
      convertWarningsToExceptions="true"
      processIsolation="true"
      stopOnFailure="false"
      syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <file>./tests/Pixel_BasicTest.php</file>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>./vendor</directory>
        </blacklist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="DB_DATABASE" value="test"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>
</phpunit>

I'm still curious why exactly this works, and what exactly this setting does? I know that this runs each test in a seperate PHP process, but I am unsure why this helps/fixes the issue, and if this truely fixed my issue or if I will run into more problems later on.

If someone with more experience with PHPUnit could shed some light on this that would be greatly appreciated.

EDIT: Some links to the documentation I used: phpunit.xml config file: https://phpunit.de/manual/current/en/appendixes.configuration.html definition of some of the settings: https://phpunit.de/manual/current/en/textui.html#textui.clioptions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.