print out test duration, in seconds
[lisp-unit.git] / extensions / test-anything-protocol.lisp
blob09653f86a81bb35b589abb5e6c174e62595c2942
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
2 #|
4 Test Anything Protocol (TAP) support for LISP-UNIT
6 Copyright (c) 2009-2013, Ryan Davis <ryan@acceleration.net>
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
26 References
27 [TAP]: http://testanything.org/wiki/index.php/Main_Page
31 (in-package :lisp-unit)
33 ;;; Symbols exported from the TAP extension
35 (export '(write-tap write-tap-to-file))
37 (defun run-time-s (test-result)
38 "calculate the run-time of the test in seconds"
39 (/ (run-time test-result)
40 internal-time-units-per-second))
42 (defun %write-tap-test-result (name test-result i stream)
43 "Output a single test, taking care to ensure the indentation level
44 is the same before and after invocation."
45 (pprint-logical-block (stream nil)
46 (format stream
47 "~:[ok~;not ok~] ~d ~s (~,2f s)"
48 (or (fail test-result)
49 (exerr test-result))
50 i name
51 (run-time-s test-result))
52 (when (or (fail test-result)
53 (exerr test-result))
54 ;; indent only takes affect after a newline, so force one
55 (format stream "~2I~:@_---~@:_")
56 (when (exerr test-result)
57 (format stream "message: |~4I~_~s~2I~@:_" (exerr test-result)))
58 (when (fail test-result)
59 (format stream "message: ~d failed assertions~@:_"
60 (length (fail test-result))))
61 (format stream "..."))
62 ;; always reset to zero and force a newline
63 (format stream "~0I~@:_")))
65 (defun write-tap (test-results &optional (stream *standard-output*))
66 "Write the test results to `stream` in TAP format. Returns the test
67 results."
68 (check-type test-results test-results-db)
69 (let ((i 0)
70 (*print-pretty* T))
71 (format stream "TAP version 13~%1..~d~%"
72 (hash-table-count (database test-results)))
73 (maphash
74 #'(lambda (name test-result)
75 (%write-tap-test-result name test-result (incf i) stream))
76 (database test-results)))
77 test-results)
79 (defun write-tap-to-file (test-results path)
80 "write the test results to `path` in TAP format, overwriting `path`.
81 Returns pathname to the output file"
82 (check-type path (or string pathname))
83 (ensure-directories-exist path)
84 (with-open-file (s path :direction :output :if-exists :supersede)
85 (write-tap test-results s))
86 (truename path))