skillfed

pytest-mysql

MySQL process and client fixtures for pytest

pytest-mysql v4.0.0 276.1K downloads/30d#8,167 on PyPI58
Copyleft 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) Active released

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-mysql

uv

uv add pytest-mysql

poetry

poetry add pytest-mysql

Installing 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

Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)Natural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Testing

Tags

pytest mysql fixturemysql testing pluginmariadb pytestdatabase test fixturesmysql process managementpytest database setupmysql test isolation
pytest-plugindatabase-testingmysql-mariadb

More Python Modules packages