- What is the error
- What is phpunit.xml set to use as database?
<server name="DB_CONNECTION" value="testing"/>
<server name="DB_DATABASE" value=":memory:"/>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Greetings friends developers !
I'm a noob on Unit Testing and i try to follow the series on unit testing, i'm on the beggining.
I searched for someone who had the same problem but i didnt find one...
I try to do a basic UserTest to make some practise of what i learned. But when i want to fetch my Users, i ve got an error on my "php artisan test" that i dont understand.
I guess its my DB connection that doesnt work and i dont know why. What do i miss to make my DB connection work on testing purposes ?
Is there anyone that could help me please ?
** NB : i've tested my DB connection on tinker and everything works fine
`<?php
namespace Tests\Unit;
use App\User; use PHPUnit\Framework\TestCase; use Illuminate\Database\Eloquent\Factory; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\DatabaseTransactions; use Database\Factories\UserFactory;
class UserTest extends TestCase { use DatabaseTransactions;
protected function createUser()
{
$user1 = User::create([
'name' => "test",
'email' => "[email protected]",
'password' => "secret"
]);
}
/** @test */
public function there_are_2_users()
{
$this->createUser();
// $users = User::all();
// // $count = $users->count();
$count = 2;
$this->assertEquals(2, $count);
}
}`
Instead of use PHPUnit\Framework\TestCase use use Tests\TestCase; instead.
You need to extend the Laravel TestCase to give you access to the framework.
You're also using both of these:
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
You just need one.
Please or to participate in this conversation.