aplteam / CodeCoverage

Collects data on code coverage and create an HTML report on the data collected
MIT License
1 stars 1 forks source link

The class CodeCoverage

Overview

This class provides methods to watch certain parts of the workspace for being executed, probably as part of running a test framework like Tester2, although checking on code coverage by just running an application can also be useful, especially for legacy code.

Note: CodeCoverage releases are published as Tatin packages, see there.

Getting started

The following gives an example, assuming that there is an application in #.Foo we are interested in:

      C←⎕NEW #.CodeCoverage (,⊂'#.Foo')
      C.ignore←'#.Foo.TestCases,#.Foo.Samples'
      C.filename←'./Foo-Coverage'
      C.Start ⍬
      #.Foo.Run ⍬
      C.Stop ⍬
      C.Finalise ⍬
      ⎕←#.CodeCoverage.ProcessDataAndCreateReport  C.filename
./Foo-Coverage.html      
      C.Start ⍬
      #.Foo.Run ⍬
      C.Stop ⍬
      C.Finalise ⍬
      ⎕←1 #.CodeCoverage.ProcessDataAndCreateReport C.filename      
./Foo-Coverage.html      

Notes

A real-world example taken from two runs of the test suite of the FilesAndDirs package looks like this:

Example

Lines that are empty or contain nothing but a comment line, or contain only an element of a control structure such as :Else or any flavour of :End are not counted as executable lines.

The test suite was run twice, once on Windows and once on Linux. This is important because in a considerable number of cases FilesAndDirs will execute different lines of code depending on the operating system.

The fact that there are four functions that are not covered at all will be of interest in any case:

Ideally test cases should cover 100% of the code, but this may not be feasible. However, a code coverage of less than 50% seems to be unacceptable for functions that have a significant number of "executable" lines.

More Details

Limitations

Running multiple test suites

It is possible to run the test suite more than once (e.g. on different platforms), or to run two test suites that share code (e.g. Client and Server part of an application) and have CodeCoverage aggregate all the coverage data into a single component file before generating a report.

After running the last test suite you can call the shared method CodeCoverage.AggregateCoverageDataAndCreateReport and pass the name of the component file containing all the coverage data. The function will then aggregate the data, massage it and then create an HTML file with the final report.

CodeCoverage provides three shared methods for aggregation and reporting:

The verbose option

verbose, the optional left argument of both CreateReport and ProcessDataAndCreateReport, defaults to 0. This means that the body of any function or operator that is only partially covered will not be listed in the report, only the fully qualified names and information about which lines were not executed, the coverage percentage and the total number of lines.

Usually this makes sense when you generate the first coverage report as you will often have quite a few functions that are not yet covered by the tests, and you will probably add more tests to cover some of them, or exclude those that you have good reasons not to be interested in; see the next topic for more on this.

Others may be so trivial that there is no point in covering them, so you may want to ignore them as well.

After you have done this, it is advisable to run the tests again with the coverage option enabled, but this time you generate the report with verbose←1 because now it makes sense to look at the details.

The ignore property

This is a text vector that is empty by default. You can add fully qualified comma-separated names of functions and operators that you want to be ignored when reporting on code coverage.

Note that ignore is an instance property.