CodeIgniter Forums
Unit testing with database in CI 3.x - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: Unit testing with database in CI 3.x (/thread-67610.html)



Unit testing with database in CI 3.x - aaldhahe - 03-15-2017

Hello all,

I have started writing Unit tests for an existing application. I am testing with database. I wanted to know if is there a way reinitialize MySQL back to its state before running the test. What am looking for is suggestions and resources. Thanks all.


RE: Unit testing with database in CI 3.x - InsiteFX - 03-15-2017

What do you mean by reinitialize MySQL Database?

Back to an empty database or what?

If it's back to an empty database then all you need to do is delete all the tables.


RE: Unit testing with database in CI 3.x - aaldhahe - 03-16-2017

(03-15-2017, 04:07 PM)InsiteFX Wrote: What do you mean by reinitialize MySQL Database?

Back to an empty database or what?

If it's back to an empty database then all you need to do is delete all the tables.

Sorry for the misunderstanding. I meant that I have a database at a certain state. I am testing my models, and some tests insert and delete info from the database, I want the database to go back to the state it was before running the tests.


RE: Unit testing with database in CI 3.x - InsiteFX - 03-16-2017

You would need to make an sql dump of the normal state then delete all tables and run the sql dump to put it back.

You can do this using CodeIgniter Query Builder.


RE: Unit testing with database in CI 3.x - aaldhahe - 03-17-2017

(03-16-2017, 01:29 PM)InsiteFX Wrote: You would need to make an sql dump of the normal state then delete all tables and run the sql dump to put it back.

You can do this using CodeIgniter Query Builder.

Thank you


RE: Unit testing with database in CI 3.x - kenjis - 03-19-2017

If you use ci-phpunit-test, it provides sample database seeders.
See https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md#database-seeding