assert-random

Assert random is a simple gem that extends the test/unit assertions to include an assert-random test. Assert random works like any other assertion and accepts a block of code. Assert random checks this block of code records the results of the output. The out put is then checked for the following…

  • Identical results

  • Sequences of numbers (eg… 1 2 3 4 or… 10 20 30 etc)

Usage(Will pass):

def test_something
   assert_random do
      rand(1000)
   end
end

Usage(Will fail):

def test_something_else
   assert_random do
      1000
   end
end

Tolerance

Random numbers and results are hard to test. If your generating with in a small range of numbers say 1 to 10 then you can't guarantee that the same number will not come up twice. This is despite the generator working correctly. For cases like this assert-random supports the tolerance option. Tolerance allows the test to tolerate more than one instance of a number from a narrow range.

Tolerance Usage:

def test_tolerance
   assert_random :tolerance => 5 do
      rand(10)
   end
end

The above example will pass even if the same number comes up 4 times.

Copyright

Copyright © 2010 Stewart Matheson. See LICENSE for details.