Logging Code Coverage

Later in the development process, you might want to refine your code for performance and ensure that you've adequately tested the code by logging code coverage information.

Code coverage gives you information about which lines of code have been executed and how long it took to execute them. This information can help you identify areas of code that aren't being executed and therefore aren't being tested, as well as areas of the code that you might want to fine tune for performance.

You can toggle code coverage on and off by clicking the Code Coverage toolbar button in the Debugger window. If you toggle code coverage on, the Coverage dialog box opens so that you can specify a file to save the coverage information to.

You can also toggle coverage logging on and off programmatically by using the SET COVERAGE TO command. You could, for example, include the following command in your application just before a piece of code you want to investigate:

SET COVERAGE TO mylog.log

After the section of code you want to log coverage for, you could include the following command to set code coverage off:

SET COVERAGE TO

When you've specified a file for the coverage information, switch to the main Visual FoxPro window and run your program, form, or application. For every line of code that is executed, the following information is written to the log file:

  • How long in seconds the line took to execute.
  • The class, if any, that the code belongs to.
  • The method or procedure the line of code is in.
  • The number of the line of code.
  • The file that the code is in.
  • Call stack level in which the line of code executes.

The easiest way to extract information from the log file is to convert it into a table so that you can set filters, run queries and reports, execute commands, and manipulate the table in other ways.

The Coverage Profiler application creates a cursor from the data generated in coverage logging and uses this cursor in a window for easy analysis.

The following program converts the text file created by the coverage log into a table:

cFileName = GETFILE('DBF')
IF EMPTY(cFileName)
   RETURN
ENDIF

CREATE TABLE (cFileName) ;
   (duration n(7,3), ;
   class c(30), ;
   procedure c(60), ;
   line i, ;
   file c(100))
   
APPEND FROM GETFILE('log') TYPE DELIMITED

See Also

Output Display | The Handling of Run-Time Errors | Testing and Debugging Applications | Debugger | Coverage | SET COVERAGE TO