This video is available to students only
How to Test Flask Routes and Handle 404 Errors
Testing
In the last chapter we created a single test the model in tests/test_product.py
. 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.
yumroad-app/tests/test_product.py
from yumroad.models import db, Product
@pytest.fixture
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.
yumroad-app/tests/test_product.py
from flask import url_for
import pytest
This page is a preview of Fullstack Flask: Build a Complete SaaS App with Flask