lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

Attached is a patch that adds output handlers to lunit.  So far I
wrote a console handler (the same output that lunit has now) and an
HTML handler.  To change handlers, try:

   lunit.run({output="html"})

If anyone wants to do XML output or make the HTML output nicer, feel free.

-- 
Zachary P. Landau <kapheine@gmail.com>
diff -urN lunit-0.3alpha.orig/lunit-console.lua lunit-0.3alpha/lunit-console.lua
--- lunit-0.3alpha.orig/lunit-console.lua	1969-12-31 19:00:00.000000000 -0500
+++ lunit-0.3alpha/lunit-console.lua	2005-07-30 15:46:39.567619104 -0400
@@ -0,0 +1,43 @@
+
+local console = {}
+
+function console.start_test_suite(stats)
+    print()
+    print("#### Test Suite with "..stats.tests.." Tests in "..stats.testcases.." Test Cases loaded.")
+end
+
+function console.end_test_suite(stats)
+    print()
+    print("#### Test Suite finished.")
+
+    local msg_assertions = stats.assertions.." Assertions checked. "
+    local msg_passed     = stats.passed == stats.tests and "All Tests passed" or  stats.passed.." Tests passed"
+    local msg_failed     = stats.failed > 0 and ", "..stats.failed.." failed" or ""
+    local msg_run        = stats.notrun > 0 and ", "..stats.notrun.." not run" or ""
+
+    print()
+    print(msg_assertions..msg_passed..msg_failed..msg_run.."!")
+end
+
+function console.start_testcase(tc)
+    print()
+    print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...")
+end
+
+
+function console.end_testcase(tc)
+end
+
+function console.skip_test(tc, testname)
+    print("WARN: Skipping '"..testname.."'...")
+end
+
+function console.ok_test(tc, testname)
+end
+
+function console.error_test(tc, testname, msg)
+    print()
+    print("FAIL: " .. testname ..": " ..msg)
+end
+
+return console
diff -urN lunit-0.3alpha.orig/lunit-html.lua lunit-0.3alpha/lunit-html.lua
--- lunit-0.3alpha.orig/lunit-html.lua	1969-12-31 19:00:00.000000000 -0500
+++ lunit-0.3alpha/lunit-html.lua	2005-07-30 15:46:30.742960656 -0400
@@ -0,0 +1,41 @@
+
+local html = {}
+
+function html.start_test_suite(stats)
+    print("<html><head><title>Test Results</title></head><body>")
+    print("<h1>Test Suite with "..stats.tests.." Tests in "..stats.testcases.." Test Cases</h1>")
+end
+
+function html.end_test_suite(stats)
+    local msg_assertions = stats.assertions.." Assertions checked. "
+    local msg_passed     = stats.passed == stats.tests and "All Tests passed" or  stats.passed.." Tests passed"
+    local msg_failed     = stats.failed > 0 and ", "..stats.failed.." failed" or ""
+    local msg_run        = stats.notrun > 0 and ", "..stats.notrun.." not run" or ""
+
+    print("</table>")
+    print("<h1>"..msg_assertions..msg_passed..msg_failed..msg_run.."!</h1>")
+    print("</body></html>")
+end
+
+function html.start_testcase(tc)
+    print("<h2>"..tc.__lunit_name..": " .. table.getn(tc.__lunit_tests) .. " tests<h2>")
+    print("<table><tr><th>Test</th><th>Result</th></tr>")
+end
+
+function html.end_testcase(tc)
+    print("</table>")
+end
+
+function html.skip_test(tc, name)
+    print("<tr><td>" .. name .. "</td><td>Skipped</td></tr>")
+end
+
+function html.ok_test(tc, name)
+    print("<tr><td>" .. name .. "</td><td>Passed</td></tr>")
+end
+
+function html.error_test(tc, name, msg)
+    print("<tr><td>" .. name .. "</td><td>Failed</td></tr>")
+end
+
+return html
diff -urN lunit-0.3alpha.orig/lunit.lua lunit-0.3alpha/lunit.lua
--- lunit-0.3alpha.orig/lunit.lua	2004-08-22 12:20:38.000000000 -0400
+++ lunit-0.3alpha/lunit.lua	2005-07-30 15:50:07.794963736 -0400
@@ -57,7 +57,7 @@
 local getfenv = getfenv
 local setfenv = setfenv
 local tostring = tostring
-
+local require = require
 
 -- Start package scope
 setfenv(1, P)
@@ -432,8 +432,13 @@
 -- Runs the complete Test Suite --
 ----------------------------------
 
-function run()
+local output
+function run(opts)
   
+  opts = opts or {}
+  local output_type = opts.output or "console"
+  output = assert(require("lunit-" .. output_type))
+
   ---------------------------
   -- Initialize statistics --
   ---------------------------
@@ -460,8 +465,7 @@
   -- Print Header --
   ------------------
   
-  print()
-  print("#### Test Suite with "..stats.tests.." Tests in "..stats.testcases.." Test Cases loaded.")
+  output.start_test_suite(stats)
   
   ------------------------
   -- Run all Test Cases --
@@ -475,16 +479,7 @@
   -- Print Footer --
   ------------------
   
-  print()
-  print("#### Test Suite finished.")
-  
-  local msg_assertions = stats.assertions.." Assertions checked. "
-  local msg_passed     = stats.passed == stats.tests and "All Tests passed" or  stats.passed.." Tests passed"
-  local msg_failed     = stats.failed > 0 and ", "..stats.failed.." failed" or ""
-  local msg_run	       = stats.notrun > 0 and ", "..stats.notrun.." not run" or ""
-  
-  print()
-  print(msg_assertions..msg_passed..msg_failed..msg_run.."!")
+  output.end_test_suite(stats)
   
   -----------------
   -- Return code --
@@ -520,11 +515,7 @@
     orig_assert(is_string(errprefix))
     orig_assert(is_function(func))
     local ok, errmsg = xpcall(function() func(tc) end, traceback)
-    if not ok then
-      print()
-      print(errprefix..": "..errmsg)
-    end
-    return ok
+    return ok, errmsg
   end
   
   ------------------------------------
@@ -546,11 +537,13 @@
   local function run(testname)
     orig_assert(is_string(testname))
     orig_assert(is_function(tc[testname]))
-    local ok = call("FAIL: "..testname, tc[testname])
+    local ok, err = call("FAIL: "..testname, tc[testname])
     if not ok then
       stats_inc("failed")
+      output.error_test(tc, testname, err)
     else
       stats_inc("passed")
+      output.ok_test(tc, testname)
     end
     return ok
   end
@@ -569,8 +562,7 @@
   -- Run all Tests on a TestCase --
   ---------------------------------
   
-  print()
-  print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...")
+  output.start_testcase(tc)
   
   for _, testname in ipairs(tc.__lunit_tests) do
     if setup() then
@@ -578,10 +570,12 @@
       stats_inc("run")
       teardown()
     else
-      print("WARN: Skipping '"..testname.."'...")
+      output.skip_test(tc, testname)
       stats_inc("notrun")
     end
   end
+
+  output.end_testcase(tc)
   
 end