Run once before starting tests
Just put the common code in your test/test_helper.exs
:
ExUnit.start()
# Common Code
ModuleOne.some_method
ModuleTwo.other_method(args) # etc
Run before every test
Assuming you have already worked out cleaning up the db and running migrations between tests, you can put something like this in your test/test_helper.exs
:
defmodule TestProject.Helpers do
def setup do
# Common Code
end
end
And use this setup
block in all of your tests:
setup do
TestProject.Helpers.setup
end
Setting up a Test Database / Schema
If you also need to set up a fake Database, Schema and Migrations for your tests, you'll need to define them as well, and put this in your test_helper
(assuming here that the driver you are using is Postgrex
):
# Start Ecto
{:ok, _} = Ecto.Adapters.Postgres.ensure_all_started(TestProject.Repo, :temporary)
_ = Ecto.Adapters.Postgres.storage_down(TestProject.Repo.config)
:ok = Ecto.Adapters.Postgres.storage_up(TestProject.Repo.config)
{:ok, _} = TestProject.Repo.start_link
For a more detailed example, you can see my Ecto.Rut
package that creates a fake database (for the test
env only), and resets it before running each test.