Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing Flask

Throughout this book we’ll have various examples. To make sure they work properly we’ll also have tests for these examples. This is the test for the example with Flask. It uses pytest and it has another example of a decorator. Here we decorate a function to become a fixture.

import pytest
import flask_app

@pytest.fixture()
def web():
    return flask_app.app.test_client()

def test_main_page(web):
    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'Hello World!' == rv.data

def test_main_page(web):
    rv = web.get('/login')
    assert rv.status == '200 OK'
    assert b'Showing the login page ...' == rv.data