Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Inline (Unit) Tests for OCaml

License

NotificationsYou must be signed in to change notification settings

vincent-hugot/qtest

Repository files navigation

qtest is an inline test extraction project, originallydeveloped internally forthe OCaml Batteries Included library under thenameqtest.It relies onoUnit as a testing framework,though users need not know anything about it for basic usage;it also relies onQcheck for random testing.

Note
qtest stands forQuick Testing.
  • qtest is available for installation as anOPAM package.

  • It has extensive documentation: see section "Introduction: What Is QTest?" to get started.

  • It has good syntax highlighting for Kate (KatePart: KWrite, KDevelop, Konqueror,…​)and basic support for Emacs. See theeditor_support directory

Manual installation:

./configuremake build install

To use a custom installation prefix, use

./configure --prefix <path>

Future works:

There are ideas floating around on how to improve qtest2, generally revolvingaround going from a test "extraction" to an "annotation" model.No timetable is set yet, as all parties involved are busy bees,and qtest2 currently covers most of the needs of the Batteries projectand others.

History of the project:

(or at least, what I (VH) can unearth of it thanks to git logs)

  • 2007—​2008 : Ilmari Heikkinen writesmake_suite.rb for his Prelude.ml.

  • Jan 17, 2011:make_suite.rb is copied into Batteries. (=qtest0)

  • Jan 27, 2011: Kaustuv Chaudhuri writes from scratchan equivalentmake_suite.mll to replace the Ruby script. (=qtest1)

  • Jan 19, 2012: Vincent Hugot writes from scratch a new version, with a lotof new features. Syntax changes a bit. (=qtest2)

  • Oct 21, 2012: qtest2 moves to its own repository.

  • Sept. 2015: Simon Cruanes contributes a significant improvement of the random generation process.

  • March. 2016: Simon Cruanes integratesqcheck withqtest

  • Dec. 2016:qcheck andqtest are split apart again

  • Feb. 2018: renaming the repository toqtest again

Over time, the various versions of qtest havereceivedcontributions by:Eric Norige, Gabriel Scherer, Cedric Cellier, Valentin Gatien-Baron, Max Mouratov,and Simon Cruanes.

Contact:

The preferred way is to create anew issue on GitHub.

Current maintainer:Simon Cruanes.


Introduction: What Is QTest?

In a nutshell, qtest is a small program which reads.ml and.mli sourcefiles and extracts inline unit tests from them. It is used internally bytheOCaml Batteries project,and is shipped with it as of version 2.0, but it does notdepend on it and can be compiled and used independently.

Browse its code in theGithub Repository.

Using qtest: a Quick, Simple Example

Say that you have a filefoo.ml, which contains the implementation ofyour new, shiny functionfoo.

letrecfoox0f=function|[] ->0|x::xs -> f x (foo x0 f xs)

Maybe you don’t feel confident about that code; or maybe you do, but youknow that the function might be re-implemented less trivially in thefuture and want to prevent potential regressions. Or maybe you simplythink unit tests are good practice anyway. In either case, you feel thatbuilding a separate test suite for this would be overkill. Using qtest,you can immediately put simple unit tests in comments nearfoo, forinstance:

(*$T foo  foo  0 ( + ) [1;2;3] = 6  foo  0 ( * ) [1;2;3] = 0  foo  1 ( * ) [4;5]   = 20  foo 12 ( + ) []      = 12*)

the syntax is simple:(*$ introduces a qtest "pragma", such asTin this case.T is by far the most common and represents a "simple"unit test.T expects a "header", which is most of the time simplythe name of the function under test, herefoo. Following that, eachline is a "statement", which must evaluate totrue for the test topass. Furthermore,foo must appear in each statement.

Now, in order to execute those tests, you need to extract them; this isdone with the qtest executable. The command

$ qtest -o footest.ml extract foo.mlTarget file: `footest.ml'. Extraction : `foo.ml' Done.

will create a filefootest.ml; it’s not terribly human-readable, butyou can see that it contains your tests as well as someOUnitboilerplate. Now you need to compile the tests, for instance withocamlbuild, and assuming OUnit was installed forocamlfind.

$ ocamlbuild -cflags -warn-error,+26 -use-ocamlfind -package oUnit \    footest.nativeFinished, 10 targets (1 cached) in 00:00:00.

Note that the-cflags -warn-error,+26 is not indispensable butstrongly recommended. Its function will be explained in more detail inthe more technical sections of this documentation, but roughly it makessure that if you write a test forfoo, via(*$T foo for instance,thenfoo isactually tested by each statement – the tests won’tcompile if not.

Important note: in order for this to work,ocamlbuild must knowwhere to findfoo.ml; iffootest.ml is not in the same directory,you must make provisions to that effect. Iffoo.ml needs some specificflags in order to compile, they must also be passed.

Now there only remains to run the tests:

$ ./footest.native..FF==============================================================================Failure: qtest:0:foo:3:foo.ml:10OUnit: foo.ml:10::>  foo 12 ( + ) [] = 12------------------------------------------------------------------------------==============================================================================Failure: qtest:0:foo:2:foo.ml:9OUnit: foo.ml:9::>  foo 1 ( * ) [4;5] = 20------------------------------------------------------------------------------Ran: 4 tests in: 0.00 seconds.FAILED: Cases: 4 Tried: 4 Errors: 0 Failures: 2 Skip:0 Todo:0

Oops, something’s wrong… either the tests are incorrect orfoo is.Finding and fixing the problem is left as an exercise for the reader.When this is done, you get the expected

$ ./footest.native....Ran: 4 tests in: 0.00 seconds.
Tip
those steps are easy to automate, for instance with a small shellscript:
set -e # stop on first errorqtest -o footest.ml extract foo.mlocamlbuild -cflags -warn-error,+26 -use-ocamlfind -package oUnit footest.native./footest.native

More qtest Pragmas

Different Kinds of Tests

Simple Tests:T for "Test"

The most common kind of tests is the simple test, an example of which isgiven above. It is of the form

(*$T <header>  <statement>  ...*)

where eachstatement must be a boolean OCaml expression involving thefunction (or functions, as we will see when we study headers) referencedin theheader. The overall test is considered successful if eachstatement evaluates totrue. Note that the "close comment"*)must appear on a line of its own.

Tip: if a statement is a bit too long to fit on one line, if can bebroken using a backslash (\), immediately followed by the carriagereturn. This also applies to randomised tests.

Equality Tests:=

The vast majority of test cases tend to involve the equality of twoexpressions; using simple tests, one would write something like:

(*$T foo  foo 1 ( * ) [4;5] = foo 3 ( * ) [1;5;2]*)

While this certainly works, the failure report for such a test does notconvey any useful information besides the simple fact that the testfailed. Wouldn’t it be nice if the report also mentioned the values ofthe left-hand side and the right-hand side ? Yes it would, andspecialised equality tests provide such functionality, at the cost of alittle bit of boilerplate code. The bare syntax is:

(*$= <header>  <lhs> <rhs>  ...*)

However, used bare, an equality test will not provide much moreinformation than a simple test: just a laconic "not equal". In orderfor the values to be printed, a "value printer" must be specified forthe test. A printer is a function of type'a → string, where'a isthe type of the expressions on both side of the equality. To pass theprinter to the test, we useparameter injection (cf. SectionHeader Parameters Injection); equality tests have an optional argumentprinter forthis purpose. In our example, we have'a = int, so the test becomes simply:

(*$= foo & ~printer:string_of_int  (foo 1 ( * ) [4;5]) (foo 3 ( * ) [1;5;2])*)

The failure report will now be more explicit, sayingexpected: 20 but got: 30.

Randomized Tests:Q for "Quickcheck"

Quickcheck is a small library useful for randomized unit tests. Using itis a bit more complex, but much more rewarding than simple tests.

(*$Q <header>  <generator> (fun <generated value> -> <statement>)  ...*)

Let us dive into an example straight-away:

(*$Q foo  Q.small_int (fun i-> foo i (+) [1;2;3] = List.fold_left (+) i [1;2;3])*)

The Quickcheck module is accessible simply asQ within inline tests;small_int is a generator, yielding a random, small integer. When thetest is run, each statement will be evaluated for a large number ofrandom values – 100 by default. Running this test for theabove definition of foo catches the mistake easily:

law foo.ml:14::>  Q.small_int (fun i-> foo i (+) [1;2;3]    = List.fold_left (+) i [1;2;3])failed for 2

Note that the random value for which the test failed is provided by theerror message – here it is 2. It is also possible to generate severalrandom values simultaneously using tuples. For instance

(Q.pairQ.small_int (Q.listQ.small_int)) \  (fun (i,l)-> foo i (+) l=List.fold_left (+) i l)

will generate both an integer and a list of small integers randomly. Afailure will then look like

law foo.ml:15::>  (Q.pair Q.small_int (Q.list Q.small_int))    (fun (i,l)-> foo i (+) l = List.fold_left (+) i l)failed for (727, [4; 3; 6; 1; 788; 49])

A generator such asQ.pair Q.small_int Q.printable_string is actually a value of type'a Q.arbitrary (in this particular case,(int * string) arbitrary).It combines a random generation function ('a Q.Gen.t),and optional printing, shrinking and size functions that are used todisplay counter-examples and minimize their size. It is possible, asexplained below, to define one’s own'a arbitrary values, for instancefor custom types.

Available Generators:

Simple generators

unit,bool,float,pos_float,neg_float,int,int32,int64,pos_int,small_int,neg_int,char,printable_char,numeral_char,string,printable_string,numeral_string

Structure generators

list andarray. They take one generator as their argument. Forinstance(Q.list Q.neg_int) is a generator of lists of (uniformlytaken) negative integers.

Tuple generators

pair andtriple are respectively binary and ternary. See above foran example ofpair.

Size-directed generators

string,numeral_string,printable_string,list andarray allhave*_of_size variants that take the size of the structure as theirfirst argument.

See theonline documentation of QCheckfor more details.

Tips:

Duplicate Elements in Lists

When generating lists, avoidQ.list Q.int unless you have a good reason to do so. The reason isthat, given the size of theQ.int space, you are unlikely to generateany duplicate elements. If you wish to test your function’s behaviourwith duplicates, preferQ.list Q.small_int.

Filtering Inputs

Rando, inputs can be filtered for aprecondition by stating a propertyf =⇒ g. An inputx will be tested for the propertyg only iff x holds,otherwise it is discarded and a new input is generated. The total number ofinputs generated can be capped using the~max_gen:int parameter (it shouldbe bigger than~count). The system will try to makecount tests, but stopsaftermax_gen inputs are generated to avoid looping forever if acceptableinputs are too rare.

Changing Number of Tests

If you want a specific test to executeeach of its statements a specific number of times (deviating from thedefault of 100), you can specify it explicitly throughparameter injection (cf. SectionHeader Parameters Injection) using thecount :argument.

Getting a Better Counterexample

By default, a random test stops assoon as one of its generated values yields a failure. This first failurevalue is probably not the best possible counterexample. You canforceqtest to generate and test allcount random values regardless, and todisplay the value which is smallest with respect to a certain measurewhich you define. To this end, it suffices to use parameter injection topass argumentsmall : 'a → 'b, where'a is the type of generated values and'b is any totally ordered set (wrt.<).Typically you will take'b = int or'b = float. Example:

letfuzx= xletrecflu=function|[] ->[]|x ::l ->ifList.mem x lthen flu lelse x :: flu l(*$Q fuz; flu & ~small:List.length  (Q.list Q.small_int) (fun x -> fuz x = flu x)*)

The meaning of~small:List.length is therefore simply: "choose the shortest list". For very complicated cases, you can simultaneouslyincreasecount to yield an even higher-quality counterexample.

Shrinking

A parametershrink: ('a → 'a Q.Iter.t) can be provided along with a randomgenerator.'a Q.Iter.t is an iterator on values of type'a.shrink xshould iterate on a set of values that are smaller thanx (for instance,ifx: int list,shrink x will remove each element of the list).If a generator (of type'a arbitrary) defines a shrink function, thenwhenever a counter-example is found for a property, thecounter-example will be shrunk recursively as long as it continues refutingthe property; this allows to find smaller and simpler counter-examples.However, shrinking can be slow.A parameter~max_fail:int can be given to the testby writing(*$Q & ~max_fail:5 to limit the number of counter-examplesto find, in case shrinking them is too slow.

The moduleQ.Shrink can be used to combine shrinking functions.

Example: the false property(Q.list Q.int) (fun l → not (List.mem 5 l))might be falsified by the counter-example[1;2;3;4;5;6;7;8]. By recursivelyshrinking the value (trying to remove elements one by one) the minimalcounter-example[5] will be found and displayed.

Raw Random Tests

Using($QR, similar to the raw unit test($R, it is possible towrite a random test on multiple lines without the trailing\characters.

(*$QR foo  Q.small_int    (fun i->      foo i (+) [1;2;3] = List.fold_left (+) i [1;2;3]    )*)

The(*$QR block needs to contain exactly two values:

Random Generator

of type'a Quickcheck.arbitrary

Property to test

of type'a → bool

Custom Generators

For types that are not lists of integers or strings, it can be useful to defineone’s own'a arbitrary instance for the type. The function to use isQ.make, it takes a'a Q.Gen.t random generator, and optional arguments

  • ~shrink:('a → 'a Iter.t) to define how to shrink counter-examples

  • ~small:('a → 'b) (where'b is ordered) to select small counter-examples

  • ~print:('a → string) to print counter-examples

  • ~collect:('a → string) maps inputs to astring descriptor andcounts how many values belong to each descriptor, for statistics.

    Some generators are already defined inQ.Gen. Gabriel Scherer’srandom-generator library is alsoa good basis for more advanced generators.

    Printers can be defined usingQ.Print, shrinkers usingQ.Shrink.

Raw OUnit Tests:R for "Raw"

When more specialised test pragmas are too restrictive, for instance ifthe test is too complex to reasonably fit on one line, then one can useraw OUnit tests.

(*$R <header>  <raw oUnit test>...  ...*)

Here is a small example, with two tests stringed together:

(*$R foo  let thing = foo  1 ( * )  and li = [4;5] in  assert_bool "something_witty" (thing li = 20);  assert_bool "something_wittier" (foo 12 ( + ) [] = 12)*)

Note that if the first assertion fails, the second will not be executed;so stringing two assertions in that mode is different in that respectfrom doing so under aT pragma, for instance.

That said, raw tests should only be used as a last resort; for instanceyou don’t automatically get the source file and line number when thetest fails. IfT andQ do not satisfy your needs, then it isprobably a hint that the test is a bit complex and, maybe, belongs ina separate test suite rather than in the middle of the source code.

Exception-Throwing Tests:E for "Exception"

… not implemented yet…

The current usage is to use(*$T and the following pattern forfunctionfoo and exceptionBar:

try ignore (foo x);falsewithBar ->true

If your project uses Batteries and no pattern-matching is needed, thenyou can also use the following, sexier pattern:

Result.(catch foo x|> is_exnBar)

Manipulation Pragmas

Not all qtest pragmas directly translate into tests; for non-trivialprojects, sometimes a little boilerplate code is needed in order to setthe tests up properly. The pragmas which do this are collectively called"manipulation pragmas"; they are described in the next section.

Opening Modules:open Pragma<…​> and--preamble Option

The tests should have access to the same values as the code under test;however the generated code forfoo.ml does not actually live insidethat file. Therefore some effort must occasionally be made tosynchronise the code’s environment with the tests’. There are three mainusecases where you might want to open modules for tests:

Project-Wide Global Open

It may happen thatevery single file in your project opens a givenmodule. This is the case for Batteries, for instance, where every moduleopensBatteries. In that case simply use the–preamble switch. Forinstance,

qtest --preamble "open Batteries;;"  extract mod1.ml mod2.ml ... modN.ml

Note that you could insert arbitrary code using this switch.c

Global Open in a File

Now, let’s say thatfoo.ml opensBar andBaz; you want the testsinfoo.ml to open them as well. Then you can use theopen pragma initsglobal form:

(*$< Bar, Baz >*)

The modules will be open for every test in the same.ml file, andfollowing the pragma. However, in our example, you will have aduplication of code between the "open" directives offoo.ml, and theopen pragma of qtest, like so:

open Bar;; open Baz;;(*$< Bar, Baz >*)

It might therefore be more convenient to use thecode injection pragma(see next section) for that purpose, so you would write instead:

(*${*) open Bar;; open Baz;; (*$}*)

The code between that special markup will simply be duplicated into thetests. The two methods are equivalent, and the second one isrecommended, because it reduces the chances of an impedance mismatchbetween modules open forfoo.ml and its tests. Therefore, the globalform of theopen pragma should preferentially be reserved for caseswhere youwant such a mismatch. For instance, if you have specialmodules useful for tests but useless for the main code, you can easilyopen then for the tests alone using the pragma.

Local Open for a Submodule

Let’s say we have the followingfoo.ml:

letouterx=<something>moduleSubmod=structletinnery=2*x(*$T inner    inner 2 = 4*)end

That seems natural enough… but it won’t work, because qtest is notactually aware that the test is "inside" Submod (and making it awareof that would be very problematic). In fact, so long as you use onlytest pragmas (ie. no manipulation pragma at all), the positions and eventhe order of the tests – respective to definitions or to each other –are unimportant, because the tests do not actually live infoo.ml. Sowe need to open Submod manually, using thelocal form of theopenpragma:

moduleSubmod=struct(*$< Submod*)letinnery=2*x(*$T inner    inner 2 = 4*)end(*$>*)

Notice that the<…​> have simply been split in two, compared to theglobal form. The effect of that construct is that Submod will be openfor every test between($< Submod *) and($>*). Of course, youcould also forgo that method entirely and do this:

moduleSubmod=structletinnery=2*x(*$T &    Submod.inner 2 = 4*)end

… but it is impractical and you areforced to use an empty headerbecause qualified names are not acceptable as headers. The first methodis thereforestrongly recommended.

Code Injection Pragma:

TODO: ocamldoc comments that define unit tests from the offered examples

Technical Considerations and Other Details

What has been said above should suffice to cover at least 90% ofuse-cases for qtest. This section concerns itself with the remaining10%.

Function Coverage

The headers of a test are not just there for decoration; threeproperties are enforced when a test, say,(*$X foo is compiled, whereX isT,R,Q,QR,… :

  • foo exists; that is to say, it is defined in the scope of the modulewhere the testappears – though one can play with pragmas to relax thiscondition somewhat. At the very least, it has to be definedsomewhere. Failure to conform results in anError: Unbound value foo.

  • foo is referenced ineach statement of the test: forT andQ,that means "each line". ForR, that means "once somewhere in thetest’s body". Failure to conform results in aWarning 26: unused variable foo, which will be treated as an error if-warn-error +26 is passed to the compiler. It goes without saying thatthis is warmly recommended.

  • the test possesses at least one statement.

Those two conditions put together offer a strong guarantee that, if afunction is referenced in a test header, then it is actually tested atleast once. The list of functions referenced in the headers of extractedtests is written by qtest intoqtest.targets.log. Each line is of theform

foo.ml   42    foo

wherefoo.ml is the file in which the test appears, as passed toextract, and42 is the line number where the test pragma appears infoo.ml. Note that a same function can be listed several times for thesame source file, if several tests involve it (say, two times if it hasboth a simple test and a random one). The exact number of statementsinvolvingfoo in each test is currently not taken into account in thelogs.

Headers and Metaheaders

The informal definition of headers given in the above was actually asimplification. In this section we explore two syntaxes available forheaders.

Aliases

Some functions have exceedingly long names. Case in point :

letrecpretentious_drivelx0f=function|[] -> x0|x::xs -> pretentious_drivel (f x x0) f xs
(*$T pretentious_drivel  pretentious_drivel 1 (+) [4;5] = foo 1 (+) [4;5]  ... pretentious_drivel of this and that...*)

The constraint that each statement must fit on one line does not playwell with very long function names. Furthermore, youknown whichfunction is being tested, it’s right there is the header; no need torepeat it a dozen times. Instead, you can define analias, and writeequivalently:

(*$T pretentious_drivel as x  x 1 (+) [4;5] = foo 1 (+) [4;5]  ... x of this and that...*)

…thus saving many keystrokes, thereby contributing to thepreservation of the environment. More seriously, aliases have usesbeyond just saving a few keystrokes, as we will see in the nextsections.

Mutually Tested Functions

Most of the time, a test only pertains to one function. There are times,however, when one wishes to test two functions – or more – at the sametime. For instance

letreceven=function0 ->true|n -> odd (pred n)andodd=function0 ->false|n -> even (pred n)

Let us say that we have the following test:

(*$Q <header>  Q.small_int (fun n-> odd (abs n+3) = even (abs n))*)

It involves botheven andodd. That question is: "what is a properheader for this test?" One could simply put "even", and thus it wouldbe referenced as being tested in the logs, butodd would not, which isunfair. Putting "odd" is symmetrically unfair. The solution is to putboth, separated by a semi-colon:

(*$Q even; odd

That wayboth functions are referenced in the logs:

    foo.ml   37    even    foo.ml   37    odd

and of course the compiler enforces that both of them are actuallyreferenced in each statement of the test. Of course, each of them can bewritten under alias, in which case the header could beeven as x; odd as y.

Testing Functions by the Dozen

Let us come back to our functionsfoo (after correction) andpretentious_drivel, as defined above.

letrecfoox0f=function|[] -> x0|x::xs -> f x (foo x0 f xs)letrecpretentious_drivelx0f=function|[] -> x0|x::xs -> pretentious_drivel (f x x0) f xs

You will not have failed to notice that they bear more than a passingresemblance to one another. If you write tests for one, odds are thatthe same test could be useful verbatim for the other. This is a verycommon case when you have closely related functions, or even severalimplementations of the same function, for instance the old, slow,naïve, trustworthy one and the new, fast, arcane, highly optimisedversion you have just written. The typical case is sorting routines, ofwhich there are many flavours.

For our example, recall that we have the following test forfoo:

(*$Q foo  (Q.pair Q.small_int (Q.list Q.small_int)) \    (fun (i,l)-> foo i (+) l = List.fold_left (+) i l)*)

The same test would apply topretentious_drivel; you could justcopy-and-paste the test and change the header, but it’s not terriblyelegant. Instead, you can just just add the other function to theheader, separating the two by a comma, and defining an alias:

(*$Q foo, pretentious_drivel as x  (Q.pair Q.small_int (Q.list Q.small_int)) \  (fun (i,l)-> x i (+) l = List.fold_left (+) i l)*)

This same test will be run once forx = foo, and once forx = pretentious_drivel. Actually, you need not define an alias: if theheader is of the form

(*$Q foo, pretentious_drivel

then it is equivalent to

(*$Q foo, pretentious_drivel as foo

so you do not need to alter the body of the test if you subsequently addnew functions. A header which combines more than one "version" of afunction in this way is called ametaheader.

Metaheaders Unleashed

All the constructs above can be combined without constraints: thegrammar is as follows:

    Metaheader  ::=   Binding {";" Binding}    Binding     ::=   Functions [ "as" ID ]    Functions   ::=   ID {"," ID}    ID          ::=   (*OCaml lower-case identifier*)

Header Parameters Injection

Use(*$inject foo *) to inject the piece of codefoo at thebeginning of this module’s tests. This is useful, for instance, todefine frequently used random generators, or printers, or to instantiatea functor before testing it.

Warnings and Exceptions Thrown by qtest

Fatal error: exception Failure("Unrecognised qtest pragma: ` T foo'")

You have written something like($ T foo; there must not be any spacebetween($ and the pragma.

Warning: likely qtest syntax error: `(* $T foo'. Done.

Self-explanatory; if$ is the first real character of a comment, it’slikely a mistyped qtest pragma. This is only a warning though.

Fatal error: exception Core.Bad_header_char("M", "Mod.foo")

You have used a qualified name in a header, for instance(*$T Mod.foo.You cannot do that, the name must be unqualified and defined under thelocal scope. Furthermore, it must be public, unless you have usedpragmas to deal with private functions.

Error: Comment not terminatedFatal error: exception Core.Unterminated_test(_, 0)

Most probably, you forgot the comment-closing*) to close some test.

Fatal error: exception Failure("runaway test body terminator: n))*)")

The comment-closing*) must be on a line of its own; or, put anotherway, every statement must be ended by a line break.

qtest Command-Line Options

$ qtest --help** qtest (qtest)USAGE: qtest [options] extract <file.mli?>...OPTIONS:--output <file.ml>    (-o) def: standard output  Open or create a file for output; the resulting file will be an OCaml  source file containing all the tests.--preamble <string>   (-p) def: empty  Add code to the tests' preamble; typically this will be an instruction  of the form 'open Module;;'--help          Displays this help page and stops

qtest Runtime Command-Line Options

Test files generated by qtest also accept command line options, describedby--help if needed.

$ qtest extract foo.ml -o footest.ml$ ocamlfind ocamlopt -package qcheck -linkpkg footest.ml -o footest$ ./footest --helprun qtest suite-v-verbose  enable verbose tests-l-list     print list of tests (2 lines each). Implies -verbose-s-seed     set random seed (to repeat tests)-help     Display this list of options--help    Display this list of options

Currently the options are:

  • --verbose: verbose quick check tests (print statistics, etc.)

  • --list: print a list of tests as they are executed.

  • --seed: force the choice of a random seed. When random tests start, therandom seed used by the random generators is displayed; later, providingthe same seed with--seed <s> will repeat the same tests.

A few tricks

A few useful tricks when writing inline tests:

  • if possible, favor($= a b *) over($T (a = b) *), because the formermakes it possible to add a printer (with& ~printer:some_printer) incase the two values are not equal

  • random tests are useful to check general properties, or compare a complex-but-efficientimplementation to a (possibly naive) reference implementation. Forinstance, if we had implemented a fancy sort functionmy_sort on lists, wecould compare it to the stdlib’sList.sort:

    (*$Q  Q.(list int) (fun l -> \    my_sort compare l = List.sort compare l)*)
  • to factor some code that is useful in tests, but should not appear in themodule (for instance, printers or generators for running complex tests),you can use(*$inject …​ *) somewhere in the.ml file:

    typefoo= {a :int;b :string }(*$inject  let pp_foo f = Printf.sprintf "foo{a=%d, b=%s}" f.a f.b*)(*$= & ~printer:pp_foo  {a=0; b="b1"} {a=42; b="b2"}*)

    here, the test can use a custom printer defined above (and it needs it,for it will fail badly).

Using qtest with dune

The simplest way is to use(inline_tests (backend qtest.lib)) in alibrarystatement:

(library  (name foo)  (inline_tests (backend qtest.lib)))

And thendune runtest should automatically find inline tests in thelibrary’s modules.

For better control, a rule can be used (adapt to fit your needs):

(rule  (targets run_qtest.ml)  (deps (source_tree src))  ; here is where you need to tell qtest what files to consider  (action (run qtest extract src/foo1.ml src/foo2.ml > %{targets})))(executable  (name run_qtest)  (modules run_qtest)  ; disable some warnings in qtests  (flags :standard -warn-error -a -w -33-35-27-39)  (libraries qcheck))(alias  (name    runtest)  (deps    run_qtest.exe)  (action  (run %{deps})))

Using qtest with OCamlbuild

The following snippet, added tomyocamlbuild.ml, will useqtestto extractfoo_tests.ml fromfoo.ml for any modulefoo.

openOcamlbuild_plugin;;rule"qtest extract"~prod:"%_tests.ml"~deps:["%.ml"]  (funenvbuild ->Cmd(S[A"qtest";A"extract";A"-o";P(env"%_tests.ml");P(env"%.ml")]))

It is also possible to make a singleall_tests.ml file from many modules, ifthey are listed inall_tests.qtestpack file (similar to.mllib):

openOcamlbuild_plugin;;letimport_qtestpackbuildpackfile=let tags1= tags_of_pathname packfileinlet files= string_list_of_file packfileinlet include_dirs=Pathname.include_dirs_of (Pathname.dirname packfile)inlet files_alternatives=List.mapbeginfunmodule_name ->      expand_module include_dirs module_name ["ml";"mli"]    end filesinlet files=List.mapOutcome.good (build files_alternatives)inlet tags2=List.fold_right      (funfile ->Tags.union (tags_of_pathname file))      files tags1in  (tags2, files)letqtest_manytargetpackfileenvbuild=let packfile= env packfileand target= env targetinlet tags, files= import_qtestpack build packfileinCmd(S[A"qtest";A"extract";T tags;A"-o";A target;Command.atomize_paths files]);;rule"ocaml: modular qtest (qtestpack)"~prods:["%.ml"]~deps:["%.qtestpack"]~doc:"Qtest supports building a test module by extracting casesdirectly from several composing several .ml{,i} files together.  \To use that feature with ocamlbuild, you should create a .qtestpack \file with the same syntax as .mllib or .mlpack files: \a whitespace-separated list of the capitalized module names \of the .ml{,i} files you want to combine together."  (qtest_many"%.ml""%.qtestpack");

For instance,run_tests.qtestpack might contain

src/Foosrc/sub/Bar

and the target would be

ocamlbuild -use-ocamlfind -package qcheck \-I src -I src/sub run_tests.native

[8]ページ先頭

©2009-2025 Movatter.jp