Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Using mock library (written by Michael Foord
Lacrima
2/25/2010 10:57:00 AM
Hello!
I use mock library
http://www.voidspace.org.uk/py...
. There is
no user group for the library, so I post in comp.lang.python and hope
that people who use it will help me.
The library allows to patch objects, using patch decorator. Patching
is done only within the scope of the function. So if I have a lot of
tests that need specific object to be patched I have to specify the
same decorator for each test method:
class TestSomething(unittest.TestCase):
@patch('module.Class', spec = True)
def test_method1(self, MockClass):
Class()
self.assertEquals(MockClass.called)
@patch('module.Class', spec = True)
def test_method2(self, MockClass):
Class()
MockClass.assert_called_with('foo')
@patch('module.Class', spec = True)
def test_method3(self, MockClass):
foo = Class()
self.assertRaises(AttributeError, foo.some_method)
# and more ...
So for every test method I always do the same patching! How can I
avoid this?
Thanks in advance.
Sorry if my English isn't proper enough.
With regards,
Maxim.
1 Answer
Peter Otten
2/25/2010 1:26:00 PM
0
Lacrima wrote:
> I use mock library
http://www.voidspace.org.uk/py...
. There is
> no user group for the library, so I post in comp.lang.python and hope
> that people who use it will help me.
>
> The library allows to patch objects, using patch decorator. Patching
> is done only within the scope of the function. So if I have a lot of
> tests that need specific object to be patched I have to specify the
> same decorator for each test method:
>
> class TestSomething(unittest.TestCase):
>
> @patch('module.Class', spec = True)
> def test_method1(self, MockClass):
> Class()
> self.assertEquals(MockClass.called)
>
> @patch('module.Class', spec = True)
> def test_method2(self, MockClass):
> Class()
> MockClass.assert_called_with('foo')
>
> @patch('module.Class', spec = True)
> def test_method3(self, MockClass):
> foo = Class()
> self.assertRaises(AttributeError, foo.some_method)
>
> # and more ...
>
> So for every test method I always do the same patching! How can I
> avoid this?
I don't know mock, but the following is generic and should continue to work
when you replace the faux patch() function with the one from the mock
module.
import unittest
def patch(v):
def patch(f):
print "%s-patching" % v, f.__name__
return f
return patch
def make_postprocess(*args, **kw):
def postprocess(name, bases, classdict):
for k, v in classdict.items():
if k.startswith("test_"):
classdict[k] = patch(*args, **kw)(v)
return type(name, bases, classdict)
return postprocess
class A(unittest.TestCase):
__metaclass__ = make_postprocess("Y")
def test_bar(self):
print "bar"
def test_baz(self):
print "baz"
def foo(self):
pass
if __name__ == "__main__":
unittest.main()
Peter
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Using mock library (written by Michael Foord
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password