Fixtures

Good tests are always executed considering a predefined, reproducible application state; that is, whenever you run a test in the chosen state, the result will always be equivalent. Usually, this is achieved by setting your database data yourself and clearing your cache and any temporary files (if you make use of external services, you should mock them) for each test. Clearing cache and temporary files is not hard, while setting your database data, on the other hand, is.

If you're using Flask-SQLAlchemy to hold your data, you would need to hardcode, somewhere in your tests as follows:

attributes = { … }
model = MyModel(**attributes)
db.session.add(model)
db.session.commit()

This approach does not scale as it is not easily reusable (when you ...

Get Building Web Applications with Flask now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.