【问题标题】:Flask Testing: Modify session for next request烧瓶测试:为下一个请求修改会话
【发布时间】:2018-04-21 19:32:23
【问题描述】:

我正在尝试测试我的烧瓶应用程序的购物车部分。失败的测试如下:

# Set cart
with client.session_transaction() as sess:
    sess["cart"] = [1, 2]

# Remove item from cart
client.get("/cart/remove/2")

# Test cart
with client.session_transaction() as sess:
    resp = client.get("/")
    assert [1] == sess.get("cart")

最后一个请求的结果是会话仍然包含[1, 2]。我知道我正在测试的实际代码是有效的,因为我之前已经手动测试过它,所以它必须与我如何使用会话有关。提前致谢!

【问题讨论】:

  • 不确定这是否会有所不同...通常我使用with client.session_transaction() as sess: 设置会话,但对于您的测试用例,我只需添加from flask import sessionassert 1 in session["cart"] & @ 987654326@
  • @abigperson 我试过了,现在可以了,谢谢!

标签: python testing flask session-cookies


【解决方案1】:

问题是我使用了错误的会话。感谢@abigperson!固定代码:

def test_add(client):
    # Set cart
    with client.session_transaction() as sess:
        sess["cart"] = [1, 2]

    # Remove item from cart
    client.get("/cart/remove/2")

    # Test cart
    assert [1] == session.get("cart")

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2014-02-24
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多