Software / code / prosody
Comparison
tests/test.lua @ 361:a2d83b04d769
Update unit testing to output coverage reports
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 20 Nov 2008 21:02:49 +0000 |
| parent | 337:4a1dd1c2c219 |
| child | 370:9ade55e059ea |
comparison
equal
deleted
inserted
replaced
| 360:e918c979ad1a | 361:a2d83b04d769 |
|---|---|
| 1 | |
| 2 function run_all_tests() | |
| 3 dotest "util.jid" | |
| 4 dotest "core.stanza_router" | |
| 5 dotest "core.s2smanager" | |
| 6 dotest "core.configmanager" | |
| 7 end | |
| 1 | 8 |
| 2 local verbosity = tonumber(arg[1]) or 2; | 9 local verbosity = tonumber(arg[1]) or 2; |
| 3 | 10 |
| 4 package.path = package.path..";../?.lua"; | 11 package.path = package.path..";../?.lua"; |
| 5 | 12 |
| 34 return; | 41 return; |
| 35 end | 42 end |
| 36 | 43 |
| 37 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) }); | 44 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) }); |
| 38 | 45 |
| 39 local chunk, err = loadfile("../"..unitname:gsub("%.", "/")..".lua"); | 46 local fn = "../"..unitname:gsub("%.", "/")..".lua"; |
| 47 local chunk, err = loadfile(fn); | |
| 40 if not chunk then | 48 if not chunk then |
| 41 print("WARNING: ", "Failed to load module: "..unitname, err); | 49 print("WARNING: ", "Failed to load module: "..unitname, err); |
| 42 return; | 50 return; |
| 43 end | 51 end |
| 44 | 52 |
| 48 print("WARNING: ", "Failed to initialise module: "..unitname, err); | 56 print("WARNING: ", "Failed to initialise module: "..unitname, err); |
| 49 return; | 57 return; |
| 50 end | 58 end |
| 51 | 59 |
| 52 for name, f in pairs(unit) do | 60 for name, f in pairs(unit) do |
| 61 local test = rawget(tests, name); | |
| 53 if type(f) ~= "function" then | 62 if type(f) ~= "function" then |
| 54 if verbosity >= 3 then | 63 if verbosity >= 3 then |
| 55 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function"); | 64 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function"); |
| 56 end | 65 end |
| 57 elseif type(tests[name]) ~= "function" then | 66 elseif type(test) ~= "function" then |
| 58 if verbosity >= 1 then | 67 if verbosity >= 1 then |
| 59 print("WARNING: ", unitname.."."..name.." has no test!"); | 68 print("WARNING: ", unitname.."."..name.." has no test!"); |
| 60 end | 69 end |
| 61 else | 70 else |
| 62 local success, ret = pcall(tests[name], f, unit); | 71 local line_hook, line_info = new_line_coverage_monitor(fn); |
| 72 debug.sethook(line_hook, "l") | |
| 73 local success, ret = pcall(test, f, unit); | |
| 74 debug.sethook(); | |
| 63 if not success then | 75 if not success then |
| 64 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]"); | 76 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]"); |
| 65 print(" Location: "..ret:gsub(":%s*\n", "\n")); | 77 print(" Location: "..ret:gsub(":%s*\n", "\n")); |
| 78 line_info(name, false); | |
| 66 elseif verbosity >= 2 then | 79 elseif verbosity >= 2 then |
| 67 print("TEST SUCCEEDED: ", unitname, name); | 80 print("TEST SUCCEEDED: ", unitname, name); |
| 81 print(string.format("TEST COVERED %d/%d lines", line_info(name, true))); | |
| 82 else | |
| 83 line_info(name, success); | |
| 68 end | 84 end |
| 69 end | 85 end |
| 70 end | 86 end |
| 71 end | 87 end |
| 72 | 88 |
| 79 print("SUBTEST FAILED: "..(msg or "(no description)")); | 95 print("SUBTEST FAILED: "..(msg or "(no description)")); |
| 80 error(ret, 0); | 96 error(ret, 0); |
| 81 end | 97 end |
| 82 end | 98 end |
| 83 | 99 |
| 84 dotest "util.jid" | 100 function new_line_coverage_monitor(file) |
| 85 dotest "core.stanza_router" | 101 local lines_hit, funcs_hit = {}, {}; |
| 86 dotest "core.s2smanager" | 102 local total_lines, covered_lines = 0, 0; |
| 103 | |
| 104 for line in io.lines(file) do | |
| 105 total_lines = total_lines + 1; | |
| 106 end | |
| 107 | |
| 108 return function (event, line) -- Line hook | |
| 109 if not lines_hit[line] then | |
| 110 local info = debug.getinfo(2, "fSL") | |
| 111 if not info.source:find(file) then return; end | |
| 112 if not funcs_hit[info.func] and info.activelines then | |
| 113 funcs_hit[info.func] = true; | |
| 114 for line in pairs(info.activelines) do | |
| 115 lines_hit[line] = false; -- Marks it as hittable, but not hit yet | |
| 116 end | |
| 117 end | |
| 118 if lines_hit[line] == false then | |
| 119 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source); | |
| 120 lines_hit[line] = true; | |
| 121 covered_lines = covered_lines + 1; | |
| 122 end | |
| 123 end | |
| 124 end, | |
| 125 function (test_name, success) -- Get info | |
| 126 local fn = file:gsub("^%W*", ""); | |
| 127 local total_active_lines = 0; | |
| 128 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+"); | |
| 129 for line, active in pairs(lines_hit) do | |
| 130 if active ~= nil then total_active_lines = total_active_lines + 1; end | |
| 131 if coverage_file then | |
| 132 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n"); | |
| 133 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end | |
| 134 end | |
| 135 end | |
| 136 if coverage_file then coverage_file:close(); end | |
| 137 return covered_lines, total_active_lines, lines_hit; | |
| 138 end | |
| 139 end | |
| 140 | |
| 141 run_all_tests() |