View Source ExampleTest (ExampleTest v0.0.1)

A utility for defining eminently readable example-based tests.

Example tests look like this:

example_test "grants free shipping based on the marketing site's stated policy",
             | spending_by_category          | coupon      | gets_free_shipping? |
             | %{shoes: 19_99, pants: 29_99} |             | false               |
             | %{shoes: 59_99, pants: 49_99} |             | true                |
             | %{socks: 10_99}               |             | true                |
             | %{pants: 1_99}                | "FREE_SHIP" | true                |
               spending_by_category: spending_by_category,
               coupon: coupon,
               gets_free_shipping?: gets_free_shipping?
             } do
  shipping_cost = ShippingCalculator.calculate_shipping(spending_by_category, coupon)
  free_shipping? = shipping_cost == 0
  assert free_shipping? == gets_free_shipping?

Why example testing?

Example testing reduces toil associated with writing tests that cover a wide variety of different cases. It also localizes the test logic into a single place, so that at a glance you can see how a number of different factors affect the behavior of the system under test.

As a bonus, a table of examples (with their expected results) often matches how the business communicates the requirements of a system, both internally and to customers—for instance, in a table describing shipping costs based on how much a customer spends, where they're located, whether they've bought a promotional product, etc. This means example tests can often be initially created by pulling directly from a requirements document that your product folks provided, and the product folks can later read the tests (or at least the examples table) if they want to verify the behavior of the system.



Link to this macro

example_test(test_name, examples, context_ast \\ %{}, blocks)

View Source (macro)

Defines tests that use your example data.

Use it like:

example_test "works as expected", examples, %{value: from_context, expected_result: expected_result} do
  assert something(from_context) == expected_result
Link to this function

parse_examples(table, opts \\ [])

View Source
@spec parse_examples(String.t(), Keyword.t()) :: [map()]