How to spawn a cowboy server in a test?

Background

I have a specific test where I need to spawn a cowboy server listening on port 8082 that returns some static responses.

Objective

Normally I would spin up a cowboy process in my application.ex file like this:

   def start(_type, args) do
     children = children([{Cowboy, scheme: :http, plug: MyServer, options: [port: 8082]}])

     opts = [strategy: :one_for_one, name: MyApp.Supervisor]
     Supervisor.start_link(children, opts)
   end

But the code I am working on is not an application, so I can’t do that.

My objective is to use the setup_all callback of ExUnit to do this, however I don’t know how to do it or if it is possible.

Questions

  1. How can I start a cowboy server in ExUnit?

Are you familiar with bypass? Use it directly if you can, but if you have some unique requirements that preclude that, then its source contains techniques you could customize.

2 Likes