Search code examples
elixirex-unit

Elixir ExUnit: Run function before the complete test suite?


There is the setup callback that is invoked before each test and the setup_all callback that is invoked only once per module, before any test runs.

I have the situation where I need to prepare something before the whole test suite runs. Only once. I am working with a non-standard DB and setting up the schema with all indexes takes a little. So I would like to speed up tests by not doing that more often than needed.

Is there a way to handle this?


Solution

  • You can put your initialization code in test/test_helper.exs. Any code in that file will be executed before the tests are run and only once. As @mudasobwa pointed out, it doesn't matter if the code is before or after ExUnit.start(), it'll be run before the tests.

    # your code here 
    
    ExUnit.start()
    
    # or here