https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Alexander Larsson

06/12/2020, 8:19 AM
Does anyone know if there is a recommended way to unit test SQLDelight common code on Android? You need an android context for the sql driver. Should I use instrumented unit tests or Roboelectric? Is there some other better way?
j

John O'Reilly

06/12/2020, 8:45 AM
This is article from last year by Sam Edwards that also covers unit testing SQLDelight code https://handstandsam.com/2019/08/23/sqldelight-1-x-quick-start-guide-for-android/
one key thing is that the unit tests are running on JVM and therefore use SQLDelight's JVM driver (JdbcSqliteDriver)
s

saket

06/12/2020, 4:09 PM
I found it easier to use an Android driver (in-memory) because it takes care of running table creation queries for me iirc.
p

Peter Hsu

06/12/2020, 4:53 PM
I used the in memory JDBC driver to run unit tests
I found the documentation to be slightly out of date, hopefully this helps:
Copy code
commonTest {
    dependencies {
      implementation kotlin('test-common')
      implementation kotlin('test-annotations-common')

        implementation "com.squareup.sqldelight:sqlite-driver:$rootProject.sqlDelightVer"
        implementation "com.squareup.sqldelight:jdbc-driver:$rootProject.sqlDelightVer"
    }
}
and to create the db
Copy code
val driver: SqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
Database.Schema.create(driver)
val db = Database(driver)
148 Views