Elixir port of Hegel, the reusable property-based testing engine.
Hegel splits property-based testing in two:
libhegel(shipped from hegel-rust) implements generation, shrinking, the example database, and scheduling, and exposes it all through a C ABI.- Each language frontend binds to that ABI and adds idiomatic generators plus test-framework integration.
This package is the Elixir frontend. The C ABI is bridged through a
Rustler NIF crate
(native/hegel_native) that statically links the engine — hegeltest-c,
pinned to 0.44.1 — and exposes the full call-driven surface
(hegel_context_new, hegel_settings_new, hegel_run_start, …) per
hegel.h.
Where the other frontends download prebuilt libhegel release assets
(e.g. @hegeldev/hegel (TypeScript) pins LIBHEGEL_VERSION), this crate
builds the engine from source via cargo.
Engine reference: https://hegel.dev/reference/libhegel
Prerequisites: Nix with flakes enabled and optionally
direnv (with nix-direnv).
direnv allow # or: nix develop
mix deps.get
mix testThe flake provides Elixir, Erlang/OTP, Rust (rustc/cargo for Rustler),
compilers, and OpenSSL — no system-wide installs needed.
Common tasks live in a root justfile, so local runs and CI invoke
the same recipes. Run them from inside the dev shell (direnv allow or
nix develop), or prefix each one with nix develop --command (the recipes do
not nest nix develop themselves):
| Command | What it does |
|---|---|
just |
list the available recipes |
just setup |
fetch Mix dependencies |
just format / just format-check |
format / check formatting |
just build |
build with warnings as errors |
just test |
run the test suite under the coverage gate (mix test --cover) |
just credo (or just lint) |
run Credo in strict mode |
just check |
format-check + build + credo + test (coverage-gated), then the NIF crate's checks |
just examples |
build and test every example project |
just docs |
build the Hex docs |
just audit |
audit dependencies |
just native::hegel_native::check |
Rust formatting check + cargo check + Clippy on the NIF crate |
just ci |
the full CI gauntlet (check, examples, docs, audit) |
The NIF crate keeps its own commands next to the crate in
native/hegel_native/justfile (format,
format-check, build, clippy, check), reached through just's module
system as native::hegel_native::….
test "addition is commutative" do
Hegel.test!(fn tc ->
x = Hegel.TestCase.draw!(tc, Hegel.Generators.integer())
y = Hegel.TestCase.draw!(tc, Hegel.Generators.integer())
assert x + y == y + x
end)
endHegel.test!/2 runs the body 100 times by default (override with
test_cases:); a failing case is shrunk to the minimal counterexample and
reported by raising Hegel.Failure. See Hegel for settings,
Hegel.Generators for generators, and Hegel.Generator for
map/2/filter/2/flat_map/2.
Sugar, in the StreamData spirit — use Hegel.Property imports the macros
and the generators:
defmodule MyTest do
use ExUnit.Case, async: true
use Hegel.Property
property "addition is commutative" do
check all x <- integer(), y <- integer() do
assert x + y == y + x
end
end
endTrailing keywords inside check all are Hegel.test!/2 settings:
property "bounded" do
check all x <- integer(min_value: 0), test_cases: 50 do
assert x >= 0
end
endThe test case is available as tc inside the body, so case-level features work
with the sugar too:
property "only even draws count" do
check all n <- integer(min_value: 0, max_value: 100) do
Hegel.TestCase.assume(tc, rem(n, 2) == 0)
assert rem(n, 2) == 0
end
endproperty/3 also takes an ExUnit context pattern, like test/2:
property "the cart from setup is empty", %{cart: cart} do
check all x <- integer() do
assert ShoppingCart.item_count(cart) == 0
end
endEvery run has a known seed — pass seed: yourself, or let the runner pick a
fresh one (with derandomize: true the engine derives it from the database
key). A failure carries that seed, the engine's base64 reproduction blob, and a
confirmation caveat when the failure did not reproduce deterministically:
error = assert_raise Hegel.Failure, fn -> Hegel.test!(failing_body) end
error.seed # => 14184399313335844545
error.blob # => "AQIDBA..." (base64 reproduction blob)
error.caveat # => nil when the failure reproduced deterministically
error.origins
# Re-run the exact sequence:
Hegel.test!(failing_body, seed: error.seed)
# Or replay the blob itself (no seed, never touches the database):
Hegel.replay_blob!(failing_body, error.blob)Hegel.replay_blob!/3 returns :ok once the blob no longer reproduces a
failure and raises Hegel.Failure while it still does. A blob only reproduces
in the engine version that produced it (Hegel.libhegel_version/0). The
failure message shows the drawn values — each check all binding by name
and each explicit draw! as draw_N — so the minimal counterexample is
visible without extra note/2 calls. It ends with
seed: N (re-run with seed: N). With print_blob: true (the default) the
blob is also printed to stderr. The example database (.hegel/examples/ by
default) additionally replays stored counterexamples first, so a failure
reproduces even without the seed.
Beyond plain properties:
Hegel.StateMachine— stateful testing with rules, preconditions, invariants, and concurrent workers; failures carry executed steps.Hegel.Pool— variable pools: engine-drawn references to generated values.Hegel.TestCase.target!/3— coverage-style targeting ("higher is more interesting").Hegel.Printer— engine pretty-printer documents for rendering values.Hegel.Generators.recursive/3— engine-steered recursive values.Hegel.TestCase.event!/2/event_value!/3— end-of-run statistics (withshow_statistics: true).
Defaults for any Hegel.test!/2 setting can be set once, in mix.exs:
def project do
[
app: :my_app,
# ...
hegel: [
database: true, database_path: ".hegel",
test_cases: 200,
suppress_health_checks: [:too_slow]
]
]
endor in config/config.exs:
config :hegel, database: false, test_cases: 200Per-call options always win, and mix.exs wins over config :hegel:
config :hegel < mix.exs `hegel:` < per-call options
Every setting in Hegel.Config.setting_keys/0 can be defaulted this way;
:test_location cannot, because the check all macro fills it from the call
site.
Two settings control the example database, each with a default:
hegel: [database: true, database_path: ".hegel/examples"]:database is a boolean (true by default) and :database_path says where the
examples live (".hegel/examples" by default). A relative path is resolved
against the project root — so it does not depend on the directory your suite is
started from — and created on demand; an absolute path is used as given. Set
database: false for hermetic runs and CI. Add .hegel/ to your .gitignore.
examples/ holds five standalone Mix projects, ordered from the simplest
property to a full supervised OTP application. Each one depends on this
library by relative path ({:hegel, path: "../.."}), so it builds and tests
against your working copy:
| Example | App | What it demonstrates |
|---|---|---|
01_basics |
pure shopping cart | property / check all, core generators, assume, note, plain Hegel.test!/2 |
02_custom_generators |
validated domain types | reusable domain generators (composite, record, tuple, weighted, recursive), round-trip and idempotence properties, shrink assertions |
03_stateful |
GenServer bounded queue |
Hegel.StateMachine: model vs system, preconditions, invariants, cleanup, shrink-to-minimal operation sequences |
04_concurrency |
parallel map + ETS counter | testing concurrent code, Hegel.Pool, statistics (event!), target!, state machines with concurrent workers |
05_link_shortener |
supervised link shortener | testing a real Application: unit + property + state machine tests, seed:/derandomize/database settings, reproducing a reported failure |
Start with examples/01_basics/README.md; each example's README explains the
app, the testing ideas, and how to run it.
cd examples/01_basics
mix deps.get
mix testTo build and test all five in one go (from the dev shell):
nix develop --command just examples
# or, from inside the dev shell:
just examples