Closed robmoore-i closed 4 years ago
Hi,
OCaml is not very flexible on the typing and it makes it complicated to implement an "assert_equal" that can print numbers, strings and other structures. This is a limitation of the language, everything has to be typed correctly.
You can find an introduction to error reporting here: https://gildor478.github.io/ounit/ounit2/index.html#error-reporting
You can find some options in assert_equal
assert_equal : [...] ?printer:('a -> string) -> ?pp_diff:[....]-> ?msg:string -> [...]
Here is an example of the output with ~msg and ~printer:
==============================================================================
Error: OUnit:6:OtherTests:0:TestFakeHTML.
File "/home/gildor/programmation/ounit/_build/default/test/oUnit-OUnit-pico#01.log", line 69, characters 1-1:
Error: OUnit:6:OtherTests:0:TestFakeHTML (in the log).
File "test/testOtherTests.ml", line 90, characters 1-1:
Error: OUnit:6:OtherTests:0:TestFakeHTML (in the code).
Number of test case in junit.xml.
expected: 7 but got: 6
------------------------------------------------------------------------------
Ok, cheers. I'll close this since it seems to be a constraint from the language, so something more complex (I've tried to be useful and make a suggestion anyway) would be more difficult.
Given that constraint from ocaml, I wonder if assertions would be easier to do with a hamcrest-style (http://hamcrest.org/JavaHamcrest/tutorial), since it is further towards the composable end of the test-framework design spectrum. It'd use the type signature: assertThat : T -> T Matcher.t -> unit
and you'd call it with something like assertThat "foo" (equalTo (myFunction "bar"))
.
Anyway, thanks for clarifying and giving additional info on how to improve the failure messages. Best wishes,
Rob
Here's an example of the output of a failing test:
It doesn't even show expected vs actual - it just says "not equal" which I find quite disappointing. This lack of information about the failure hugely reduces the usefulness of the tests, and inclines me to add my own print statements into the tests. In general, I'd like the test framework to provide descriptive failure messages for common sense assertions like 'assert_equal'.
I'm open to learning that most OCaml programmers don't care, but from the perspective of someone who has been embedded in TDD for a long time within other languages, I would quite like this feature, and I suspect it's quite a low-hanging fruit. I'm not raising this issue to throw rocks at OUnit (which I was so happy to come across!), but just to let the maintainers know that this is a feature that I and others new to the language would certainly appreciate.
Best wishes, stay well, and many thanks for creating this package, Rob
Edit note: Here is an example of the code I write to get the information I want from my test failures:
Which will output: