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.
Iterations¶ ↑
You can now set the amount of times that the passed block will iterate. This is handy when testing numbers from different ranges. It is used like so. Warning: assert random will throw exceptions if you pass 1. There is no point in only iterating once.
def test_iterations assert_random :iterations => 2 do rand(10) end end
Copyright¶ ↑
Copyright © 2010 Stewart Matheson. See LICENSE for details.