1
votes

We have a couple of REST APIs that need to be tested. APIs are owned by some other team. Currently, we use Node.js with Ava to test our APIs.

We have recently started exploring Elixir as our scripting language. We would like to experiment with Elixir to test REST API. The question here is - how to test external web services/REST API with Elixir?

Every Google search around Elixir testing refers back to ExUnit which is basically for unit testing of Elixir apps. We don't have any app written in Elixir or in Phoenix.

All we want to do is to test API end-to-end. How to do that with Elixir? Which libraries to use? I know I can make network calls from my tests written in ExUnit and verify the API behavior, but not sure if it is the right way.

NOTE: We already have JMeter in place for load testing of API but we wish to keep functional testing separate from load testing due to complex workflows involved with API.

1
You are not supposed to test 3rd party API. You are supposed to believe it’s working properly and mock it in your internal tests. That said, I doubt there is anything provided to accomplish the wrong task in Elixir macrosystem.Aleksei Matiushkin
@mudasobwa, this is not a 3rd party API. It is just that QA and dev teams are different. QA team needs to carry out automated functional tests for API. Test code and Source code are owned by different teams in different repositories.Harshal Patil
Then your description of the task is a bit misleading. It does not matter how many repos are involved, assert(conn(:get, url)) should be fine as described here: robots.thoughtbot.com/…Aleksei Matiushkin

1 Answers

3
votes

I think what you descried in the answer is the right way.

You can use ExUnit as the test running and reporting framework. In ExUnit you can do whatever you want, make network calls, even parse the DOM. For testing a REST API you can use HTTPoision and assert on status code and response body.

Create a new mix project

mix new api_test

Add HTTPoison dependency to mix.exs

...
  defp deps do
    [
      {:httpoison, "~> 1.6"}
    ]
  end
...

Run mix deps.get to get HTTPoison installed.

Add a test in test/api_test_test.exs

defmodule ApiTestTest do
  use ExUnit.Case
  doctest ApiTest

  test "API alive" do
      resp = HTTPoison.get!("https://api.github.com")
      assert resp.status_code == 200
  end
end

Run the tests with mix test from the project root.