mirage / alcotest

A lightweight and colourful test framework
ISC License
451 stars 80 forks source link

Provide a PPX to complement the Alcotest API #256

Open craigfe opened 4 years ago

craigfe commented 4 years ago

A lot of the Alcotest API is quite conducive to code generation. For instance, the structure of a suite could fairly easily be inferred from the structure of modules and functions in the current file:

module%test Alpha = struct
  let%test first () = (* ... *)

  let%test second () = (* ... *)
end

module%test Beta = Beta (* tests in another file *)

(* Generates the following footer code: *)

let () =
  Alcotest.run "<filename>"
    [
      ( "Alpha",
        [
          Alcotest.test_case "first" `Quick Alpha.first;
          Alcotest.test_case "second" `Quick Alpha.second;
        ] );
      ("Beta", Beta.suite);
    ]

This allows some flexibility in structuring the tests (they can be split across multiple files / modules), while getting rid of the test registration boilerplate (even after writing a lot of these, I still never remember how to write them properly...). We could support an option for registering tests in top-level structure items (for small test suites), and something like let%test_lwt for using Alcotest_lwt instead.

Other possible candidates for PPX include:

craigfe commented 4 years ago

I've done some experimenting with this idea in a personal project (here). Not much there yet, but I'll keep adding features and report back.

The biggest win would be to extract parts of ppx_irmin to a common library that can be shared here, since the implementations of [@@deriving] and [%check] outlined above would be almost identical. (At the same time, we could add combinators for variants and records to Alcotest.) This would be the beginning of the great dynamic type consolidation :wink:

ELLIOTTCABLE commented 1 year ago

@craigfe would you consider that completely dead? because the heavy syntax is still one of the biggest barriers to non-inline testing in OCaml; it'd be great to have a succinct, fluid syntax for Alcotest or OUnit …

yawaramin commented 1 year ago

I've suggested a design for a succinct test writing syntax that I hope will be less confusing: https://github.com/mirage/alcotest/issues/126#issuecomment-1722621039