Write unit tests for that function in a separate file test.ml.
Build and run test to execute the unit tests.
Example
sum.ml
1
2
3
letrecsum=function|[]->0|x::xs->x+sumxs
test.ml
1
2
3
4
5
6
7
8
9
10
11
openOUnit2openSumlettests="test suite for sum">:::[(*assert_equal checks whether its two arguments are equal*)"empty">::(fun_->assert_equal0(sum[]));"singleton">::(fun_->assert_equal1(sum[1]));"two_elements">::(fun_->assert_equal3(sum[1;2]));]let_=run_test_tt_maintests
To improve output, we have
1
2
3
4
5
lettests="test suite for sum">:::["empty">::(fun_->assert_equal0(sum[])~printer:string_of_int);"singleton">::(fun_->assert_equal1(sum[1])~printer:string_of_int);"two_elements">::(fun_->assert_equal3(sum[1;2])~printer:string_of_int);]
To improve the code, we have
1
2
3
4
5
6
7
8
letmake_sum_testnameexpected_outputinput=name>::(fun_->assert_equalexpected_output(suminput)~printer:string_of_int)lettests="test suite for sum">:::[make_sum_test"empty"0[];make_sum_test"singleton"1[1];make_sum_test"two_elements"3[1;2];]