Only starting applications (Ecto etc) if necessary when running tests

I’m finding that the startup time to run mix test on my Phoenix app is getting quite long (5–10 seconds). This isn’t a problem when running the whole suite, but is annoying when I’m test-driving a module that only contains pure functions, and doesn’t interact with the database etc.

For now, I’ve worked round it by using mix test --no-start where appropriate, and wrapping setup code in test_helper.exs with a check that the application is running, but it would be nice to run the tests with --no-start as the default, and start things as appropriate in setup hooks. I had a go at getting that set up, but couldn’t get the Ecto connection checkout stuff to work reliably (particularly with async enabled).

Has anyone got this kind of thing working, or am I embarking on a wild goose chase?

One option could be to use a Mix alias to supply default arguments to the test task:

Finally, aliases can also be used to augment existing tasks. Let’s suppose you want to augment mix clean to clean another directory Mix does not know about:

[clean: ["clean", &clean_extra/1]]

So something like:

[test: ["test --no-start"]]
# or
[test: "test --no-start"]

What technique(s) have you been trying so far to start the Ecto Repo, and what sort of errors are you receiving?

1 Like