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

Pytest: testing Flask echo GET

We have a fixture that will return the Flask test_client that we can use in every test function to send a get request.

import echo_get
import pytest


@pytest.fixture()
def app():
    print("setup")
    return echo_get.app.test_client()

def test_main(app):
    rv = app.get('/')
    assert rv.status == '200 OK'
    assert b'<form action="/echo" method="GET">' in rv.data

def test_echo(app):
    rv = app.get('/echo?text=Hello')
    assert rv.status == '200 OK'
    assert b'You said: Hello' in rv.data

def test_empty_echo(app):
    rv = app.get('/echo')
    assert rv.status == '200 OK'
    assert b'Nothing to say?' in rv.data

def test_missing_page(app):
    rv = app.get('/qqrq')
    assert rv.status == '404 NOT FOUND'
    assert b'The requested URL was not found on the server.' in rv.data