Laravel 8 not running newly created tests, and not picking up deleted tests
Asked Answered
L

5

10

I'm just getting started with the Laravel 8 testing suite and have opted to create a feature test for my account creation process. I've ran php artisan make:test AccountCreation and have written the first test case as a function, however, when I run php artisan test it's not picking up my feature tests, why?

Equally, if I try to delete the default example test, I get an error telling me that the test can't be found? What am I missing?

tests/Feature/AccountCreation.php

<?php

namespace Tests\Feature;

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

class AccountCreation extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_creates_user_account_successfully()
    {
        $response = $this->post('/api/account/create');
        $response->assertStatus(201);
    }
}

Is there a special command I need to run for Laravel to pick up these tests?

no tests

Landmark answered 15/3, 2021 at 18:56 Comment(2)
Unless otherwise configured, I believe PHPUnit looks only for classes that end with Test, so you can probably just change AccountCreation to AccountCreationTest. Oh, and rename the file accordingly as well.Possessive
"Oh, and rename the file accordingly as well" — "Confirmed. That's what I forgot some time as well.Isma
U
26

According to laravel doc, you should append 'Test' to your test class, because PHPUnit will check all classes end with Test, so change:

class AccountCreation extends TestCase { ...

to:

class AccountCreationTest extends TestCase { ...

Don't forget to change your class file name.

Undistinguished answered 15/3, 2021 at 20:19 Comment(0)
M
6

For others who end up at this question but for whom the accepted answer does not help. PHPUnit expects the test class to end in 'Test' and also for the individual test method to start with the word 'test'.

So this works as per @Abolfazl Mohajeri's answer:

class AccountCreationTest extends TestCase
{
    public function test_creates_user_account_successfully()
    {
        //code
    }
}

This does not run tests (the original question from @Ryan H):

class AccountCreation extends TestCase
{
    public function test_creates_user_account_successfully()
    {
        //code
    }
}

This also will not run tests:

class AccountCreationTest extends TestCase
{
    public function it_creates_user_account_successfully()
    {
        //code
    }
}

@keepyourmouthshut's answer works because of the PHPUnit annotation documented here

Manometer answered 26/3, 2023 at 23:35 Comment(0)
J
3

/** @test */

Put this before every test. It worked for me.

Jitters answered 17/6, 2022 at 19:38 Comment(0)
C
0

In my case I had this issue because my data provider wasn't a static function. By changing it to a static function, my test could run properly.

Cultivated answered 26/4, 2023 at 6:59 Comment(0)
S
-1

My issue was that in one place my code was throwing a QueryException, which caused the tests to stop executing without any errors in the console.

Since it wasn't clear which test was causing the error, I had to comment tests one by one to find the broken one and use debugger to find the problematic part.

Smilax answered 21/12, 2023 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.