What is CodeIgniter 4 feature testing best practices?
Answer
CI4's feature testing simulates real HTTP requests against your application. Best practices: Use separate test database configured in app/Config/Database.php under the tests group and activated with protected $DBGroup = "tests" in the test class. Use DatabaseTestTrait with $refresh = true to reset the database between tests — this rolls back all transactions or drops/recreates tables. Seed test data: specify $seed = "TestSeeder" or call $this->seed("UserSeeder"). Test authentication: $result = $this->withSession(["user_id" => 1])->get("/dashboard"). Mock services: Services::injectMock("email", $this->createMock(Email::class)). Test assertions: assertStatus(), assertRedirectTo(), assertSee(), assertDontSee(), assertSessionHas(), assertCookieSet(). Structure tests in the same namespace hierarchy as your controllers for clarity.