# Consumer driven contract testing in Elixir

**URL:** <https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134>\
**Category:** Discussions\
**Tags:** microservice\
**Created:** [November 30, 2022, 5:29pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134 "2022-11-30T17:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![csadewa](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/csadewa/32/24535_2.png) [@csadewa](https://forum.elixirforum.com/u/csadewa)\
**Post date:** [November 30, 2022, 5:29pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/1 "2022-11-30T17:29:42Z")

</div>

Hi all, i am currently researching consumer driven contract testing (in particular [pact.io](http://pact.io)) in hope that it would make better development collaboration for frontend - backend or service to service in microservice architecture. However, even after reading through blogs and docs regarding [pact.io](http://pact.io), i am still struggling to fully understand it’s benefit. Could you share your experience?:

1. Worked with consumer driven contract testing before? do you find it helpful?
2. Does consumer driven contract ensure no breaking change? Does it improve no-breaking possibility much compared to using strongly typed schema (GraphQL/gRPC) combined with schema evolution?
3. Does consumer driven contract enable better autonomy? Enable frontend to be developed separately from backend?
4. Does there is consumer driven contract library in elixir, especially one that follow pact specification?

---

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [December 15, 2022, 1:39pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/2 "2022-12-15T13:39:40Z")

</div>

I did work in a team that had multiple services in different technologies and there was a push to do consumer-driven contract testing with Pact and it got implemented for Ruby services. At that point the tooling for Elixir was not that great so we didn’t do it in the end, but I think the biggest reason was that it was always a low priority item for us - we did have an extensive set of tests for the API, so I saw little to no value (but significant effort) in adding Pact tests.

Instead of Pact I’d suggest writing unit tests (note that here the unit of the test is a behavior/endpoint, not a module/function) this way:

```elixir
describe "GET /api/foobars" do
  test "requires authorization" do
    assert %{status: 401, body: body} = get(nil, "/api/foobars")
    assert %{"error" => "Unauthorized"} = body   
  end

  test "returns a list of foobars" do
    assert %{"foobars" => []} = get!(api_key, "/api/foobars")
  end
end

```

The idea here is to write your tests as if they were written using an external tool (you’ll need to add some helpers for making HTTP requests), but run them inside your normal test suite (`mix test`) to leverage all the ExUnit goodies and keep them fast.

---

<div class="post-metadata">

**Author:** ![D4no0](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/32/33624_2.png) [@D4no0](https://forum.elixirforum.com/u/D4no0)\
**Post date:** [December 15, 2022, 2:06pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/3 "2022-12-15T14:06:19Z")

</div>

I don’t know if you can call these unit tests, I usually refer to these kind of tests as integration tests, as they test the system interactions as a whole, not just isolated parts.

---

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [December 15, 2022, 2:20pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/4 "2022-12-15T14:20:41Z")

</div>

I certainly would call them unit tests, but then we get to the point of what is the unit of a test? I think the common approach of treating a class, module or a function a unit of a test is not really useful. For me the unit of a test is a single feature/behaviour.

In my books, an integration test is one where multiple independently-deployable services are involved.

---

<div class="post-metadata">

**Author:** ![csadewa](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/csadewa/32/24535_2.png) [@csadewa](https://forum.elixirforum.com/u/csadewa)\
**Post date:** [December 16, 2022, 4:12am UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/5 "2022-12-16T04:12:08Z")

</div>

@stefanchrobot thanks for detailed reply! Do you know impact of Pact on the Ruby services? Does using Pact on these services manifest significant positive impact (in term of ability to change API/enable autonomy for Frontend/Backend) that outweigh its implementation and maintenance cost?

---

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [December 20, 2022, 1:28pm UTC](https://forum.elixirforum.com/t/consumer-driven-contract-testing-in-elixir/52134/6 "2022-12-20T13:28:58Z")

</div>

Not sure, unfortunately. This initiative was pushed by the QA team. It did increase test coverage of the APIs, but from my perspective, I’d rather invest more in those end-to-end tests.
