64 |
65 |
Overview
66 |
67 |
68 | LuaCov is a simple coverage analyzer for Lua
69 | scripts. When a Lua script is run with the luacov module loaded, it
70 | generates a stats file with the number of executions of each line of the
71 | script and its loaded modules. The luacov command-line script then
72 | processes this file generating a report file which allows one to visualize
73 | which code paths were not traversed, which is useful for verifying the
74 | effectiveness of a test suite.
75 |
76 |
77 |
78 | LuaCov is free software and uses the same license as Lua (MIT).
79 |
80 |
81 |
Download
82 |
83 |
84 | LuaCov can be downloaded via LuaRocks:
85 |
86 |
87 |
88 | luarocks install luacov
89 |
90 |
91 |
92 | There are some C extensions LuaCov can use (if they are available) to improve performance
93 | and analysis accuracy. To install LuaCov with these extensions install
94 | CLuaCov package instead:
95 |
96 |
97 |
98 | luarocks install cluacov
99 |
100 |
101 |
102 | LuaCov itself is written in pure Lua and has no external dependencies.
103 |
104 |
105 |
106 | You can also get the code directly from the git repo.
107 |
108 |
109 |
Instructions
110 |
111 |
112 | Using LuaCov consists of two steps: running your script to collect
113 | coverage data, and then running luacov on the collected data to
114 | generate a report.
115 |
116 |
117 |
118 | To collect coverage data, your script needs to load the luacov
119 | Lua module. This can be done from the command-line, without modifying
120 | your script, like this:
121 |
122 |
123 |
124 | lua -lluacov test.lua
125 |
126 |
127 |
128 | or
129 |
130 |
131 |
132 | lua -erequire('luacov.runner')('myconfigfilename') test.lua
133 |
134 |
135 |
136 | (Alternatively, you can add require("luacov") to the first line
137 | of your script.)
138 |
139 |
140 |
141 | Once the script is run, a file called luacov.stats.out is generated.
142 | If the file already exists, statistics are added to it. This is useful,
143 | for example, for making a series of runs with different input parameters in
144 | a test suite. To start the accounting from scratch, just delete the stats file.
145 |
146 |
147 |
148 | To generate a report, just run the luacov command-line script.
149 | It expects to find a file named luacov.stats.out in the current
150 | directory, and outputs a file named luacov.report.out.
151 |
152 |
153 |
This is an example output of the report file:
154 |
155 |
156 | ==============================================================================
157 | test.lua
158 | ==============================================================================
159 | 1 if 10 > 100 then
160 | *0 print("I don't think this line will execute.")
161 | else
162 | 1 print("Hello, LuaCov!")
163 | end
164 |
165 |
166 |
167 | Note that to generate this report, luacov reads the source files.
168 | Therefore, it expects to find them in the same location they were when
169 | the luacov module ran (the stats file stores the filenames, but
170 | not the sources themselves).
171 |
172 |
173 |
174 | To silence missed line reporting for a group of lines, place inline options
175 | luacov: disable and luacov: enable in short comments around them:
176 |
177 |
178 |
179 | if SOME_DEBUG_CONDITION_THAT_IS_ALWAYS_FALSE_IN_TESTS then
180 | -- luacov: disable
181 |
182 | -- Lines here are not marked as missed even though they are not covered.
183 |
184 | -- luacov: enable
185 | end
186 |
187 |
188 |
189 | LuaCov saves its stats upon normal program termination. If your program
190 | is a daemon -- in other words, if it does not terminate normally -- you
191 | can use the luacov.tick module or the tick configuration option,
192 | which periodically saves the stats file. For example, to run (on Unix systems)
193 | LuaCov on Xavante,
194 | just modify the first line of xavante_start.lua so it reads:
195 |
196 |
197 |
198 | #!/usr/bin/env lua -lluacov.tick
199 |
200 |
201 | or add this to
.luacov config file:
202 |
203 |
204 | tick = true
205 |
206 |
207 | LuaCov includes several configuration options, which have their defaults
208 | stored in
luacov.defaults module.
209 | These are the global defaults. To use project specific configuration, create a Lua script
210 | setting options as globals or returning a table with some options and store it as
211 |
.luacov in the project directory from where
luacov is being run.
212 | For example, this config informs LuaCov that only
foo module and its submodules
213 | should be covered and that they are located inside
src directory:
214 |
215 |
216 | modules = {
217 | ["foo"] = "src/foo/init.lua",
218 | ["foo.*"] = "src"
219 | }
220 |
221 |
222 |
History
223 |
224 |
225 | - 0.16.0 [Dec 04, 2024]
226 | -
227 |
228 | - HTML reporter
229 | - use $LUACOV_CONFIG as the default config file
230 | - Exclude goto statements and labels from accounting
231 |
232 |
233 |
234 | - 0.15.0 [Feb 15, 2021]
235 | -
236 |
237 | - Lua 5.4 support (without CLuaCov)
238 | - Fixes in the feature for including untested files:
239 |
240 | - paths are correctly normalized
241 | - the stats object format is corrected.
242 | - the include config option is honored
243 |
244 |
245 | - The includeuntestedfiles now accepts either true or a table of files and directories to check
246 |
247 |
248 | - 0.14.0 [Jan 28, 2020]
249 | -
250 |
251 | - Added option to include untested files in the report
252 | - Reduce probability of interrupt errors when running LuaCov in a subprocess
253 |
254 |
255 | - 0.13.0 [May 5, 2018]
256 | -
257 |
258 | - Added luacov: disable and luacov: enable inline options that mark source lines between them as impossible to hit.
259 | - Fixed error when reporing coverage for files with a shebang lines using CLuaCov.
260 |
261 |
262 | - 0.12.0 [June 29, 2016]
263 | -
264 |
265 | - Added support for experimental C extensions (CLuaCov).
266 | - Changed config format: options are now set by assigning to globals, old format (returning a table) is still supported.
267 | - Added tickconfig option, equivalent to using luacov.tick module.
268 | - Fixed coverage data being saved to wrong file when using relative statsfile path and the program running LuaCov changes directories.
269 | - Improved config loading error handling.
270 | - Added :on_file_error() stub method to base reporter class, used for reporting problems when analyzing coverage data related to a file.
271 |
272 |
273 | - 0.11.0 [April 18, 2016]
274 | -
275 |
276 | - Fixed several cases of lines falsely reported as missed.
277 | - Fixed luacov.tick module not working.
278 | - Improved default reporter output format.
279 | - Reduced coverage collection overhead.
280 | - Changed how coverage is saved, it's now possible to start a child Lua process with LuaCov enabled without wrapping the launch in luacov.pause and luacov.resume in the parent.
281 |
- Several minor fixes and improvements.
282 |
283 |
284 | - 0.10.0 [February 9, 2016]
285 | -
286 |
287 | - Added debug_hook() function for use in custom debug hooks.
288 | - Fixed patterns passed as command-line arguments matching too much.
289 | - Fixed order in which module name translations are applied
290 |
291 |
292 | - 0.9.1 [December 7, 2015]
293 | -
294 |
295 | - Fixed error when running LuaCov using Lua 5.1.
296 |
297 |
298 | - 0.9 [December 6, 2015]
299 | -
300 |
301 | - with_luacov() function for covering coroutines created via the C API.
302 | - fix priorities in modules list
303 | - improve coverage analysis/exclusions list
304 | - improve handling of multiline function declarations
305 | - LDoc documentation
306 |
307 |
308 | - 0.8 [September 30, 2015]
309 | -
310 |
311 | - Improved lexer which reduces false positives
312 | - luacov.pause() and luacov.resume() functions
313 | - "modules" option for configuration
314 | - Plus several fixes and code cleanups.
315 |
316 |
317 | - 0.7 [January 12, 2015]
318 | -
319 |
320 | - Improvement in detection of long strings.
321 | - Added "savestepsize" option.
322 | - Fix handling "codefromstring" option.
323 |
324 |
325 | - 0.6 [September 10, 2014]
326 | -
327 |
328 | - Support for custom reporter objects
329 | - Configuration option for processing/skipping strings
330 | - Several fixes: behavior of on_exit, inclusion/exclusions lists, etc.
331 |
332 |
333 | - 0.5 [February 8, 2014]
334 | -
335 |
336 | - Improved performance in reporter module
337 | - More improvements in exclusions list
338 |
339 |
340 | - 0.4 [December 3, 2013]
341 | -
342 |
343 | - Lua 5.2 compatibility fixes
344 | - Several improvements in exclusions list
345 |
346 |
347 | - 0.3 [October 10, 2012]
348 | -
349 |
350 | - Added configuration options and files
351 | - Summary in report
352 | - Improved handling of long strings and comments
353 | - Support for coroutines and os.exit()
354 |
355 |
356 | - 0.2 [April 30, 2009]
357 | -
358 |
359 | - Ignore code loaded from strings.
360 |
361 |
362 | - 0.1 [July 16, 2007]
363 | -
364 |
365 | - Initial release.
366 |
367 |
368 |
369 |
370 |
Credits
371 |
372 |
373 | LuaCov was originally designed and implemented by Hisham Muhammad as
374 | a tool for testing LuaRocks. A number
375 | of people have improved it since: see the Git logs for the full list of
376 | contributors!
377 |
378 |
379 |