May 2018
Beginner to intermediate
452 pages
11h 26m
English
The rand_between() method generates a random number between a and b. Because we can't possibly predict its output, we can't provide a fixed value to test it against. How can we test this method?
A naive approach is as follows:
def test_rand_between(self): rv = self.mycalc1_0.rand_between() self.assertLessEqual(rv, 1) self.assertGreaterEqual(rv, 0)
This test passes if our code is correct, but it doesn't necessarily fail if the code is wrong; in fact, if the code is wrong, it may pass or fail unpredictably. For example, if MyCalc(1, 10).rand_between() was incorrectly returning values between 2 and 11, there is only a 10% chance that the test would fail on each run.
We can safely assume that a standard library function ...