Skip to Content
Python GUI Programming with Tkinter
book

Python GUI Programming with Tkinter

by Alan D. Moore
May 2018
Beginner to intermediate content levelBeginner to intermediate
452 pages
11h 26m
English
Packt Publishing
Content preview from Python GUI Programming with Tkinter

Using Mock and patch

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python GUI Programming with Tkinter - Second Edition

Python GUI Programming with Tkinter - Second Edition

Alan D. Moore

Publisher Resources

ISBN: 9781788835886Supplemental Content