{"id":25621661,"url":"https://github.com/dbfixtures/pytest-mysql","last_synced_at":"2025-04-09T22:10:41.101Z","repository":{"id":12703343,"uuid":"72486761","full_name":"dbfixtures/pytest-mysql","owner":"dbfixtures","description":"This is a pytest plugin, that enables you to test your code that relies on a running MySQL Database. It allows you to specify fixtures for MySQL process and client.","archived":false,"fork":false,"pushed_at":"2025-04-02T02:24:03.000Z","size":1038,"stargazers_count":56,"open_issues_count":3,"forks_count":20,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-02T21:06:41.692Z","etag":null,"topics":["mysql","pytest","pytest-plugin","python"],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/pytest-mysql/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dbfixtures.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-31T23:31:09.000Z","updated_at":"2025-04-02T02:24:05.000Z","dependencies_parsed_at":"2023-01-13T17:05:48.042Z","dependency_job_id":"8b108879-ec86-40ec-a8ea-bd8fe71a2ef8","html_url":"https://github.com/dbfixtures/pytest-mysql","commit_stats":{"total_commits":480,"total_committers":20,"mean_commits":24.0,"dds":0.6854166666666667,"last_synced_commit":"5812141ddfb18d6c0b325695d1797562e726b8f7"},"previous_names":["dbfixtures/pytest-mysql","clearcodehq/pytest-mysql"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbfixtures%2Fpytest-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbfixtures%2Fpytest-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbfixtures%2Fpytest-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbfixtures%2Fpytest-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dbfixtures","download_url":"https://codeload.github.com/dbfixtures/pytest-mysql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119294,"owners_count":21050755,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","pytest","pytest-plugin","python"],"created_at":"2025-02-22T09:11:48.102Z","updated_at":"2025-04-09T22:10:41.074Z","avatar_url":"https://github.com/dbfixtures.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":".. image:: https://raw.githubusercontent.com/dbfixtures/pytest-mysql/master/logo.png\n    :width: 100px\n    :height: 100px\n\npytest-mysql\n============\n\n.. image:: https://img.shields.io/pypi/v/pytest-mysql.svg\n    :target: https://pypi.python.org/pypi/pytest-mysql/\n    :alt: Latest PyPI version\n\n.. image:: https://img.shields.io/pypi/wheel/pytest-mysql.svg\n    :target: https://pypi.python.org/pypi/pytest-mysql/\n    :alt: Wheel Status\n\n.. image:: https://img.shields.io/pypi/pyversions/pytest-mysql.svg\n    :target: https://pypi.python.org/pypi/pytest-mysql/\n    :alt: Supported Python Versions\n\n.. image:: https://img.shields.io/pypi/l/pytest-mysql.svg\n    :target: https://pypi.python.org/pypi/pytest-mysql/\n    :alt: License\n\nWhat is this?\n=============\n\nThis is a pytest plugin, that enables you to test your code that relies on a running MySQL Database.\nIt allows you to specify fixtures for MySQL process and client.\n\n.. warning::\n\n    Only MySQL 5.7.6 and up are supported. For older versions, please use pytest-mysql 2.0.3\n    Although Pull Request to add back support for older MySQL versions are welcome.\n\nHow to use\n==========\n\nPlugin contains two fixtures\n\n* **mysql** - it's a client fixture that has functional scope. After each test drops test database from MySQL ensuring repeatability.\n* **mysql_proc** - session scoped fixture, that starts MySQL instance at it's first use and stops at the end of the tests.\n* **mysql_noproc** - session scoped fixtures, that allows to connect to already existing MySQL instance, and cleans the database at the end of the tests\n\nSimply include one of these fixtures into your tests fixture list.\n\nYou can also create additional mysql client and process fixtures if you'd need to:\n\n\n.. code-block:: python\n\n    from pytest_mysql import factories\n    from getpass import getuser\n\n    mysql_my_proc = factories.mysql_proc(\n        port=None, user=getuser())\n    mysql_my = factories.mysql('mysql_my_proc')\n\n.. note::\n\n    Each MySQL process fixture can be configured in a different way than the others through the fixture factory arguments.\n\nConfiguration\n=============\n\nYou can define your settings in three ways, it's fixture factory argument, command line option and pytest.ini configuration option.\nYou can pick which you prefer, but remember that these settings are handled in the following order:\n\n    * ``Fixture factory argument``\n    * ``Command line option``\n    * ``Configuration option in your pytest.ini file``\n\n.. list-table:: Configuration options\n   :header-rows: 1\n\n   * - MySQL/MariaDB option\n     - Fixture factory argument\n     - Command line option\n     - pytest.ini option\n     - Noop process fixture\n     - Default\n   * - Path to executable\n     - mysqld_exec\n     - --mysql-mysqld\n     - mysql_mysqld\n     - -\n     - mysqld\n   * - Path to safe executable\n     - mysqld_safe\n     - --mysql-mysqld-safe\n     - mysql_mysqld_safe\n     - -\n     - mysqld_safe\n   * - Path to mysql_install_db for legacy installations\n     - install_db\n     - --mysql-install-db\n     - mysql_install_db\n     - -\n     - mysql_install_db\n   * - Path to Admin executable\n     - admin_executable\n     - --mysql-admin\n     - mysql_admin\n     - -\n     - mysqladmin\n   * - Database hostname\n     - host\n     - --mysql-host\n     - mysql_host\n     - yes\n     - localhost\n   * - Database port\n     - port\n     - --mysql-port\n     - mysql_port\n     - yes (3306)\n     - random\n   * - MySQL user to work with\n     - user\n     - --mysql-user\n     - mysql_user\n     - -\n     - root\n   * - User's password\n     - passwd\n     - --mysql-passwd\n     - mysql_passwd\n     - -\n     -\n   * - Test database name\n     - dbname\n     - --mysql-dbname\n     - mysql_dbname\n     - -\n     - test\n   * - Starting parameters\n     - params\n     - --mysql-params\n     - mysql_params\n     - -\n     -\n   * - Log directory location [DEPRECATED]\n     - logsdir\n     - --mysql-logsdir\n     - mysql_logsdir\n     - -\n     - $TMPDIR\n\n\nExample usage:\n\n* pass it as an argument in your own fixture\n\n    .. code-block:: python\n\n        mysql_proc = factories.mysql_proc(\n            port=8888)\n\n* use ``--mysql-port`` command line option when you run your tests\n\n    .. code-block::\n\n        py.test tests --mysql-port=8888\n\n\n* specify your port as ``mysql_port`` in your ``pytest.ini`` file.\n\n    To do so, put a line like the following under the ``[pytest]`` section of your ``pytest.ini``:\n\n    .. code-block:: ini\n\n        [pytest]\n        mysql_port = 8888\n\nExamples\n========\n\nPopulating database for tests\n-----------------------------\n\nWith SQLAlchemy\n+++++++++++++++\n\nThis example shows how to populate database and create an SQLAlchemy's ORM connection:\n\nSample below is simplified session fixture from\n`pyramid_fullauth \u003chttps://github.com/fizyk/pyramid_fullauth/\u003e`_ tests:\n\n.. code-block:: python\n\n    from sqlalchemy import create_engine\n    from sqlalchemy.orm import scoped_session, sessionmaker\n    from sqlalchemy.pool import NullPool\n    from zope.sqlalchemy import register\n\n\n    @pytest.fixture\n    def db_session(mysql):\n        \"\"\"Session for SQLAlchemy.\"\"\"\n        from pyramid_fullauth.models import Base  # pylint:disable=import-outside-toplevel\n\n        # assumes setting, these can be obtained from pytest-mysql config or mysql_proc\n        connection = f'mysql+mysqldb://root:@127.0.0.1:3307/tests?charset=utf8'\n\n        engine = create_engine(connection, echo=False, poolclass=NullPool)\n        pyramid_basemodel.Session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))\n        pyramid_basemodel.bind_engine(\n            engine, pyramid_basemodel.Session, should_create=True, should_drop=True)\n\n        yield pyramid_basemodel.Session\n\n        transaction.commit()\n        Base.metadata.drop_all(engine)\n\n\n    @pytest.fixture\n    def user(db_session):\n        \"\"\"Test user fixture.\"\"\"\n        from pyramid_fullauth.models import User\n        from tests.tools import DEFAULT_USER\n\n        new_user = User(**DEFAULT_USER)\n        db_session.add(new_user)\n        transaction.commit()\n        return new_user\n\n\n    def test_remove_last_admin(db_session, user):\n        \"\"\"\n        Sample test checks internal login, but shows usage in tests with SQLAlchemy\n        \"\"\"\n        user = db_session.merge(user)\n        user.is_admin = True\n        transaction.commit()\n        user = db_session.merge(user)\n\n        with pytest.raises(AttributeError):\n            user.is_admin = False\n.. note::\n\n    See the original code at `pyramid_fullauth's conftest file \u003chttps://github.com/fizyk/pyramid_fullauth/blob/2950e7f4a397b313aaf306d6d1a763ab7d8abf2b/tests/conftest.py#L35\u003e`_.\n    Depending on your needs, that in between code can fire alembic migrations in case of sqlalchemy stack or any other code\n\nConnecting to MySQL/MariaDB (in a docker)\n-----------------------------------------\n\nTo connect to a docker run MySQL and run test on it, use noproc fixtures.\n\n.. code-block:: sh\n\n    docker run --name some-db -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql --expose 3306\n\n.. code-block:: sh\n\n    docker run --name some-db -e MARIADB_ALLOW_EMPTY_PASSWORD=yes -d mariadb --expose 3306\n\nThis will start MySQL in a docker container, however using a MySQL installed locally is not much different.\n\nIn tests, make sure that all your tests are using **mysql_noproc** fixture like that:\n\n.. code-block:: python\n\n    mysql_in_docker = factories.mysql_noproc()\n    mysql = factories.mysql(\"mysql_in_docker\")\n\n\n    def test_mysql_docker(mysql):\n        \"\"\"Run test.\"\"\"\n        cur = mysql.cursor()\n        cur.query(\"CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);\")\n        mysql.commit()\n        cur.close()\n\nAnd run tests:\n\n.. code-block:: sh\n\n    pytest --mysql-host=127.0.0.1\n\n\n\nRunning on Docker/as root\n=========================\n\nUnfortunately, running MySQL as root (thus by default on docker) is not possible.\nMySQL (and MariaDB as well) will not allow it.\n\n.. code-block::\n\n    USER nobody\n\nThis line should switch your docker process to run on user nobody. See `this comment for example \u003chttps://github.com/dbfixtures/pytest-mysql/issues/62#issuecomment-367975723\u003e`_\n\nPackage resources\n-----------------\n\n* Bug tracker: https://github.com/dbfixtures/pytest-mysql/issues\n\nRelease\n=======\n\nInstall pipenv and --dev dependencies first, Then run:\n\n.. code-block::\n\n    pipenv run tbump [NEW_VERSION]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbfixtures%2Fpytest-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdbfixtures%2Fpytest-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbfixtures%2Fpytest-mysql/lists"}