[TIP] newbie - verifying the type of a basic property set

Charles Medcoff cmedcoff at hotmail.com
Mon Jul 8 08:53:16 PDT 2013


The first test is what I would call a State based test without using any
mocking library.  The second using arrange, act, assert using mock.  Works
but seems clunky.  Is there a cleaner, more intuitive way?  setBar
represents what might be some method that has logic to it and determine what
(sub) type to do in an assignment.

 

import unittest

from mock import Mock, PropertyMock

class Foo(object):

               def setBar(self, bar):

                              self.bar = bar

class Bar(object):

               pass

 

class MyTestCase(unittest.TestCase):

               def
test_setBarOnFooResultsInCorrectType_StateBasedTesting(self):

                              foo = Foo()

                              foo.setBar(Bar())

                              self.assertEqual(type(foo.bar), type(Bar()))

               def test_setBarOnFooResultsInCorrectType_UsingMock(self):

                              foo = Foo()

                              mockBar = PropertyMock()

                              type(foo).bar = mockBar

                              foo.setBar(Bar())

                              mockBar.assert_called_once()

                              arg, junk = mockBar.call_args

                              self.assertEqual(type(arg[0]), type(Bar()))

 

if __name__ == '__main__':

               unittest.main()

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 6410 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130708/a58c1121/attachment.bin>


More information about the testing-in-python mailing list