I am getting ready to write tests for my app. This is completely new to me, so I’m going to first tackle LiveView tests.
I haven’t been able to find a high level summary of the steps to set up the entire test system. I want to make sure I understand the basic steps so that I know I’m headed in the right direction and that I haven’t missed anything.
Based on the articles I’ve read, I’ve pieced together the 5 steps below. Could someone sanity check this to see if my understanding is correct?
1. Set up Test Database:
I’m using PostgresSQL13. I have a my_app_test repository which was created during project setup so I don’t need to run ecto.create. But the repository is currently empty. To populate it with my tables, I need to run: MIX_ENV=test mix ecto.migrate
2. Populate fixed tables using seeds
Some of my tables have fixed data that does not change (e.g. geo data). The tables were populated using seed scripts. So I need to run these same scripts using this command:
MIX_ENV=test mix run priv/repo/seeds.exs
3. Ignore Sandbox Mode for now:
My understanding is that Sandbox Mode allows for concurrent transactional tests and it runs the entire test in a transaction (so nothing gets committed). If I’m understanding the documentation correctly, it uses the dev repository but it kills the transaction at the end so that nothing gets permanently added to the tables. I don’t need concurrent transactional tests and I’d prefer to use the test database, so I will skip this (unless someone says I really should use it).
4. Set up Factories:
I will need to populate the database with some fake data. I saw this article discussing the advantages of ExMachina and Faker. I’m not entirely sure how I point ExMachina to my test database and not my dev database, but I’m assuming there is a configuration somewhere.
Then I read this page on Ecto Test Factories. It looks pretty straight forward, but a number of articles say that more complex tests will require more complex factories, so it’s better to leverage an existing library like ExMachina.
Any thoughts on this?
5. Write LiveView Tests:
My app is almost entirely in LiveView, so I’m going to focus on just writing LiveView tests. These tests will call functions in the context module and schema, so I don’t see the point of writing separate tests for those.
By focusing tests on my app’s forms and tables, I will be testing the accuracy of all the functions. And since it is using the test database, all the ecto calls and underlying relationships will also be verified. If it doesn’t display the expected output … then something has gone wrong.
Am I missing anything major by taking this approach?
My resources for writing these tests are the Pragmatic Studio course, German Valasco’s Testing LiveView course and the book Texting Elixir by Andrea Leopardi and Jeffrey Matthias. Any other resource recommendations would be welcomed!
Does this all sound correct? Have I missed anything that I should research before I set up my test environment?