Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

keizah7's avatar
Level 17

PhpUnit Feature tests fails locate factory in data provider

I'm using dataProvider (https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.dataProvider) in my Feature tests

<?php

namespace Tests\Feature\Category;

use App\Category;
use Tests\TestCase;

class ValidationTest extends TestCase
{
    /**
     * @dataProvider provider
     */
    public function testAdd($a, $b, $c)
    {
        $this->assertEquals($c, $a + $b);
    }

    public function provider()
    {
        return array(
            array(0, 0, 0),
            array(0, 1, 1),
            array(1, 0, 1),
            array(1, 1, \factory(Category::class)->create()->id)
        );
    }
}

For some reason factory is not working in provider.

1) Warning
The data provider specified for Tests\Feature\Category\ValidationTest::testAdd is invalid.
InvalidArgumentException: Unable to locate factory for [App\Category].
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:273
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:296
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:148
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:304
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:223
C:\Users\Arturas\code\iskelbimai\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:169
C:\Users\Arturas\code\iskelbimai\tests\Feature\Category\ValidationTest.php:26

If I run test in PhpStorm I got message No tests executed

dump(factory(Category::class)->create()->id) in testAdd() works, but it is not working in provider. Where is the problem?

0 likes
1 reply

Please or to participate in this conversation.