|
| 1 | +pytest-sqlalchemy |
| 2 | +================= |
| 3 | + |
| 4 | +|Docs|_ |PyPI|_ |Git|_ |
| 5 | + |
| 6 | +.. |Docs| image:: https://readthedocs.org/projects/pytest-sqlalchemy/badge/?version=latest |
| 7 | +.. _Docs: https://pytest-sqlalchemy.readthedocs.io/ |
| 8 | + |
| 9 | +.. |PyPI| image:: https://badge.fury.io/py/pytest-sqlalchemy.svg |
| 10 | +.. _PyPI: https://pypi.org/project/pytest-sqlalchemy/ |
| 11 | + |
| 12 | +.. |Git| image:: https://github.com/toirl/pytest-sqlalchemy/actions/workflows/ci.yml/badge.svg |
| 13 | +.. _Git: https://github.com/toirl/pytest-sqlalchemy |
| 14 | + |
| 15 | +SQLAlchemy related fixtures to handle connections and transactions with SQLAlchemy in tests. |
| 16 | + |
| 17 | +Fixtures |
| 18 | +-------- |
| 19 | +This plugin provides the following fixtures which gives access to the SQLAlchemy objects of the same |
| 20 | +name. |
| 21 | + |
| 22 | +* **engine** The engine used to connect to the database. Scope is "module". |
| 23 | +* **connection** An open connection to the database. Scope is "module". |
| 24 | + |
| 25 | +See `Working with Engines and Connections`__ on how to use these fixtures. |
| 26 | + |
| 27 | +__ http://docs.sqlalchemy.org/en/latest/core/connections.html#module-sqlalchemy.engine |
| 28 | + |
| 29 | +* **transaction** A started transaction on the connection. Transaction will be rolled back. |
| 30 | + No Scope. |
| 31 | + |
| 32 | +See `Using Transactions`__ on how to use this fixtures |
| 33 | + |
| 34 | +__ http://docs.sqlalchemy.org/en/latest/core/connections.html#using-transactions |
| 35 | + |
| 36 | +* **dbsession** A sqlalchemy session *not* bound to any model. No scope. |
| 37 | + |
| 38 | +See `Session Basics`__ to learn about how to use sessions. |
| 39 | + |
| 40 | +__ http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#session-basics |
| 41 | + |
| 42 | +Usage |
| 43 | +----- |
| 44 | +The fixtures can be used in your tests like any other `Pytest Fixtures`__. |
| 45 | + |
| 46 | +__ https://docs.pytest.org/en/3.6.1/fixture.html |
| 47 | + |
| 48 | +**Example**: |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + import pytest |
| 53 | + from pytest_sqlalchemy import connection |
| 54 | + |
| 55 | + def test_connection(connection): |
| 56 | + # Do fancy stuff with the connection. |
| 57 | + # Note you will not need to close the connection. This is done |
| 58 | + # automatically when the scope (module) of the fixtures ends. |
| 59 | + assert connection |
| 60 | +
|
| 61 | +Invoke |
| 62 | +------ |
| 63 | +You need to provide the connection URL for the engine when invoking the pytest command: |
| 64 | + |
| 65 | +.. code-block:: bash |
| 66 | +
|
| 67 | + pytest --sqlalchemy-connect-url="postgresql://scott:tiger@localhost:5432/mydatabase" |
| 68 | + |
| 69 | +Or override the ``sqlalchemy_connect_url`` fixture on your ``conftest.py`` file: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + @pytest.fixture(scope="session") |
| 74 | + def sqlalchemy_connect_url(): |
| 75 | + return 'postgresql://scott:tiger@localhost:5432/mydatabase' |
| 76 | +
|
| 77 | +Development |
| 78 | +---------- |
| 79 | + |
| 80 | +To get going, in a checkout: |
| 81 | + |
| 82 | +.. code-block:: bash |
| 83 | +
|
| 84 | + uv sync |
| 85 | +
|
| 86 | +You can then run the tests with: |
| 87 | + |
| 88 | +.. code-block:: bash |
| 89 | +
|
| 90 | + uv run pytest |
0 commit comments