package hegel

Build not yet checked
  1. Overview
  2. Docs
Hegel property-based testing library for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

hegel-0.28.1-opam.tar.gz
md5=594516a158a53be319680141ac849057
sha512=58a878a38296d3e993b7e5947de870fe96074a89a0b3be7eb71f1f61a02aa739b1ef8647277d65a164acc45dbad7176e0a62cc2fbd7821ce2ed7e4025fd09224

Description

A property-based testing library that communicates with the Hegel engine to enable universal property-based testing from OCaml.

Added to opam-repository:

README

Hegel for OCaml

Hegel is a property-based testing library for OCaml based on Hypothesis, and runs the native libhegel backend in-process.

Install Hegel

opam install hegel

The Hegel version in opam sometimes lags behind the version in GitHub. To pin the version in GitHub (the frontend lives in the hegeldev/hegel monorepo, under frontends/ocaml):

opam pin add --subpath frontends/ocaml hegel "git+https://github.com/hegeldev/hegel.git"

Hegel calls the native libhegel shared library and locates it automatically at runtime. It looks for the following in order:

  1. $HEGEL_LIBHEGEL_PATH, an explicit path to the library (or a directory containing it)
  2. a copy bundled into the installed package: every opam release ships the matching engine, so opam install hegel needs nothing else
  3. the in-tree build of a hegeldev/hegel checkout: target/release/libhegel_c.<ext> (then target/debug/) under $CARGO_TARGET_DIR, or under the nearest directory above the current working directory that has one — what cargo build --release -p hegeltest-c produces.

A git pin ships no engine, so set HEGEL_LIBHEGEL_PATH to a library from the matching libhegel release (the libhegel/<version> tags) or build one in a checkout.

Hegel for OCaml supports Linux (amd64/arm64) and macOS (Apple Silicon). macOS amd64 (Intel) has no published libhegel artifact, so on that platform set HEGEL_LIBHEGEL_PATH to a locally built libhegel_c.dylib.

Quick start

Hegel works with whatever test framework your project already uses. The examples below use Alcotest.

Add hegel and alcotest to your dune test stanza:

(test
 (name my_tests)
 (libraries hegel alcotest)
 (preprocess (pps ppx_hegel_test)))

Write a property test using let%hegel_test:

open Hegel

let%hegel_test commutative_addition tc =
  let a = draw tc (integers ~min_value:(-1000) ~max_value:1000 ()) in
  let b = draw tc (integers ~min_value:(-1000) ~max_value:1000 ()) in
  assert (a + b = b + a)
;;

let () =
  Alcotest.run
    "my_tests"
    [ "properties", [ Alcotest.test_case "commutative addition" `Quick commutative_addition ] ]
;;

Run dune runtest. Hegel generates up to 100 random input pairs and reports the minimal counterexample if it finds one. When a test fails, Hegel prints a report naming each value you drew from the failing case (a = …, b = …, named after the let binding) and a copy-pasteable line to replay it. Alcotest reports the test as failed and exits non-zero.

To override the default settings, attach a [@@settings ...] attribute:

let%hegel_test commutative_addition tc =
  let a = draw tc (integers ()) in
  let b = draw tc (integers ()) in
  assert (a + b = b + a)
[@@settings Settings.create ~test_cases:500 ()]
;;

For a full walkthrough, see the docs.

Development

The frontend is developed in the hegeldev/hegel monorepo and built and tested against the engine in libhegel/:

just ocaml::setup   # install the opam dependencies (needs opam)
just ocaml::check   # lint + docs + tests with 100% coverage, against the in-tree libhegel
just ocaml::test    # tests only

See AGENTS.md for the architecture and ../../CONTRIBUTING.md for how to contribute.

Dependencies (12)

  1. ppxlib >= "0.28"
  2. ppx_js_style >= "v0.17"
  3. ocplib-endian >= "1.2"
  4. dune-site >= "3.16"
  5. cmdliner >= "1.1.0"
  6. ipaddr >= "5.0"
  7. ctypes-foreign >= "0.20"
  8. ctypes >= "0.20"
  9. sexplib0 >= "v0.17"
  10. ocaml >= "5.1"
  11. cppo build
  12. dune >= "3.16"

Dev Dependencies (7)

  1. odoc with-doc
  2. ocamlformat with-dev-setup & = "0.29.0"
  3. bisect_ppx with-dev-setup & >= "2.8"
  4. re with-test & >= "1.12"
  5. ppx_expect with-test & >= "v0.17"
  6. core_unix with-test & >= "v0.17"
  7. alcotest with-test & >= "1.7"

Optional Dependencies (4)

  1. ppx_template
  2. concurrent
  3. async
  4. core >= "v0.17"

Used by (3)

  1. opentelemetry >= "0.92"
  2. ppx_hegel_generator >= "0.28.1"
  3. ppx_hegel_test >= "0.28.1"

Conflicts

None

Build status

Results of building hegel with each compiler on check.ci.ocaml.org, run of against opam-repository 6e2873f. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.27.2 n/a n/a good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure