pytest-mysql
MySQL process and client fixtures for pytest
What it is and what it does
pytest-mysql is a pytest plugin that automates MySQL and MariaDB database setup for integration tests. It provides three main fixtures: mysql (a function-scoped client that drops the test database after each test), mysql_proc (a session-scoped process that starts a local MySQL instance), and mysql_noproc (a session-scoped fixture for connecting to an already-running database). The plugin depends on pytest, port-for, mirakuru, pymysql, and packaging to manage process lifecycle, port allocation, and database connections.
You use it by installing the plugin and then importing fixtures into your test suite. Configuration is flexible—you can set options via fixture factory arguments, command-line flags, or pytest.ini entries. The plugin handles port discovery, database creation, user authentication, and cleanup automatically, making it straightforward to write repeatable database tests without manual setup or teardown boilerplate.
Use it for:
- Write integration tests that verify application code against a real MySQL database without manual server management.
- Run isolated test suites in CI/CD pipelines where each test gets a clean database and the server starts and stops automatically.
- Connect tests to a MySQL instance running in Docker or on a remote host using the noproc fixture.
- Populate test databases with SQLAlchemy ORM or raw SQL and verify application behavior against schema changes.
- Configure different MySQL instances for different test fixtures with custom ports, users, and parameters.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that provides MySQL and MariaDB fixtures for testing—manages both local database processes and connections to running instances, with automatic cleanup between tests.
Yes. This is a stable, actively maintained plugin with low install friction and no known vulnerabilities. It solves a real problem—automating MySQL test infrastructure—and is widely used. The LGPLv3+ license is permissive for test code. Install it if your test suite needs a managed MySQL database.
Install
pytest-mysql on PyPI
pip
pip install pytest-mysqluv
uv add pytest-mysqlpoetry
poetry add pytest-mysqlInstalling pytest-mysql
Before you install
Low friction install with five runtime dependencies (pytest, port-for, mirakuru, pymysql, packaging). Actively maintained with recent releases.
License in practice
Licensed under LGPLv3+, a copyleft license. Your test code using this plugin must comply with LGPL terms; derivative works of the plugin itself require source disclosure, but linking to it from proprietary tests is permitted under LGPL's library exception.
Quickstart
pip install pytest-mysql
In conftest.py or test file:
from pytest_mysql import factories
mysql_proc = factories.mysql_proc()
mysql = factories.mysql('mysql_proc')
def test_example(mysql):
cur = mysql.cursor()
cur.execute('SELECT 1')
mysql.commit()
Requires MySQL 8.0+ or MariaDB 10.11+ installed on the test machine; mysqld and mysqladmin binaries must be in PATH or configured via fixture arguments.
Verify before relying
- Whether the plugin supports Python versions beyond 3.14 or if 3.14 is the current ceiling.
- Performance characteristics when running many concurrent test fixtures or large test suites.
- Compatibility with MySQL 8.1+ or MariaDB 11.0+ versions released after the tested range.
Package facts
| License | GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this… (full text in the JSON record) (copyleft) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — pytest, port-for, mirakuru, pymysql, packaging |
| Maintenance | actively maintained — 124 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 276,115/month — #8,167 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_mysql-4.0.0-py3-none-any.whl
Keywords: tests, pytest, fixture, mysql
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
pytest-postgresqlA pytest plugin that provides fixtures to…
copyleft · top 5,000 on PyPI
pytest-rabbitmqA pytest plugin that provides fixtures to start…
copyleft · top 15,000 on PyPI
pytest-redisA pytest plugin that provides fixtures for…
copyleft · top 15,000 on PyPI
pytest-sparkA pytest plugin that integrates Apache Spark…
permissive · top 15,000 on PyPI
pytest-mock-resourcesProvides pytest fixtures that spin up real…
permissive · top 15,000 on PyPI
pytest-databasesProvides pytest fixtures that spin up isolated…
permissive · top 15,000 on PyPI
pytest-dockerpytest-docker provides pytest fixtures that…
permissive · top 5,000 on PyPI
pytest-deadfixturesA pytest plugin that identifies unused and…
permissive · top 5,000 on PyPI
pytest-unused-fixturesA pytest plugin that identifies and reports…
permissive · top 15,000 on PyPI
pytest-containerA pytest plugin that automates container…
copyleft · top 15,000 on PyPI