如何使用pytest测试处理请求的Flask(非路由)函数?

How to test a Flask (non-route) function that processes a request, using pytest?

上下文:我有两个处理相同请求数据的 Flask 路由(一个交互,一个作为 API)。为了让我的代码保持干燥,我想写一个函数 process_post_request() that

  1. 接受来自每个路由的输入参数中的请求,
  2. 解析请求,
  3. returns 路由可以使用的结果。

例如,在views.py中:

@app.route('/interactive', methods=['GET', 'POST'])
def interactive():
    if request.method == 'POST':
        sum, product = process_post_request(request)
        # present the results on a web page

@app.route('/api', methods=['GET', 'POST'])
def api():
    if request.method == 'POST':
        sum, product = process_post_request(request)
        # return the results so that JavaScript can parse them

def process_post_request(request):
    param1 = request.form.get('param1')
    param2 = request.form.get('param2')
    sum = param1 + param2
    product = param1 * param2
    return sum, product

问题:如何为 process_post_request() 编写 pytest?问题是,如果我创建一个“请求”并尝试将其传递给 process_post_request(),该请求会转到一条路线,因此路线 returns 会产生结果。例如,在 views_test.py:

import pytest

@pytest.fixture
def client():
    """Create a client to make requests from"""
    with app.test_client() as client:
        with app.app_context():
            pass
        yield client

def test_process_post_request():
    request = client.post('/interactive', data={'param1': 5, 'param2': 7})
    sum, product = process_post_request(request)
    assert sum == 12

因为requestreturns一个响应,pytest抛出这个错误:

>       request = client.post('/interactive', data={'param1': 5, 'param2': 7})
E       AttributeError: 'function' object has no attribute 'post'

我创建了一个到 return 逗号分隔参数的应用程序路由:

@app.route('/pass-through', methods = ['GET', 'POST'])
def pass_through():
    if request.method == 'POST':
        params = process_post_request(request)
        return ','.join(params)

然后测试了那个应用路由:

def test_process_post_request():
    with app.test_client() as client:
        response = client.post('/pass-through', data={'param1': 5, 'param2': 7})
        sum, product = response.data.decode().split(',')
        assert sum == 12

其中 decode() 从字节转换为字符串。

这不是最令人满意的解决方案,因为它真正测试了应用程序路由,因此它取决于应用程序路由函数 pass_through() 使用与 process_post_request().

相同的参数名称