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

John Wong gokoproject at gmail.com
Mon Jul 8 09:45:32 PDT 2013


It depends on how much work you are doing inside setBar method.

If you are simply setting self.bar to the input object, I don't see any
reason to do any mocking. Mocking is useful is you have more than a simple
assignment inside the method. If the logic is simple enough, doing the
first one is enough.

Is that the template for the real code? Also, If you have to paste code
into your mesage, make sure it is formmated such that you are not using
tabs (8-space). Use 4-space.




On Mon, Jul 8, 2013 at 8:53 AM, Charles Medcoff <cmedcoff at hotmail.com>wrote:

> 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()
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130708/2b91d1f3/attachment.htm>


More information about the testing-in-python mailing list