Pytest Flask Sqlalchemy

screenshot of Pytest Flask Sqlalchemy
flask

A pytest plugin for preserving test isolation in Flask-SQLAlchemy using database transactions.

Overview

pytest-flask-sqlalchemy is a powerful plugin designed to simplify the process of writing tests for applications built with Flask and SQLAlchemy. By leveraging a transactional testing approach, it ensures that any changes made to the database during a test are rolled back once the test completes. This functionality not only makes it safer to test various scenarios that involve database interactions but also enhances the developer's confidence in the integrity of their tests.

Inspired by Django's support for transactional tests, this plugin effectively introduces an easy-to-use set of fixtures tailored for Flask-SQLAlchemy. With pytest-flask-sqlalchemy, developers can now execute complex database operations within tests without the worry of leaving residual data that could affect subsequent tests.

Features

  • db_session Fixture: This fixture allows for easy database updates during tests, ensuring that no changes persist beyond the test's execution body.
  • db_engine Fixture: Mimicking the behavior of SQLAlchemy's Engine object, this fixture enables developers to manage database connections seamlessly in their tests.
  • Nested Transactions: Configuration options enable nested transactions, allowing any method in the codebase to operate within a test context safely.
  • PostgreSQL Support: The plugin is primarily tested against PostgreSQL 9.6, ensuring a reliable testing environment for users of this database.
  • Future Backend Support: Official support for SQLite and MySQL is planned for upcoming releases, broadening the plugin's usability.
  • Easy Installation: Installation is straightforward via pip, with pytest automatically detecting the plugin during the test collection phase.
  • Customizable Conftest Setup: Users can define their own database access fixtures, which the plugin relies on to facilitate nested transactions in tests.
flask
Flask

Flask is a lightweight and popular web framework for Python, known for its simplicity and flexibility. It is widely used to build web applications, providing a minimalistic approach to web development with features like routing, templates, and support for extensions.