Controlling the Life Cycle of OTP Processes in Tests

Until now, when we needed to start a GenServer in our tests, we always called the GenServer’s start_link/1 function directly. We even went as far as having assertions on the return value of this function.

 assert {​:ok​, pid} = RollingAverageServer.start_link(​max_measurements:​ n)

This kind of assertion might be useful to test that the start_link/1 function behaves properly, but it has a few drawbacks. The most important one is that the process that we start is tied to the life cycle of the test process but not in a clean way. The processes are just linked. When the test finishes executing, the test process will terminate (peacefully); and since our GenServer process is linked to it, the ...

Get Testing Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.