Author eric.araujo
Recipients eric.araujo, ezio.melotti, michael.foord
Date 2011-03-24.20:43:59
SpamBayes Score 1.49791e-10
Marked as misclassified No
Message-id <1300999439.71.0.376555102459.issue11664@psf.upfronthosting.co.za>
In-reply-to
Content
A common thing to do in setUp or test* methods is to replace some module attribute with something else, either to mock an object calling an external resource or to test platform-specific behavior (for example, changing os.name before calling some function).  Care has to be taken to restore the initial object with addCleanup, tearDown or in a finally block.

I propose that a new method TestCase.patch (inspired by mock.patch, but more limited in scope) be added, to allow such usages (each example is standalone):

  def setUp(self):
      self.patch(socket, 'socket', MockSocket)

  def test_default_format(self):
      self.patch(os, 'name', 'posix')
      self.assertEqual(get_default_format(), '.tar.gz')
      self.path(os, 'name', 'nt')
      self.assertEqual(get_default_format(), '.zip')

  def setUp(self):
      self.patch(sys, 'path', sys.path.copy())

In each example, patch(object, attribute, value) does this: save object.attribute, set object.attribute to value, register a cleanup function to restore object.attribute.

I assigned to Michael so that he can kill this idea early if he has reason to do so.  If not, please move stage to “patch needed” (no pun).  I am willing to work on a patch for 3.3 and unittest2 (not sure which is first :)
History
Date User Action Args
2011-03-24 20:43:59eric.araujosetrecipients: + eric.araujo, ezio.melotti, michael.foord
2011-03-24 20:43:59eric.araujosetmessageid: <1300999439.71.0.376555102459.issue11664@psf.upfronthosting.co.za>
2011-03-24 20:43:59eric.araujolinkissue11664 messages
2011-03-24 20:43:59eric.araujocreate