Error: Unit-Test fail to locate factory class but running the same factory in tinker (no issue in tinker).
For Laravel 6.8 or above, changes were made to “Unit-Test” to not loaded the framework anymore. So, if you need to use a “Factory” you should create a test within the “Feature-Test” directory or:
use this (in the Unit-Test):
use Tests\TestCase;
instead of:
use PHPUnit\Framework\TestCase;
For detail, you can refer to below link:
https://github.com/laravel/framework/commit/e30a0c979d98f2f1f7b6c565e4002734237a280b
Below the error capture:

But after change the Unit-Test as below:
<?php
namespace Tests\Unit\Models;
use App\Models\Category;
////use PHPUnit\Framework\TestCase; ==> Change to below
use Tests\TestCase;
class CategoryTest extends TestCase
{
public function test_category_has_many_children()
{
$childrend = Category::factory(3)->create([
'parent_id' => $parent = Category::factory()->create(),
]);
$this->assertCount(3,
Category::where('parent_id', $parent->id)->get()
);
}
}
The Unit-Test should be running:
