JonattanD's avatar

Test fails when running all test

I have multiple test and when I run each of them individually they all pass, but when I run all test at once one of them always fails, in this case it's the "user_can_login" test, it fails with a 401(Unauthorized).

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserTest extends TestCase
{

    use RefreshDatabase;

    /** @test */
    public function user_can_login()
    {
        $response = $this->post(route('user-login', [
            'samaccountname' => env('TEST_USER'),
            'password' => env('TEST_USER_PASS')
        ]));

        $response->assertStatus(200);
 
    }
}

I've remove all tests keeping only the example test and it keeps failing, this also happens with some other test I've written. The example test:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}
0 likes
8 replies
chaudigv's avatar

First, do not use env directly rather use it via config. Second, just to confirm that's the issue, use a valid fixed value for samaccountname and password and see if the issue persist when running all tests.

neilstee's avatar

@jonattand the ExampleTest test is just a sample that comes by default to Laravel.

My guess is that your / route has a middleware that it needs to be logged in first to visit. In this case, you can change your test to match that result:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(401);
    }
}

Or another way is to force login a specific user using Auth::loginUsingId(1); before running $response = $this->get('/'); where 1 is an ID that exists in your users table.

JonattanD's avatar

Sorry I've updated my question since I wasn't being clear. The issue is with the user_can_login test. Passing the user credential by reading the .env has no adverse effect, because I've already tried typing the credentials directly into the test and it still fails. The "/" route is not protected by any middleware.

Tray2's avatar

Where do you add the user to the database? If there are no users in the database which it isn't when you refresh the database it will fail the login.

chaudigv's avatar

What we are asking is, do you have a user either in db or MS Active directory server to begin with? If there is no user than your login is gonna fail every time.

JonattanD's avatar

The user exists and I can manually login using it's credential.

JonattanD's avatar
JonattanD
OP
Best Answer
Level 1

Was not able to figure out what the problem was, but found a way around it by enabling phpunit to run each test in a separate process by changing processIsolation to true.

Please or to participate in this conversation.