[Flask] pytest code from flask official tut "with" vs "no with" block

Arup Rakshit ar at zeit.io
Mon Jun 10 08:32:40 EDT 2019


Hi,

In this piece of code:

def test_login(client, auth):
    assert client.get('/auth/login').status_code == 200
    response = auth.login()
    assert response.headers['Location'] == 'http://localhost/'

    with client:
        client.get('/‘) # 1
        assert session['user_id'] == 1 # 2
        assert g.user['username'] == ‘test’ # 3

Why #1, #2 and #3 is wrapped into the with block, instead of writing them normally like:

client.get('/')
assert session['user_id'] == 1
assert g.user['username'] == 'test'

I found the code from https://github.com/pallets/flask/blob/master/examples/tutorial/tests/test_auth.py#L67 


Thanks,

Arup Rakshit
ar at zeit.io





More information about the Flask mailing list