chaney's avatar

Test failed when using Laravel testcase and PHPUnit testcase

I have two test classes in same test suite. One is extended laravel testcase another is extended phpunit testcase. When laravel testcase is executed before phpunit testcase and phpunit testcase is testing some laravel component, causing something not found or undefind error. But, executing test in counter way can pass the test.

class FirstTest extends Tests\TestCase;
{
    public function testTrue()
    {
        $this->assertTrue(true);
    }
}
class SecondTest extends \PHPUnit\Framework\TestCase;
{
    public function testIsPaginator()
    {
        $paginator = new \Illuminate\Pagination\Paginator([], 1);
        $this->assertInstanceOf(\Illuminate\Pagination\Paginator::class, $paginator);
    }
}

Digging into the laravel source code, I found Paginator using static property storing callback function and register in PaginationServiceProvider. Obviously, when running test in laravel testcase first way will storing these static value and did not reset after teardown, phpunit testcase is force to access laravel app which isn't boot then throw error.

So, in addition to attention to the order of execution carefully, does there have anyway to fix up this kind of situation?

0 likes
1 reply
mohamedallamthe1's avatar

I can tell you if you extend Laravel TestCase class, there is lot of functionality and added assertions to it, its used for integration tests that bootstraps the entire app, and runs your tests.

Anything features wont work on a unit tests. including migrating database storing data..ect

To do so dont forget to include RefreshDatabase Trait in the Feature Tests, that extend Laravel TestCase class. This will migrate the db for every test, and will drop the db after its done (tear down).

Please or to participate in this conversation.