给出如下代码:
import flask
import time
app = flask.Flask(__name__)
def authorize():
print('starting authorize io')
time.sleep(1)
print('done authorize io')
class BlockingIo():
def __init__(self, n):
self.n = n
def do(self):
print('starting blocking io')
time.sleep(1)
print('ending blocking io')
@app.route('/', methods=['GET'])
@app.route('/<int:n>/', methods=['GET'])
def foo(n=1):
authorize()
b = BlockingIo(n)
b.do()
return str(n), 200
#app.run(port=5000)
我希望能够为GET /n/
编写多个测试,每个测试都模拟authorize
和BlockingIO(n)
:
app.testing = True
testapp = app.test_client()
import unittest
from unittest import mock
mock.patch('__main__.authorize')
class TestBlockingIo(unittest.TestCase):
@mock.patch('__main__.authorize')
@mock.patch('__main__.BlockingIo.do')
def test_1(self, m, m2):
r = testapp.get('/1/')
self.assertEquals(r.data, b'1')
@mock.patch('__main__.authorize')
@mock.patch('__main__.BlockingIo.do')
def test_2(self, m, m2):
r = testapp.get('/2/')
self.assertEquals(r.data, b'2')
unittest.main()
但是,我不想一遍又一遍地写出@mock.patch
装饰器。
我知道我们可以使用类装饰器,并且可以将其子类化以提高可重用性:
@mock.patch('__main__.authorize')
@mock.patch('__main__.BlockingIo.do')
class TestBlockingIo(unittest.TestCase):
def test_1(self, m, m2):
r = testapp.get('/1/')
self.assertEquals(r.data, b'1')
def test_2(self, m, m2):
r = testapp.get('/2/')
self.assertEquals(r.data, b'2')
但是,这迫使类中的所有测试函数为每个模拟增加一个额外的参数。如果我在此类中的测试不需要BlockingIo
或authorize
的模拟怎么办?
我想我想要的是一种执行以下操作的方法:
m = mock.something('__main__.authorize')
m2 = mock.something('__main__.BlockingIo.do')
class TestBlockingIo(unittest.TestCase):
def test_1(self):
r = testapp.get('/1/')
self.assertEquals(r.data, b'1')
def test_2(self):
r = testapp.get('/2/')
self.assertEquals(r.data, b'2')
如何重用@mock.patch('__main__.authorize')
和@mock.patch('__main__.BlockingIo.do')
以避免重复测试?
答案 0 :(得分:3)
您可以使用patches并在setUp
块中重复使用它们。
补丁程序很好,您可以在测试完成后“取消补丁”,这意味着您不会永远被嘲笑,因为某些其他测试可能需要在真实代码上运行。
在上面的链接上,您将看到以下代码:
>>> class MyTest(TestCase):
... def setUp(self):
... patcher = patch('package.module.Class')
... self.MockClass = patcher.start()
... self.addCleanup(patcher.stop)
...
... def test_something(self):
... assert package.module.Class is self.MockClass
...
它工作正常,但我真的不喜欢为每个补丁调用patch()
,start()
和addCleanup()
。
您可以轻松地将其归为可在测试类中重用的基类:
class PatchMixin:
def patch(self, target, **kwargs):
p = mock.patch(target, **kwargs)
p.start()
self.addCleanup(p.stop)
class TestBlockingIo(unittest.TestCase, PatchMixin):
def setUp(self):
self.patch('__main__.authorize')
self.patch('__main__.BlockingIo.do')
def test_1(self):
r = testapp.get('/1/')
self.assertEquals(r.data, b'1')
def test_2(self):
r = testapp.get('/2/')
self.assertEquals(r.data, b'2')