Skip to content

Add new Alcotest.suite function to make writing tests easier - #393

Closed
yawaramin wants to merge 8 commits into
mirage:mainfrom
yawaramin:story/registration
Closed

yawaramin wants to merge 8 commits into
mirage:mainfrom
yawaramin:story/registration

Conversation

@yawaramin

Copy link
Copy Markdown

Fixes #126.

Example usage:

{[
let () =

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ocamlformat settings don't like the formatting of this example. IMHO, the format settings are incorrect :-)

And update Lwt suite example.
@samoht

samoht commented Sep 20, 2023 •

Copy link
Copy Markdown
Member

Many thanks for your patch - two remarks:

  • I really like using functions in the API instead of exposing concrete types. It's probably too late to make those types abstract as it will break every existing code, but let's start by exposing proper constructors first and see how adoption goes.
  • I am not convinced by inlining all the test cases in "heavy" functions. Would you mind doing the minimum possible changes in the examples to keep the test functions separate (when they are big enough like they are right now) and avoid the heavyweight begin fun () -> ... end forms when possible?

@yawaramin

yawaramin commented Sep 20, 2023 •

Copy link
Copy Markdown
Author

Hi @samoht thanks for the review. Regarding your points,

  • Agreed
  • Do you mean like e.g.:
let string_case_lower_case () =
  Alcotest.(check string) "same string" "hello!" (To_test.lowercase "hELLO!")

let string_case_capitalization () =
  Alcotest.(check string) "same string" "World." (To_test.capitalize "world.")

let string_concat_string_mashing () =
  Alcotest.(check string) "same string" "foobar" (To_test.str_concat ["foo"; "bar"])

let list_concat_list_mashing () =
  Alcotest.(check (list int)) "same lists" [1; 2; 3] (To_test.list_concat [1] [2; 3])

let () =
  Alcotest.suite "Utils" begin fun group ->
    group "string-case" begin fun case ->
      case "Lower case" string_case_lower_case;
      case "Capitalization" string_case_capitalization;
    end;

    group "string-concat" begin fun case ->
      case "String mashing" string_concat_string_mashing;
    end;

    group "list-concat" begin fun case ->
      case ~speed:`Slow "List mashing" list_concat_list_mashing;
    end;
  end

Wouldn't this bring us back to the current situation where the test implementations and registrations are separate and can get out of sync? E.g. if I don't have an empty .mli and delete one of the cases, I could forget to delete its implementation.

EDIT: also this means we need to decide on names for the test case functions, and they have to be reasonable and meaningful names. We can't just do e.g. let f1 () = .... So we are very likely saving quite a bit of naming decision-making and verbosity by just inlining the lambdas as fun () -> .... I think that's a win for users.

@lessp

lessp commented Jun 29, 2024

Copy link
Copy Markdown

Love this! Can we expect this to go in?

@samoht

samoht commented Mar 13, 2025

Copy link
Copy Markdown
Member

I'm looking at this again - I think there are 2 things in that patch that needs to be reviewed separately

  1. adding top-level types and function for group of tests. This is uncontroversial a good change and I'll be happy to merge this (given with find the right name, for instance group_testlist is a bit weird :p)
  2. changing the default tests are registered -- I'm a bit warry of this one without more user feedback. I'm ok to suggest a new way to do things but I would like to avoid breaking existing tests. Once users start to adopt this new way (and give us some feedback) we could consider changing the way we write examples, etc.

Do you think that makes sense?

@samoht

samoht commented Mar 13, 2025

Copy link
Copy Markdown
Member

In particular, I'd like to make sure we take the feedback from #294 into account

@samoht

samoht commented Nov 2, 2026

Copy link
Copy Markdown
Member

@yawaramin could you split out the uncontroversial part (top-level types and functions for groups of tests) into its own PR? Ideally without breaking the API, keeping the existing test_case and run signatures unchanged. Happy to review that part first.

@samoht

samoht commented Nov 2, 2026

Copy link
Copy Markdown
Member

(I'm happy to draft something in a separate PR if you don't have time to do so)

@yawaramin

Copy link
Copy Markdown
Author

Hi, I am OK with whatever approach you decide here. My thinking on this has shifted and nowadays I use dune's diff-and-promote workflow for testing.

@samoht

samoht commented Nov 5, 2026

Copy link
Copy Markdown
Member

The "easy" parts are in #468. Happy to revisit the rest later if there is still interest.

@samoht samoht closed this Nov 5, 2026
samoht added a commit to samoht/opam-repository that referenced this pull request Nov 10, 2026
CHANGES:

- Experimental: with `--junit` or `ALCOTEST_JUNIT=1`, write a JUnit XML report
  of each run to `junit/<name>-<time>-<id>.xml` of the log directory, with the
  `file` and `line` of each test, or of its failure (mirage/alcotest#449, mirage/alcotest#476, fixes mirage/alcotest#130,
  @samoht, report by @pbiggar)
- Record the source file that calls `run`, and write it as the `file` of the
  `<testsuite>` of the JUnit report (mirage/alcotest#449, @samoht)
- `Platform.S` now requires `timestamp` and `write_file` (mirage/alcotest#449, @samoht)
- Under dune, keep the output of tests in `_build/_tests` of the workspace,
  where dune does not delete it, rather than in `_build/default`. With mirage/alcotest#472, each
  `dune test` adds its runs to `_build/_tests/run/<name>/<time>-<id>`, which gives
  a local history of test results (mirage/alcotest#471, @samoht)
- Fix `alcotest-mirage`, which failed with `Assertion failed` in every run
  without `--verbose` since 1.5.0 (mirage/alcotest#473, @samoht)
- Keep the logs of each run in `run/<name>/<time>-<id>`, where `<name>` is the
  name given to `run`, so that the runs of each test executable are listed in
  order. `latest` still points to the last run (mirage/alcotest#472, @samoht)
- Fix the link named after each test executable, which did not always point to
  its last run. It is replaced by the `run/<name>` folder (mirage/alcotest#472, @samoht)
- Fix a crash when the name of a `suite` or `group` is longer than a file name
  can be (mirage/alcotest#472, @samoht)
- Fix log files hidden when the name of a `suite` or `group` starts with a dot
  (mirage/alcotest#472, @samoht)
- Custom platforms: `prepare_log_trap` takes the name given to `run` and returns
  the folder of the run (mirage/alcotest#472, @samoht)
- Fix creating a log directory written with `/` on Windows, as in `-o a/b`, which
  failed with `ENOENT` (mirage/alcotest#470, @samoht)
- Add `test ?tags name f` and `suite name cases`, to write `suite "a" [ test "b" f ]`
  instead of `("a", [ test_case "b" `Quick f ])`. `tags` defaults to `` `Always ``
  (mirage/alcotest#468, @samoht, combinators suggested by @yawaramin in mirage/alcotest#393)
- Add `?tags` to `group`: its cases only run when these tags hold as well as
  their own (mirage/alcotest#468, @samoht, API from mirage/alcotest#294 by @craigfe)
- Add `group name cases`, a `test_case` that nests `cases` to any depth. They
  run in place, are reported as `parent.name` and are selected with
  `test '^parent\.name'` (mirage/alcotest#453, fixes mirage/alcotest#275, @samoht, API from mirage/alcotest#294 by @craigfe,
  dotted names suggested by @mjambon)
- Show the file and line of every failing test, not only of a failing `check`.
  A failure in a helper points to the line of the test that calls it.
  `ALCOTEST_SOURCE_CODE_POSITION` still turns this off (mirage/alcotest#451, @samoht, based on
  `B0_testing` by @dbuenzli and the b0 programmers)
- Print the message of a failing check once: `ASSERT` now only shows the checks
  that passed (mirage/alcotest#465, @samoht)
- Add wasm_of_ocaml support (mirage/alcotest#461, mirage/alcotest#436, mirage/alcotest#437, @hhugo, @samoht and @mefyl)
- Remove code made redundant by OCaml 4.14 (mirage/alcotest#460, @samoht)
- Close a failed test's log file when printing it raises, instead of leaking the
  file descriptor (mirage/alcotest#460, @samoht)
- Remove the `alcotest-js` package, which had no code and only added dependencies.
  To test with js_of_ocaml, depend on `js_of_ocaml-compiler` directly (mirage/alcotest#463, @samoht)
- Require OCaml 4.14 and replace `uutf` with the stdlib UTF-8 decoder. Test names
  with invalid UTF-8 can get a different log file name (mirage/alcotest#456, @samoht)
- Print valid UTF-8 as is in `string` failures, when the output supports it
  (mirage/alcotest#456, fixes mirage/alcotest#408, @samoht, report by @feverdreme, suggestion by @lindig)
- Add `iarray`, a testable for `Iarray.t`, available from OCaml 5.4
  (mirage/alcotest#455, fixes mirage/alcotest#434, @samoht, report by @JanBeh)
- Fix UTF-8 locale detection, which never matched since 1.8.0, so `Fmt.if_utf_8`
  printers always took the non-UTF-8 branch. `LC_ALL` now overrides `LC_CTYPE` and
  `LANG`, as in POSIX, and `UTF8` also counts (mirage/alcotest#457, @samoht)
- Recognise `Alcotest.skip` and failed checks in `alcotest-async` tests: Async
  wrapped the exceptions, so a skip counted as a failure (mirage/alcotest#278, @vbmithr)
- Make `alcotest-async` unavailable on Windows, so that ocaml-ci tests the other
  packages there (mirage/alcotest#454, @samoht, suggested by @mtelvers)
- Remove stdlib-shims and ocaml-syntax-shims (mirage/alcotest#452, @copy)
- Update ocamlformat to 0.29.0 (mirage/alcotest#450, @samoht)
- Fix and modernise CI (mirage/alcotest#445, mirage/alcotest#446, mirage/alcotest#436, mirage/alcotest#407, @samoht, @hhugo and @MisterDA,
  report by @edwintorok and @ngorogiannis)
- Add `uchar`, a testable for `Uchar.t` (mirage/alcotest#435, @emengd, review by @dinosaure)
- Fix the `success` count in `--json` output, which included failures
  (mirage/alcotest#432, fixes mirage/alcotest#431, @1zumiSagiri, review by @MisterDA)
- Add conditions on tests: `test_case` and test tuples accept `` `Tag ``,
  `` `And ``, `` `Or ``, `` `Not `` and `` `Always ``, also as `Tag` combinators, which
  hold in a context set by a new `--tags` option or `ALCOTEST_TAGS`, as with Go
  build tags. A new `tags` subcommand lists them. `speed_level` is now an alias
  of the new `tag` type, which breaks code matching on it exhaustively or
  restating it as `` [ `Quick | `Slow ] ``
  (mirage/alcotest#448, fixes mirage/alcotest#124, mirage/alcotest#151 and mirage/alcotest#316, @samoht, report by @hannesm and @arvidj)
- Preserve absolute log directories: `mkdir_p` turned `/foo` into `//foo`, which
  fails with `EROFS` on Cygwin (mirage/alcotest#405, fixes mirage/alcotest#404, @bbonf, review by @MisterDA
  and @samoht)
- Allow callers to override `and_exit` and `record_backtrace` in the `test`
  subcommand (mirage/alcotest#439, fixes mirage/alcotest#438, @edwintorok)
- Keep `Unix.stdout` and `Unix.stderr` valid during tests on Windows
  (mirage/alcotest#444, fixes mirage/alcotest#443, @ngorogiannis)
- Test `alcotest-async` exception handling (mirage/alcotest#279, @craigfe)
- Add an `alcotest-async` example (mirage/alcotest#458, mirage/alcotest#421, @dannywillems)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review how tests are registered

3 participants