Error in laravel testing : SQLSTATE[HY000]: General error: 1 no such table: users

when I use PHPUnit in laravel and add a factory to my own test class after running PHPUnit appear this error in the console:

 SQLSTATE[HY000]: General error: 1 no such table: users 

my test class method:

public function testExistSomeTextsInIndexPage()
{
    $users= factory(User::class)->create();
    $this->get('/')->assertSee($users->name);
     
}

this factory code work correctly in other parts of my project just show the error in the test class

Answer

You should include the IlluminateFoundationTestingDatabaseMigrations as a trait. There is documentation about this.

use IlluminateFoundationTestingDatabaseMigrations;
use TestsTestCase;

class ExampleTest extends TestCase
{
    use DatabaseMigrations;
}

Note: Set the correct DB credentials. If you use your credentials for you existing DB, it will override the DB. I would advise you to use a in-memory DB with SQLite, see example here