Merge lp:~jml/testtools/patch-310770 into lp:~testtools-committers/testtools/trunk

Proposed by Jonathan Lange
Status: Merged
Merged at revision: 89
Proposed branch: lp:~jml/testtools/patch-310770
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 0 lines
To merge this branch: bzr merge lp:~jml/testtools/patch-310770
Reviewer Review Type Date Requested Status
testtools developers Pending
Review via email: mp+31666@code.launchpad.net

Description of the change

More and more I've found myself wanting to monkey-patch code with testtools in the same way I can for Trial. This branch adds support for just such a thing.

To post a comment you must log in.
lp:~jml/testtools/patch-310770 updated
89. By Jonathan Lange

Merge trunk

90. By Jonathan Lange

NEWS.

Revision history for this message
Robert Collins (lifeless) wrote :

A few thoughts.

Firstly, I'd expect this to delete an attribute too, if the original
was unset; this doesn't, but the tests don't make it clear whether
that is oversight or intent.

Secondly, this is so useful, I really would rather not see it on TestCase.

How about

def patch(obj, attribute, value):
    """Set obj.attribute to value and return a cleanup to restore it."""

?

Then you can do
self.addCleanup(patch(sys, "stderr", StringIO())

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

I don't like that.

We have a really nice monkey-patching module in Twisted and it simply never gets used. The only thing that ends up being used is TestCase.patch.

The delattr thing rarely comes up in actual usage, but I agree that it's a good behaviour to have. Will fix that.

Revision history for this message
Robert Collins (lifeless) wrote :

On Wed, Aug 4, 2010 at 10:32 PM, Jonathan Lange <email address hidden> wrote:
> I don't like that.
>
> We have a really nice monkey-patching module in Twisted and it simply never gets used. The only thing that ends up being used is TestCase.patch.

Why does that happen, do you think?

I'd really like others to be able to use this outside of TestCase,
e.g. for fixtures, so I'd _really_ like the core to be
not-on-TestCase, it has no justification to be there; having glue for
it on TestCase I'm ambivalent but not keen on, but ok with as long as
it really is a one-liner.

-Rob

lp:~jml/testtools/patch-310770 updated
91. By Jonathan Lange

Handle cases where attributes don't exist.

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

It doesn't happen because the need for it is small, and it's almost always easier to write it yourself than add the import and re-use code.

Still, if you're OK with a TestCase.patch() convenience function I can dredge up the external library I added in previous revisions and make TestCase.patch() a one-liner.

Revision history for this message
Robert Collins (lifeless) wrote :

> Still, if you're OK with a TestCase.patch() convenience function I can dredge up the external library I added in previous revisions and make TestCase.patch() a one-liner.

+1

lp:~jml/testtools/patch-310770 updated
92. By Jonathan Lange

Restore monkey code.

93. By Jonathan Lange

Handle missing attributes better.

94. By Jonathan Lange

Not necessary

95. By Jonathan Lange

Add a convenience function to just patch a single thing.

96. By Jonathan Lange

Make TestCase.patch() use the new patch() helper method. Becomes slightly
less useful but more pure.

Preview Diff

Empty

Subscribers

People subscribed via source and target branches