Literate test matchers for ExUnit


Keywords
elixir, elixir-lang, exunit, exunit-assertions, rspec-matchers, testing
License
MIT

Documentation

Machete

Build Status Docs Hex.pm

Machete provides ergonomic match operators to help make your ExUnit tests more literate

The easiest way to explain Machete is to show it in action:

defmodule ExampleTest do
  use ExUnit.Case
  use Machete

  test "example test" do
    response = %{
      id: 1,
      name: "Moe Fonebone",
      is_admin: false,
      created_at: DateTime.utc_now()
    }

    assert response ~> %{
      id: integer(positive: true),
      name: string(),
      is_admin: false,
      created_at: datetime(roughly: :now, time_zone: :utc)
    }
  end
end

At its heart, Machete provides the following two things:

  • A new ~> operator (the 'squiggle arrow') that does flexible matching of its left operator with its right operator
  • A set of parametric matchers such as string() or integer() which can match against general types. A comprehensive list of Machete's built-in matchers is available in the Machete documentation

These building blocks let you define test expectations that can match data against any combination of literals, variables, or parametrically defined matchers

When your matches fail, Machete provides useful error messages in ExUnit that point you directly at any failing matches using jq syntax

Installation

Machete is available in Hex, and can be installed by adding machete to your list of dependencies in mix.exs:

def deps do
  [
    {:machete, "~> 0.2.8"}
  ]
end

Documentation is published on HexDocs

License

MIT