A lightweight (but tricky) random testing tool for Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


RushCheck library - a random test library for ruby

RushCheck library is a random testing library/tools for ruby which are imported from QuickCheck library in Haskell.

QuickCheck has been developed by Koen Claessen and John Hughes in 2000.


Check out directories both data/rushcheck/doc and rdoc for details.

Current status is alpha

Less documents/examples, no gem. There is a gem version 0.8, but, it is not recommended to install because we will change the API.

An example

Let's see data/examples/sample.rb The first example is here:

require 'rushcheck'

# assert_sort_one should be true
def assert_sort_one
  RushCheck::Assertion.new(Integer) { |x|
    [x].sort == [x]

We will check that for all integer x, the array [x] does not changed after it is sorted. Here is a session with irb: (not installed, but downloaded the source code from the repository)

% irb -I lib -I data/examples -r sample
irb(main):001:0> assert_sort_one
OK, passed 100 tests.
==> true

The above test passed as expected. Another example when we will meet a bug in the code. The property that sorting does not change arrays is explicitly false.

# however, assert_sort_two is not true generally,
# and RushCheck finds a counter example.
def assert_sort_two
  RushCheck::Assertion.new(Integer, Integer) { |x, y|
    ary = [x, y]
    ary.sort == ary

Continue the irb session:

irb(main):002:0> assert_sort_two
Falsifiable, after 2 tests:
[2, 0]
=> false

When x <= y in the test, the sorted array [x, y].sort equals the original array [x, y]. However, if x > y then the sorted array differs the original. We see the counter example [2, 0] thanks to random testing.

What we have studied from the above two examples: The method RushCheck::Assertion.new takes classes as variables, and has a block with the binded variables respectively; The block should return true or false; Finally the method check executes the random testing and shows the result.

More further

Specification of the program should be provided as properties. In each property, we write what has to be satisfied. RushCheck helps to test the properties with 100 (as the default) tests by random generated test cases. We can change the number of tests easily as follows;

# 1000 tests instead
c = RushCheck::Config.new(1000)
RushCheck::Assertion(Integer) { |x|
... # the property of the test

The block of the property should be return either true or false. Therefore the assertion method in Test/Unit should not be written in the block because this returns nil object.

# NG case
RushCheck::Assertion(Integer) { |x|
  assertion_equal x+x, 2*x # NG because this returns nil

On the other hand, the check method in RushCheck returns either true or false if the test does not throw any exception. Then we can combine RushCheck and Test/Unit like this;

# OK
assertion_equal true,
  RushCheck::Assertion(Integer) { |x|
    x+x == 2*x

Contributing to rushcheck

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright © 2006-2011 Daisuke IKEGAMI. See LICENSE.txt (The MIT license) for further details.

Project webpage rushcheck.rubyforge.org

Git repository github.com/IKEGAMIDaisuke/rushcheck