codeception/module-db
Database module for Codeception that handles connecting to a DB, running queries, and cleaning up between tests. Supports populating data, verifying records, and fast resets to keep acceptance and functional test suites reliable and repeatable.
Start by installing the module via Composer: composer require --dev codeception/module-db. Then enable it in your codeception.yml or suite config (e.g., tests/acceptance.suite.yml) under modules: enabled. Configure the database connection using config with dsn, user, password, and optional cleanup settings (e.g., true to truncate tables after tests). The first use case is typically seeding a test database—use haveInDatabase(), grabFromDatabase(), or seeInDatabase() in your tests to assert or insert data.
Use haveInDatabase() for creating isolated test data in single tables, and combine it with seeInDatabase() for assertions on persisted records. For multi-table scenarios, prefer factories (if using Laravel) or custom helper methods that call haveInDatabase() multiple times. Utilize grabFromDatabase() to fetch values (e.g., IDs) for dynamic test data setup. In cleanup mode (cleanup: false), manually roll back transactions using seeInDatabase() within before()/after() hooks if you need persistent data across multiple test steps. For Laravel projects, integrate with codeception/module-laravel to leverage model factories seamlessly.
⚠️ The cleanup setting is per-suite—misconfiguring it can cause false negatives from stale data. Always verify db connection settings match your test environment (e.g., use separate DB from dev/prod). ⚠️ When using MySQL, be aware of sql_mode strictness that may cause silent failures—override via DSN options if needed. ✅ Use debug: true in DB config temporarily to log raw SQL queries during debugging. ✅ For speed, disable cleanup during local CI runs and manually reset DB via codecept run --debug --steps to inspect state. ✅ Extend Codeception\Module\DB to add project-specific helpers (e.g., registerUser() with complex validation flows).
How can I help you explore Laravel packages today?