lunes, 2 de enero de 2012

No more excuses for do not testing: picotest 0.0.1 released

Picotest is a gem which allows to write complete test suites in a few lines, targeted primarily to test separated methods, algorithms or little snippets of code

The first gem release (0.0.1) is only a proof on concept to explore the idea which is to avoid "It's not worth testing this" syndrome. This problem occurs when we develop really small features such as helper functions, calculation private methods, etc... and then we fall in "It's not worth testing this", "because test code will be much greater than code being tested".

The solution offered by picotest, is to provide a really small testing dsl to keep the "proportion" even in smaller cases, using hash syntax and other syntaxic sugar

Examples

For example, if you want to test Math.sqrt function, you could use the following:
require "picotest"

suite(1 => 1, 4 => 2, 9 => 3, 16 => 4).test(Math.method(:sqrt))

There is no need to write all cases one by one, if you known as to calculate the input from the output (the inverse of sqrt), in that cases you could use a reverse oracle
require "picotest"

suite( lambda{|x| x**2} => _set(1,2,3,4,5,6,7,8,9,10)).test(Math.method(:sqrt))
# the following is the same as the above example
suite( lambda{|x| x**2} => _set(*(1..10)).test(Math.method(:sqrt))

And mocking is as follows:
class X
attr_accessor :another_object
def foo(data)
data.split(";")[another_object.fieldno].to_f
end
end

# you can return mock objects using mock method again
s.test X.new.method(:foo).mock(:another_object => mock(:fieldno => 2) )
You can read more examples at https://github.com/tario/picotest/tree/master/samples and in the README

Install

gem install picotest

Links

No hay comentarios:

Publicar un comentario