Browse files

add support for exitstatus==percent_failed and for simple samples ret…

…urning results
  • Loading branch information...
1 parent b6dee8b commit 4dbd52180a16a4e0f04bb9a5c2db2e5507589a60 @ahoward committed Mar 29, 2009
Showing with 71 additions and 5 deletions.
  1. +2 −1 README.tmpl
  2. +2 −0 TODO
  3. +8 −1 lib/testy.rb
  4. +3 −3 samples/b.rb
  5. +56 −0 samples/c.rb
View
3 README.tmpl
@@ -40,7 +40,8 @@ PRINCIPLES AND GOALS
. hi-jacking at_exit sucks ass
. the exit status of running a test suite should indicate the degree of it's
- failure state: the more failures the higher the exit status
+ failure state (testy returns the percent of failed tests using a non-zero
+ exit status)
. sample code should easily be able to double as a test suite, including
it's output
View
2 TODO
@@ -0,0 +1,2 @@
+. add contexts
+. add setup/teardown
View
9 lib/testy.rb
@@ -58,6 +58,10 @@ def test(name, &block)
@tests[name.to_s] = block
end
+ def size
+ @tests.size
+ end
+
def list
instance_eval(&@block) if @block
tests.map{|kv| kv.first.to_s}
@@ -78,6 +82,7 @@ def run(*args)
value = block.call(result)
raise BadResult, name unless result.ok?
value = result.actual.with_string_keys unless result.empty?
+ begin; value.to_yaml; rescue; value=true; end
{'success' => value}
rescue Object => e
failures += 1
@@ -107,7 +112,9 @@ def Testy.testing(*args, &block)
y test.list
else
failures = test.run(:selectors => selectors)
- exit(failures)
+ size = test.size
+ pct_failed = [ ((failures/size.to_f)*100).to_i, 1 ].max
+ exit(pct_failed)
end
end
end
View
6 samples/b.rb
@@ -1,9 +1,9 @@
# testy will handle unexpected results and exceptions thrown in your code in
# exactly the same way - by reporting on them in a beautiful fashion and
# continuing to run other tests. notice, however, that an unexpected result
-# or raised exception will cause a non-zero exitstatus (equalling the number
-# of failed tests) for the suite as a whole. also note that previously
-# accumulate expect/actual pairs are still reported on in the error report.
+# or raised exception will cause a non-zero exitstatus (the percent of tests
+# that failed) for the suite as a whole. also note that previously
+# accumulated expect/actual pairs are still reported on in the error report.
#
require 'testy'
View
56 samples/c.rb
@@ -0,0 +1,56 @@
+# in some cases you may not even want to make test assertions and simply
+# provide example code which should run without error, with testy it's not
+# only easy to do this but the commanline supports --list to see which samples
+# can be run and running a single or multiple tests based on name or pattern.
+# notice that, when no assertions are made using 'result.check' the output of
+# the test becomes the expected *and* actual and is therefore shown in the
+# output as yaml. of course, if your samples have errors they are reported in
+# the normal fashion and the tests will fail.
+#
+#
+ require 'testy'
+
+ Testy.testing 'some samplz for you' do
+ test 'foo sample' do
+ list = 1,2
+ list.last + list.first
+ end
+
+ test 'bar sample' do
+ list = 1,2
+ list.shift * list.pop
+ end
+
+ test 'foobar sample' do
+ list = 1,2
+ eval(list.reverse.join) * 2
+ end
+ end
+
+# here are some examples of using the command line arguments on the above test
+#
+# cfp:~/src/git/testy > ruby samples/c.rb --list
+# ---
+# - foo sample
+# - bar sample
+# - foobar sample
+#
+# cfp:~/src/git/testy > ruby samples/c.rb foobar
+# ---
+# some samplz for you:
+# foobar sample:
+# success: 42
+#
+# cfp:~/src/git/testy > ruby samples/c.rb foo
+# ---
+# some samplz for you:
+# foo sample:
+# success: 3
+# foobar sample:
+# success: 42
+#
+# cfp:~/src/git/testy > ruby samples/c.rb bar
+# ---
+# some samplz for you:
+# bar sample:
+# success: 2

0 comments on commit 4dbd521

Please sign in to comment.