Skip to main content

pytest-async-sqlalchemy

PyPI version Python versions

Database testing fixtures using the SQLAlchemy asyncio API

You can install "pytest-async-sqlalchemy" via pip from PyPI

$ pip install pytest-async-sqlalchemy

Setup

Providing a Session Scoped Event Loop

The first thing to do is to declare an even_loop fixture with the scope set as "session". You can copy & paste the code below to your conftest.py:

@pytest.fixture(scope="session")
def event_loop():
    """
    Creates an instance of the default event loop for the test session.
    """
    if sys.platform.startswith("win") and sys.version_info[:2] >= (3, 8):
        # Avoid "RuntimeError: Event loop is closed" on Windows when tearing down tests
        # https://github.com/encode/httpx/issues/914
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    loop = asyncio.new_event_loop()
    yield loop
    loop.close()

This is required since pytest-async-sqlalchemy shares the database connection between tests for performance reasons, but the default even_loop fixture defined by pytest-asyncio is function scoped1 (i.e., it kills the database connection after each test).

Providing Database URL and Initialization

pytest-async-sqlalchemy provides placeholders to configure and initialize the testing database: database_url and init_database. These two must be defined in your project conftest.py like below:

@pytest.fixture(scope="session")
def _database_url():
    return "postgresql+asyncpg://postgres:masterkey@localhost/dbtest"


@pytest.fixture(scope="session")
def init_database():
    from myprorject.db import metadata

    return metadata.create_all

The _database_url must be a session-scoped fixture that returns the database URL in SQLAlchemy standard. init_database must also be a session-scoped fixture that returns the callable used to initialize the database (in most cases, this would return the metadata.create_all function).

Usage

This plugin provides the following fixtures:

  • db_session: An AsyncSession object bounded to the test session database. Database transactions are discarded after each test function, so the same database is used for the entire test suite (avoiding the overhead of initializing a database on every test).
  • database: An URL to the initialized test session database.
  • scoped_database: scoped_database provides a new database within the scope of the test function. The value of the fixture is a string URL pointing to the database.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-async-sqlalchemy" is free and open source software


1. pytest-async-sqlalchemy can't provide its own even_loop since pytest plugins are not able to override fixtures from one another. So the only solution we have now is to aks the user to declare its own event_loop fixture. Suggestions on how to overcome that in a better way are welcome, hit us on the Issues section.

Release files for pytest-async-sqlalchemy 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-async-sqlalchemy 0.2.0
File Size Uploaded
pytest-async-sqlalchemy-0.2.0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-async-sqlalchemy 0.2.0
File Interpreter ABI Platform
pytest_async_sqlalchemy-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / pytest-async-sqlalchemy-0.2.0.tar.gz

Download URL pytest-async-sqlalchemy-0.2.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0dcf80fdff1ea0046834cff2bc100c82d159e45a7ae21545a6ba9119a962b9d7
BLAKE2b-256 checksum
How to use checksums
88aa264687cab98d67b73e680fe9299daf81edfe9b6bac016c4978f2380321cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Release files / pytest_async_sqlalchemy-0.2.0-py3-none-any.whl

Download URL pytest_async_sqlalchemy-0.2.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60d7159f43d21e79d7051841fd2d6e094b7267ddc8d7192daea597afca938b12
BLAKE2b-256 checksum
How to use checksums
6514d1abbf8dcb11097a7108be8eb4b598c958fc49eb91c5f62449ca9dda489a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page