On 11/11/2013 11:46 PM, Rick Johnson wrote:
No, Python modules can be poked, prodded, and violated by
any pervert who can spell the word "import".
Or by clever programmers.
Attribute values can be reassigned and state can be
externally manipulated
Perhaps for good reasons.
resulting in all types of undefined behaviors
Not necessarily. Manipulation can also eliminate undefined behaviors.
Suppose I want to test a Idle function that uses a tkinter.messagebox
class to communicate with users. Perhaps the module has
from tkinter.messagebox import askretrycancel
The behavior of askretrycancel(*args) is undefined insofar as it depends
on the arbitrary actions of a human user (it may even hang forever). In
an automated test, there is no user, and it is difficult to simulate the
eyes and fingers of one while leaving askretrycancel as it is.
Monkeypatching with a mock solves this. Simplifying a bit, and leaving
out details, I have done something like this.
from mock_tkinter import mbox # mocks all messageboxes
mock_arc = mbox()
import mod
mod.askretrycancel = mock_arc
This makes mod.askretrycancel deterministic in that I can preload a
response into mock_arc before calling the function or method in mod.
(This simulates a user's fingers.) The mock also saves the values sent
to it to see if they are what they should be. (This simulates a user's
eyes.)
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list