Logging while/during (not after) testing with Alcotest

I have some unit tests built using Alcotest that hang on some runs, but run fine on other occasions. Unfortunately, if the test hangs, it does not fail, and thus I don’t get any feedback as to what is going on. It seems like time-outs with Lwt.pick are not working as expected (the rest of the code uses Lwt as well) at least for this test, but I’m finding it hard to explore why that’s the case without logging. I’ve considered writing directly to a file instead of to stdout/stderr, but I’d rather not make a change like that to my code if I don’t need to. Is there any way to get stdout/stderr logging while a test is running?

Hi @rosalogia,

The main Alcotest.run function has a few arguments (depending on your version of Alcotest), see Alcotest · alcotest 1.5.0 · OCaml Packages. I think maybe setting verbose and show_errors to true might be helpful :))

Hi @patricoferris! Thanks for your response. I have messed with the verbose and show_error arguments. Unfortunately, both of these seem to only affect what is shown (or where messages go) after a run terminates. Neither of them seem to affect whether or not any messages are shown during a run, which is what I’m looking for. My call looks like this:

let () =
  Lwt_main.run
  @@ Alcotest_lwt.run ~verbose:true ~show_errors:true "Tests"
       [
         ( "suite 1",
           [ ... (* tests go here *)
           ] );
       ]

in case you might be able to confirm whether or not I’m using the options correctly.

1 Like

Oh right I see, are you using the Logs library by any chance ? In which case maybe the reporter needs set:

let () =
  Logs.set_level (Some Debug);
  Logs.set_reporter (Logs_fmt.reporter ())

let () =
  Lwt_main.run
  @@ Alcotest_lwt.run ~verbose:true "Tests"
       [
         ( "suite 1",
           [
             Alcotest_lwt.test_case "a" `Quick (fun _ () -> Lwt.return ());
             Alcotest_lwt.test_case "b" `Quick (fun _ () ->
                 Logs.info (fun f -> f "Some log 1");
                 Logs.info (fun f -> f "Some log 2");
                 fst @@ Lwt.wait ());
           ] );
       ]

This works for me even though test case b never returns (the output is a little wonky looking though).

1 Like

I am not using Logs, but maybe now is the right time to start. Thanks for your help!