PLunit How-To / teaching example

I run SWI-Prolog v8.0.3 (32 bit) on a W10 box. I am writing a little compiler and I want to provide comprehensive test case coverage. I use PLUnit pretty much according to the documentation - or so I thought:

  • I have files foo.pl for a module with a set of grammatical rules, exporting these rules
  • I have files foo_TEST.pl that include the rule file by ‘use_module(foo)’, and declare the test cases.
  • I have a file test.pl that calls all the individual tests by ‘:- consult(‘foo_TEST.pl’)’ for each.

When running 'show_coverage(test.pl), I do get the output I want, but also lots of stuff that I think shouldn’t be there. Here’s a sample (line numbers added for reference).

1 c:/_tools/momat/app/monotype/src/grammar/foo.pl 43 97.7 0.0
2 …ols/momat/app/monotype/src/grammar/foo_test.pl 110 50.0 0.0
3 …ols/momat/app/monotype/src/grammar/bar_test.pl 110 50.0 0.0
4 c:/program files (x86)/swipl803-1-w32/library/option.pl 40 5.0 2.5
5 c:/program files (x86)/swipl803-1-w32/library/plunit.pl 261 0.4 0.0

1 - I expect lines like this
2 - I kind of get why this is here, but it does look awkward.
3 - And when “:-consult(bar_TEST.pl)” is commented out in test.pl I still get this …why?
4 - This I really don’t get. Why would I be interested in the test coverage of basic libraries?
5 - And this is really weird. I think this shouldn’t be here.

So, is this all on purpose and I just have strange expectations? :wink:
Or am I doing this wrong? If so, how to fix it? It would be great to have a working, exemplary demo project somewhere that I could consult for inspiration (sorry for the pun).

I see that JW uses “:-test_foo.” in PLUnit - summary report, suggesting that file “foo_TEST.pl” should define “test_foo/0” doing - what exactly? So I guess there’s a different file organization that is supposed to be used?

Thanks in advance for enlightenment and tips! :slight_smile:

1 Like

The coverage tool knows nothing about tests. It just shows coverage of code. It is up to you to figure out the file you are tested and thus that you are interested in the coverage of those files (only). It is more a proof of concept that was never finished. It should provide much more detailed information, notably on what is not covered. I would not know why not loading a file still shows the file. Possibly it is still loaded some other way? Temporary renaming it and see whether you get an error is a simple way to find out. You can also use source_file_property/2 to figure out from where the file is loaded.

This is just one way of using this. For SWI-Prolog itself, test files are organized in directories and called test_XXX.pl. They are module files that export test_XXX/0 only. This way we can easily write a generic test driver that picks up all files, loads them and calls their entry point, all without risking name conflicts. It also easily accommodates tests that were written before PlUnit was added.

1 Like

Related blog post, fresh out of the oven:

Generating code coverage reports

The described solution it would, however, require the OP to either compile the modules as objects (or convert them to objects; see this guide).

1 Like