tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.88k stars 526 forks source link

Integration/Unit Testing Sqflite #671

Open fernandasdias opened 3 years ago

fernandasdias commented 3 years ago

Hello folks,

I was hoping that someone could clarify how to proper work with integration testing in sqflite. I've seen a buzz in the past about it and I'm not sure how do implement it in a simpler way.

Thanks!

alextekartik commented 3 years ago

Integration testing for flutter is now described here: https://flutter.dev/docs/cookbook/testing/integration/introduction and sqflite should not add any specific setup.

But integration/plugin testing is still painful (in my opinion) and since I find it weird to have to use Widget to test non-UI code, I personally recommend using sqflite_common_ffi to run regular flutter test or simple dart vm test on your code that uses sqflite. More information is available here: https://github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/testing.md.

fernandasdias commented 3 years ago

Thanks! When using sqflite_common_ffi is it possible to test the same database from my application or do I need to create one? I tried using the same path but it didn't work.

alextekartik commented 3 years ago

Indeed the path during unit testing cannot be a flutter device path. Typically I use inMemoryDatabasePath to always start with a blank database during unit testing. getDatabasesPath is unfortunately not very relevant so you might choose a local relative path in this case.