Python如何重用Mock以避免多次编写mock.patch?

Mat*_*sen 4 python mocking flask python-3.x python-mock

给定的代码如下:

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)
Run Code Online (Sandbox Code Playgroud)

我希望能够为 编写几个测试GET /n/,每个测试都模拟authorizeBlockingIO(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()
Run Code Online (Sandbox Code Playgroud)

但是,我不想@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')
Run Code Online (Sandbox Code Playgroud)

但是,这会强制类中的所有测试函数为每个模拟采用一个额外的参数。如果我在这个类中有不需要模拟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')
Run Code Online (Sandbox Code Playgroud)

我怎样才能重复使用我的@mock.patch('__main__.authorize')@mock.patch('__main__.BlockingIo.do')避免在测试中重复自己?

Gui*_*ume 7

您可以使用补丁并在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
...
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我真的不喜欢调用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')
Run Code Online (Sandbox Code Playgroud)