Add patch() functionality

Bug #310770 reported by Jonathan Lange
2
Affects Status Importance Assigned to Milestone
testtools
Fix Released
Wishlist
Jonathan Lange

Bug Description

Twisted has a patch() method on TestCase that lets you easily monkey-patch existing attributes on objects. This would be very nice to have in testtools.

Changed in testtools:
status: New → Confirmed
importance: Undecided → Medium
Changed in testtools:
importance: Medium → Wishlist
Revision history for this message
Robert Collins (lifeless) wrote :

Jono; this seems like something we shouldn't have in testtools. Rather:
There should be a library for monkey patching.
It should have an API we can trivially use (e.g. it returns (callback, (args), {kwargs}), which users can pass to addCleanup:

def newfoo():
    pass
self.addCleanup(monkey.patch(sys.time, newfoo))

What do you think?

Changed in testtools:
status: Confirmed → Incomplete
Revision history for this message
Jonathan Lange (jml) wrote :

I don't feel strongly about it.

'TestCase.patch' has proven itself to be very convenient and readable in Twisted.
    self.patch(sys, 'time', newfoo)
is more readable than:
    self.addCleanup(monkey.patch, sys, 'time', newfoo)
or even
    self.addCleanup(monkey.patch(sys.time, newfoo))

And if monkey.patch can be implemented in the last way (I'm not so sure), then TestCase.patch could be written as:
    self.patch(sys.time, newfoo)

Which makes the intent very clear.

Revision history for this message
Jonathan Lange (jml) wrote :

I've changed my mind. I *do* feel strongly about it. This is such a useful, basic thing that it should be included in testtools.

Changed in testtools:
status: Incomplete → In Progress
assignee: nobody → Jonathan Lange (jml)
Changed in testtools:
status: In Progress → Fix Released
milestone: none → next
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.