1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Shake - A Simple Lua Test Engine</title>
<link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo">
<a href="http://shake.luaforge.net">
<img alt="Shake Logo" src="shake.png"/>
</a>
</div>
<div id="product_name"><big><strong>Shake</strong></big></div>
<div id="product_description">A Simple Lua Test Engine</div>
</div> <!-- id="product" -->
<div id="main">
<div id="navigation">
<h1>Shake</h1>
<ul>
<li><a href="index.html">Home</a>
<ul>
<li><a href="index.html#overview">Overview</a></li>
<li><a href="index.html#status">Status</a></li>
<li><a href="index.html#download">Download</a></li>
<li><a href="index.html#dependencies">Dependencies</a></li>
<li><a href="index.html#history">History</a></li>
<li><a href="index.html#credits">Credits</a></li>
<li><a href="index.html#contact">Contact us</a></li>
</ul>
</li>
<li><a href="manual.html">Manual</a>
<ul>
<li><a href="manual.html#introduction">Introduction</a></li>
<li><a href="manual.html#installation">Installation</a></li>
<li><a href="manual.html#reference">Reference</a></li>
</ul>
</li>
<li><strong>Examples</strong></li>
<li><a href="http://luaforge.net/projects/shake/">Project</a>
<ul>
<li><a href="http://luaforge.net/tracker/?group_id=310">Bug Tracker</a></li>
<li><a href="http://luaforge.net/scm/?group_id=310">CVS</a></li>
</ul>
</li>
<li><a href="license.html">License</a></li>
</ul>
</div> <!-- id="navigation" -->
<div id="content">
<h2><a name="example"></a>Examples</h2>
<h3>Running Shake in the command line</h3>
<p>Assuming you have a module like LuaFileSystem installed and
you go to its <code>/tests</code> directory and run Shake from there, the output would be:</p>
<pre class="example">
~/workspace/luafilesystem/tests$ shake
-> test.lua OK!
_________________
Tests: 27
Failures: 0
Errors: 0
</pre>
<p>On the other hand, if you have a test script like the one below that includes two assertions that are supposed to fail
(lines are numbered):</p>
<pre class="example">
1 items = 10
2 -- checks the correct case
3 assert (items == 10, "this should not fail")
4
5 items = 20
6 -- checks an overflow case
7 assert (items == 10, "wrong number of items")
8
9 print("Verifying the total")
10 items = 10
11 total = 30
12 assert (items == total, "wrong total")
</pre>
<p>Shake would register the failures but would run the whole test script, reporting at the end:</p>
<pre class="example">
:~/workspace$ shake
---------------- test.lua failed! ----------------
-- checks an overflow case
#7 assert (items == 10, "wrong number of items")
items -> 20
Verifying the total
#12 assert (items == total, "wrong total")
items -> 10
total -> 30
_________________
Tests: 3
Failures: 2
Errors: 0
</pre>
<p>Note how much more informative this is when compared to the default output of running the test script with Lua:</p>
<pre class="example">
:~/workspace$ lua5.1 test.lua
lua5.1: test.lua:7: wrong number of items
stack traceback:
[C]: in function 'assert'
test.lua:7: in main chunk
[C]: ?
</pre>
<h3>Implementing a simple Shake runner</h3>
<p>Here we show how to use the Shake API to implement runners. Note that if you just want to
execute tests, the built in runners (command line and CGILua app) may be enough. On the other hand,
if you need to show more detailed information about the tests results or if you want to use Shake as part
of your application runtime, then you will need to use the Shake API.</p>
<p>The minimal Shake runner would be like:</p>
<pre class="example">
require "shake"
local run = shake.runner()
run:test("somefile.lua")
print (run:summary())
</pre>
<p>This would be basically equivalent to the command line Shake runner, and if you want to report more
details than the default summary does, you will need to drill down the results using something like:</p>
<pre class="example">
function ReportModules(run)
local results = run.results
for cs, suite in ipairs(results.suites) do
-- displays information about the suite.title
if suite.error then
-- displays information about the suite error
elseif suite.failed > 0 then
-- displays information about the suite.failed results
else
-- displays information about the suite.passed and suite.failed results
end
end
end
</pre>
<p>For even more details, you may want to inspect each test result and decide what to show:</p>
<pre class="example">
function ReportModule(run)
local results = run.results
for _, suite in ipairs(results.suites) do
if suite.error == -1 then
-- displays information about the error
else
for _, context in ipairs(suite.contexts) do
if next(context.tests) then
if context.output[1] ~= "" or context.comments then
-- displays information about the context
for _, output in ipairs(context.output) do
if output and output ~= "" then
-- using context.output
end
end
if context.comments and context.comments ~= "" then
-- or context.comments
end
end
for _, test in ipairs (context.tests) do
local linenumber = test.linenumber or "???"
local op = test.op
local val2 = tostring(test.val2)
-- when there is no comparision operator, assume that this is an assert(x) case
if not op then
val2 = "True value" -- just to diferentiate from the Lua "true"
end
if not op or op == "==" then
op = ""
end
-- displays information about the test result using
-- linenumber, test.exp1, op, val2, test.val1 and test.msg
end
end
end
end
</pre>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<p><a href="http://validator.w3.org/check?uri=referer">Valid XHTML 1.0!</a></p>
<p><small>$Id: examples.html,v 1.5 2007/12/21 22:55:20 carregal Exp $</small></p>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
|