Seeding database data for tests

I’m a bit confused as to how Ecto works with seeded data when it is conducting its tests. What EXACTLY is the flow when you commence a test run (whether it be all of them via “mix test”, or only a specific file, or a specific test by specifying a line number)? Does the testing database get seeded in this process?

For example, I have some data (e.g. a list of countries) that should be the same in all environments. Do I need to explicitly seed that in the testing database? I have other data that I COULD seed ONLY in the test database just to help write thorough integration tests. How should that be best accomplished?

Thanks for any guidance!

mix test won’t seed your database, because nothing will cleanup the data which was inserted outside of the sandbox transactions of single tests. Also having those countries seeded would mean they are the same to all tests, which imho can be as annoying as any other global state in your system. I’d rather opt for inserting those countries via a Factory on demand, so you can control on a per test basis which countries are in the database.

How do you make a factory that would seed the testing database? If I run the seed operation using mix, it will by default get added to my dev database. Thanks!

Try ex_machina.

1 Like

Interesting… but that generates random data. I’m really needing to seed specific data for some pretty specific tests… especially when some of the end-to-end tests in our architecture are further downstream.

A factory is about making it easy to inserts some data into the database. If it’s random or not is more about what you need. In a simple case it could just be a module, which you wrote, that does add some data to the db.

Thank you, this is helpful – I need some time to digest it.