# PLunit How-To / teaching example

**URL:** <https://swi-prolog.discourse.group/t/plunit-how-to-teaching-example/1544>\
**Category:** Help!\
**Created:** [November 27, 2019, 6:25pm UTC](https://swi-prolog.discourse.group/t/plunit-how-to-teaching-example/1544 "2019-11-27T18:25:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HSTO](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@HSTO](https://swi-prolog.discourse.group/u/HSTO)\
**Post date:** [November 27, 2019, 6:25pm UTC](https://swi-prolog.discourse.group/t/plunit-how-to-teaching-example/1544/1 "2019-11-27T18:25:58Z")

</div>

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? 😉  
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](https://swi-prolog.discourse.group/t/plunit-summary-report/393), 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! 🙂

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 28, 2019, 8:06am UTC](https://swi-prolog.discourse.group/t/plunit-how-to-teaching-example/1544/2 "2019-11-28T08:06:00Z")

</div>

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.

> [@HSTO](#):
>
> I see that JW uses “:-test\_foo.” in [PLUnit - summary report](https://swi-prolog.discourse.group/t/plunit-summary-report/393), 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?

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.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [December 2, 2019, 4:10pm UTC](https://swi-prolog.discourse.group/t/plunit-how-to-teaching-example/1544/3 "2019-12-02T16:10:09Z")

</div>

Related blog post, fresh out of the oven:

[Generating code coverage reports](https://logtalk.org/2019/12/02/generating-code-coverage-reports.html)

The described solution it would, however, require the OP to either compile the modules as objects (or convert them to objects; see this [guide](https://logtalk.org/using_tools_with_prolog.html)).
