About¶
Elefast is an opiniated database testing toolkit that integrates Postgres, SQLAlchemy. It provides first-class features and documentation for using Docker containers to automatically start your database with your tests and Pytest to run them and manage their setup logic. Our guiding principles are:
- Ease of use 🌴 You should not need to hand-roll hundreds of lines of code, just because you want your tests to cover actual database interaction. SQLAlchemy and Docker are not niche technologies, they are used by thousands of developers around the world.
- Speed ⏱️ Using an actual database in your tests can slow thekm down significantly. E.g. launching a Postgres container using Testcontainers takes almost 30 seconds1. With elefast, we'll ensure you'll pay as little startup costs as possible, while making it easy to avoid such waiting time all together by keeping the container around for future test runs.
- Less magic 🪄 Pytest fixtures can be quite magical. Instead of providing ready-made fixtures with exotic configuration mechanisms, Elefast makes you write your fixtures yourself. A tiny bit of effort, but you have full control and visibility.
The result is readable and performant test code like the following:
from sqlalchemy import Connection, text
def test_that_uses_the_database(db_connection: Connection): # (1)!
result = db_connection.execute(text("1 + 1"))
assert 2 == result
- The
db_connectionfixture is written by you, but with the help of Elefast utility functions
When you run your tests with pytest, Elefast will
- (optional) Start an optimized Postgres container if one is not already running.
- Create a fresh database, just for the test.
- (optional) Create the necessary table structures.
- Pass the
Connectionobject to the test.
This saves you from mocking DB logic, coming up with your own way of doing all of this. Head over to the Getting Started to learn more.
-
An anecdotal test run on my Intel MacBook Pro. It is probably faster on newer or more powerful hardware but it still does not seem to be optimized for speed, at least not enough for an impatient user like me. ↩