I’m a new Yii developper and have one question about unit tests.
Can I use a memory DB during unit testing ?
I’m from a java framework (Play! Framework) and with it I can use a different database for production and for tests.
Is it possible with Yii ??
Tanks.
Yes it is possible.
You can define your Test DB in protected/config/main.php.
for example Mysql
'db' => arry(
'connectionString' => 'mysql:host=localhost;dbname=YourDBname',
'emulatePrepare' => true, // needed by some MySQL installations
'username' => 'YourUser',
'password' => 'YourPassword',
'charset' => 'utf8',
)
and for more information about DB you can find here
sorry for my bad English
phtamas
(Phtamas)
3
You can use a dedicated configuration file for tests. Take a look here.
samdark
(Alexander Makarov)
4
Thanks, I use now the test.php file in the config folder.
I specified :
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
Tests don’t go in my mysql database now but this database doesn’t contain my models tables.
So I have this error :
CDbException: The table "User" for active record class "User" cannot be found in the database.
Any idea ?
samdark
(Alexander Makarov)
6
You should create table first.
tq0fqeu
(Tq0fqeu)
7
Do not support table prefix, it will throw a exception that can not found table name "{{xxx}}" doing this way.
zeroByte
(Dispyfree)
8
MySQL has a dedicated table type called MEMORY for in-memory tables.
Regards,
The SQLite in-memory option seems to be best. At least that’s what the creator of PHPUnit recommends.