Gen::Test
Generative, property based testing for Ruby
Synopsis
AddOne = lambda { |x| x + 1 }
class AddOneTest < Minitest::Test
include Gen::Test
def test_incremented_for_all_integers
for_all Integer do |int|
inc = AddOne[int]
assert_equal(inc, int + 1)
end
end
end
Why?
Generative or property based testing is an approach to testing where tests are automatically generated from definitions for properties of the system being tested. While slower, generative testing has much value for unit, acceptance and integration testing over TDD and BDD.
For example, generative tests still have much value when they're written after your code, and when extensive can be quite useful for integration testing.
There seemed to be a need for a simple, extensible, implementation for Ruby, that isn't a DSL or a framework. This module strives to serve that purpose, it's still very incomplete, but it's proven useful.
You can use Gen::Test along side other tests written in a different style and it's designed to be used with other testing frameworks like Minitest and Rspec.
It's also extensible, defining a simple protocol for extensiblity. Just define a generate
method that can
be called with no arguments and returns your generated data on any object and it can be used as a generator.
Many core objects have already been extended (see lib/ext_core.rb). You can also make use
of Gen::Generator
which is a Proc-like object that implements the generate
method.
Install
> gem install gen-test
or, add
gem 'gen-test'
to your Gemfile.
Extentions
- Contracts::Gen, defines generators for most of contracts the Contracts gem.
See Also
- For Clojure test.check
- For Haskell (the original version) QuickCheck
- For Erlang (the most complete version) QuickCheck
- There's quite a few different versions for Ruby