This video is available to students only

How to Test Flask Routes and Handle 404 Errors


In the last chapter we created a single test the model in tests/ We can go in and add functional tests to ensure that pages we've added work as expected as well as one for our handling of 404s.

Since we'll often be creating a new book, in this test suite we have turned that logic into a function (called sample_book), and use it as a fixture in our test suites.

from yumroad.models import db, Product

def sample_book(name="Sherlock Homes", description="a house hunting real estate agent"):

We've already seen some testing of the model, but we can add in testing for our routes to ensure each of the pages load and that the 404s work correctly by adding in three more tests. Here is the full test file once we are done.

from flask import url_for
import pytest

Start a new discussion. All notification go to the author.