Flask Path or route parameters - testing



examples/flask/path/test_app.py
import app
import pytest

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

def test_app(web):
    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'Main<br>' in rv.data

@pytest.mark.parametrize('uid', ['23', '42', 'Joe'])
def test_user(web, uid):
    rv = web.get(f'/user/{uid}')
    assert rv.status == '200 OK'
    assert uid == rv.data.decode('utf-8')

def test_user_fail(web):
    rv = web.get(f'/user/')
    assert rv.status == '404 NOT FOUND'

def test_user_fail(web):
    rv = web.get(f'/user')
    assert rv.status == '404 NOT FOUND'
The test here get a bit complex. We have 3 different tests function. Each one needs the variable returned by the test_client function.

While the route with the parameter can handle any value, there is one route that it does not handle. If the visitor reached the page /user/ page either by mistake or by removing the parameter in the URL, then we'll see a "404 Not Found" page.