I am writing unit tests for my SwiftData application. Currently, I am using in-memory database, which get reset after every test. What benefits will I gain if I start using real database? My main reason is that real database (SQLite) will be the environment user will experience. I can easily wipe out the records from the real database after each test is completed.

Are there are scenario where running in-memory test will produce a different result as compared to running the test with real database? Am I ignoring and not capturing those scenarios by running it in-memory?

PS: I used the term unit tests. You can use integration test if that makes you feel better :)

  • By "in-memory vs. real" database, do you mean an SQLite in-memory DB vs. an SQLite file-on-disk database?
    – Doc Brown
    Commented Apr 16 at 19:01
  • For SQLite there is no difference between its in-memory and on-disk mode, except of course persistency. And some details around concurrent transactions might be a bit stricter if you don't enable a WAL file, not entirely sure. You can enable the in-memory mode by opening the special :memory: file instead of a real file path, which makes it really easy to integrate in tests.
    – amon
    Commented Apr 17 at 0:11
  • The biggest "advantage" of using an on-disk/on-device database in your tests is that your tests can nicely interact with each other, giving random failures when they are executed in parallel or in a different order. Commented Apr 17 at 6:42

1 Answer 1


One thing a "real database" does that an in memory database doesn't do is talk to other programs. A database often ends up being what we call "shared mutable state". That can easily result in a scenario that will produce a different result.

The in memory DB can do exactly the same thing of course. Provided something else knows about it and talks to it.

Testing the timing issues at work here is very difficult. So unless your tests are targeted at exactly those issues you're probably fine running your tests against an in memory DB. So long as they are functionally equivalent.

  • Thanks! The tests are run against the SQLite database only available on the device. It is a private database. Commented Apr 16 at 15:57
  • @user19037628 many devs start out thinking their app will be the only one to talk to the DB. Then the world changes. If you're going to design this expecting that make that assumption clear somewhere. Commented Apr 16 at 16:00
  • The app is using SwiftData. SwiftData uses Sqlite database. And syncing to iCloud is not supported. Commented Apr 16 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.