Questions tagged [ounit]

OUnit is a unit test framework for OCaml loosely based on HUnit, a unit testing framework for Haskell.

With OUnit, as with JUnit, you can easily create tests, name them, group them into suites, and execute them, with the framework checking the results automatically.

The basic principle of a test suite is to have a file test.ml which will contain the tests, and an OCaml module under test, named foo.ml.

File foo.ml:

(* The functions we wish to test *)
let unity x = x;;
let funix ()= 0;;
let fgeneric () = failwith "Not implemented";;

The main point of a test is to check that the function under test has the expected behavior. You check the behavior using assert functions. The most simple one is OUnit2.assert_equal. This function compares the result of the function with an expected result.

The most useful functions are:

  • OUnit2.assert_equal the basic assert function
  • OUnit2.(>:::) to define a list of tests
  • OUnit2.(>::) to name a test
  • OUnit2.run_test_tt_main to run the test suite you define
  • OUnit2.bracket_tmpfile that create a temporary filename.
  • OUnit2.bracket_tmpdir that create a temporary directory.

File test.ml:

open OUnit2;;

let test1 test_ctxt = assert_equal "x" (Foo.unity "x");;

let test2 test_ctxt = assert_equal 100 (Foo.unity 100);;

(* Name the test cases and group them together *)
let suite =
  "suite">:::
    ["test1">:: test1;
     "test2">:: test2]
;;

let () =
  run_test_tt_main suite
;;

And compile the module

$ ocamlfind ocamlc -o test -package oUnit -linkpkg -g foo.ml test.ml

An executable named "test" will be created. When run it produces the following output:

$ ./tests
..
Ran: 2 tests in: 0.00 Seconds
OK

When using OUnit2.run_test_tt_main, a non zero exit code signals that the test suite was not successful.

(Source)

18 questions
6
votes
1 answer

Running OUnit tests using dune

I'm having difficulties running OUnit tests, mostly because I'm new to both dune and OUnit. dune complains when I run dune runtest: File "test/dune", line 4, characters 13-14: Error: Library "f" not found. Hint: try: dune external-lib-deps --missing…
Flux
  • 9,805
  • 5
  • 46
  • 92
4
votes
1 answer

The correct way to write unit tests for a module in OCaml

I have a given interface specification in the module.mli file. I have to write its implementation in the module.ml file. module.mli provides an abstract type type abstract_type I'm using OUnit to create the tests. I need to use the type's…
marmistrz
  • 5,974
  • 10
  • 42
  • 94
4
votes
1 answer

Use OUnit module in OCaml - Unbound module OUnit error

I'm trying to use OUnit with OCaml. The unit code source (unit.ml) is as follows: open OUnit let empty_list = [] let list_a = [1;2;3] let test_list_length _ = assert_equal 1 (List.length empty_list); assert_equal 3 (List.length list_a) (*…
prosseek
  • 182,215
  • 215
  • 566
  • 871
2
votes
1 answer

Is there any usage of `test_ctxt` in oUnit

I'm playing around with oUnit2 and I'm wondering: is there any usage for the test_ctxt parameter, as here: let test1 test_ctxt = assert_equal "x" (Foo.unity "x");; Is seems superfluous to me. Is there any way to omit it while defining tests as…
marmistrz
  • 5,974
  • 10
  • 42
  • 94
2
votes
0 answers

How to configure ocaml ounit tests not to be concurrent?

Currently I have the following for running my unit tests: let suite = "suite">::: ["test_1">:: test_1; "test_2">:: test_2;] ;; let () = run_test_tt_main suite ;; Problem is, test_1 and test_2 involve some TCP communication, and have…
Adam Miller
  • 1,756
  • 1
  • 25
  • 44
1
vote
0 answers

Programmatically coping with long test suites in oUnit

My test suite looks like this let selectors = "selectors">::: [ "in_wartosc1" >:: in_wartosc1; "in_wartosc2" >:: in_wartosc2; "in_wartosc3" >:: in_wartosc3; "in_wartosc4" >:: in_wartosc4; "in_wartosc5" >::…
marmistrz
  • 5,974
  • 10
  • 42
  • 94
1
vote
1 answer

Unit testing OCaml modules with pa_ounit

I have a simple module to test with a few inline pa_ounit tests, i've setup the directory in the oasis style and got it all to build. For a reference I've been using: https://github.com/janestreet/textutils How would one execute the unit-tests for…
Pepe
  • 405
  • 5
  • 12
1
vote
1 answer

string difference pretty-printer for OUnit.assert_equal

OUnit.assert_equal ~pp_diff allows pretty-printing of expected/actual value differences and OUnitDiff seems to provide differs for collections. Is there a stock pp_diff for string values though? Ideally one that makes a best-effort to expand diffs…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1
vote
2 answers

ocamlbuild and OUnit

I have a project structured like this: Makefile src/ main.ml tests/ tests.ml and the Makefile is something like this: tests: ocamlbuild -Is src,tests tests.byte -build-dir $(BUILDDIR) $(TESTFLAGS) -lflags -I,/usr/lib/ocaml/oUnit -cflags…
snim2
  • 4,004
  • 27
  • 44
1
vote
2 answers

Module case name confusion

I made the mistake of updating software and now I can't run any OUnit tests. I think I've managed to boil the problem down to a simple REPL session. $ ocaml -I /opt/local/lib/ocaml/site-lib/oUnit OCaml version 4.01.0 #…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0
votes
1 answer

OUnit2 Unbound Module

I am writing my first Ocaml+OUnit2+Dune project. but in my unit test when I say open Mymaps it says "Unbound module Mymaps" The structure of my project is as follows mymaps | |-> lib |-> dune |-> mymaps.mli |-> mymaps.ml …
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
0
votes
1 answer

OUnit Test Issues with Dune

I have a project with this structure: - dune - main.ml -- src/ ---- dune ---- parser.ml -- test/ ---- dune ---- test_parser.ml In src/dune, I have the following: (library (name Parser)) In test/dune: (test (name test_token_type) (libraries oUnit…
abhillman
  • 3,942
  • 1
  • 20
  • 21
0
votes
0 answers

OCaml: When I add packages to the command line, Ocaide no longer executes the new bytecode

I'm using Ocaide on a Linux Mint and when I run a file with just print_string "TEST";; it prints as expected. Now I want to write some code and use OUnit, so I put open OUnit2;; at the top. It gives an error that the module is unbound, fine, I…
Addem
  • 3,635
  • 3
  • 35
  • 58
0
votes
0 answers

Is it possible to use OUnit in Ocaide?

I'm trying to use Ocaide in Eclipse, and need to use OUnit. All the instructions I find are for using it at the command line. I tried to edit the run configurations by adding -pkgs oUnit but that didn't work. I found instructions elsewhere that…
Addem
  • 3,635
  • 3
  • 35
  • 58
0
votes
1 answer

OUnit: assert value is instance of type

Using the OUnit unit testing framework in OCaml, I would like to test that the result of evaluating a function is an instance of a specified type. Defining such a test in Python's PyTest would be done as follows: def test_foo(): assert…
David Shaked
  • 3,171
  • 3
  • 20
  • 31
1
2