Search code examples
phpphpunitcodeigniter-4

No tests found in class "Tests\Support\UserDatabaseTest" in CodeIgniter 4


I need your help please, I still learning codeigniter 4 and try to build an app, I found a problem here when i build testing to my database and model and show like this when i ran the test

> phpdbg -qrr .\vendor\bin\phpunit
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

W                                                                   1 / 1 (100%)

Time: 00:00.094, Memory: 10.00 MB

There was 1 warning:

1) Warning
No tests found in class "Tests\Support\UserDatabaseTest".

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

Generating code coverage report in Clover XML format ... done [00:00.179]

Generating code coverage report in HTML format ... done [00:00.663]

Generating code coverage report in PHP format ... done [00:00.017]


Code Coverage Report:   
  2021-04-05 09:17:15   

 Summary:
  Classes:  0.00% (0/4) 
  Methods:  0.00% (0/4) 
  Lines:    0.00% (0/19)

my test is only one test, the test is to test data my user database test and soft deletion like this UserDatabaseTest

namespace Tests\Support;

use Tests\Support\Database\DatabaseTest;
use CodeIgniter\Test\CIDatabaseTestCase;

class UserDatabaseTest extends CIDatabaseTestCase
{
    protected $migrate = true;
    protected $refresh  = true;
    protected $seed = "UserSeeder";


    public function setUp(): void
    {
        parent::setUp();
    }

    public function tearDown(): void
    {
        parent::tearDown();
    }

    public function userFindAllTest()
    {
        $model = model('UserModel');
        $objects = $model->findAll();

        $this->assertCount(30, $objects);
    }

    public function UserSoftDeleteTests()
    {
        $model = model('UserModel');

        $object = $model->first();
        $model->delete($object->id);

        $this->assertNull($model->find($object->id));

        $criteria = ['id' => $object->id, 'deleted_at' => null];
        $this->dontSeeInDatabase('users', $criteria);

        $criteria = ['deleted_at' => null];
        $this->seeNumRecords(29, 'users', $criteria);
    }
}

i have no clue about the problem and how to fix the problem, thanks for your help before.

full project github


Solution

  • All the test methods should start with "test". So userFindAllTest will change to testUserFindAllTest and UserSoftDeleteTests will change to testUserSoftDeleteTests. Also, the best practice for the naming test method is to use lowercase and separate words with an underscore. Hence, testUserFindAllTest should be test_user_find_all (removing test at the end since it is implied).