小编Raf*_*ira的帖子

使用 Pytest 测试 Flask 会话

目前我正在做一个 Flask 项目,需要做一些测试。

我正在努力的测试是关于 Flask Sessions 的。

我有这样的看法:

@blue_blueprint.route('/dashboard')
"""Invoke dashboard view."""
if 'expires' in session:
    if session['expires'] > time.time():
        pass
    else:
        refresh_token()
        pass
    total_day = revenues_day()
    total_month = revenues_month()
    total_year = revenues_year()
    total_stock_size = stock_size()
    total_stock_value = stock_value()
    mean_cost = total_stock_value/total_stock_size
    return render_template('dashboard.html.j2', total_day=total_day, <br> total_month=total_month, total_year=total_year, total_stock_size=total_stock_size, total_stock_value=total_stock_value, mean_cost=mean_cost)
else:
    return redirect(url_for('blue._authorization'))
Run Code Online (Sandbox Code Playgroud)

并进行此测试:

def test_dashboard(client):
    with client.session_transaction(subdomain='blue') as session:
        session['expires'] = time.time() + 10000
        response = client.get('/dashboard', subdomain='blue')
        assert response.status_code == 200
Run Code Online (Sandbox Code Playgroud)

我目前的 conftest.py 是:

@pytest.fixture
def app(): …
Run Code Online (Sandbox Code Playgroud)

python flask

8
推荐指数
1
解决办法
1735
查看次数

标签 统计

flask ×1

python ×1