MySQL

In order to enable mysql support you have to add testsuite.databases.mysql.pytest_plugin to pytest_plugins list in your conftest.py file and configure MySQL schemas location.

By default testsuite starts MySQL service. In this case MySQL installation is required (MariaDB 10+ should work as well).

Currently mysql plugin uses synchronous pymysql driver.

MySQL plugin creates database schema once. And then populates database with data fixtures on each test. It looks for database fixtures by the following names:

  • file my_DBNAME.sql

  • directory my_DBNAME/

Environment variables

TESTSUITE_MYSQL_PORT

Use to override mysql server port. Default is 13307.

TESTSUITE_MYSQL_SERVER_START_TIMEOUT

By default testsuite will wait for up to 10s for MySQL to start, one may customize this timeout via environment variable TESTSUITE_MYSQL_SERVER_START_TIMEOUT.

Customize port

Testsuite may start MySQL with custom port, if TESTSUITE_MYSQL_PORT environment variable is specified

Use external instance

You can force it to use your own mysql installation with command-line option --mysql=mysql://user:password@hostname/.

Example integration

from testsuite.databases.mysql import discover

@pytest.fixture(scope='session')
def mysql_local():
    return discover.find_schemas([SCHEMAS_DIR])

Database access example

def test_read_from_database(mysql):
    cursor = mysql['chat_messages'].cursor()
    cursor.execute(
        'SELECT username, text FROM messages WHERE id = %s', (data['id'],),
    )
    record = cursor.fetchone()
    assert record == ('foo', 'bar')

Functions

find_schemas

testsuite.databases.mysql.discover.find_schemas(schema_dirs: List[Path], dbprefix: str = 'testsuite-', extra_schema_args: Optional[Dict[str, Any]] = None) Dict[str, DatabaseConfig][source]

Retrieve database schemas from filesystem.

Parameters:
  • schema_dirs – list of schema pathes

  • dbprefix – database name internal prefix

  • extra_schema_args

    for each DB contains list of tables we don’t have to truncate and flag for explicit creation example:

    {
        'database1': {
            'create': False,
            'truncate_non_empty': True,
            'keep_tables': [
                'table1', 'table2'
            ]
        }
    }
    

Returns:

Dictionary where key is dbname and value is classes.DatabaseConfig instance.

Fixtures

mysql

testsuite.databases.mysql.pytest_plugin.mysql()[source]

MySQL fixture.

Returns dictionary where key is database alias and value is control.ConnectionWrapper

mysql_local

testsuite.databases.mysql.pytest_plugin.mysql_local()[source]

Use to override databases configuration.

mysql_conninfo

testsuite.databases.mysql.pytest_plugin.mysql_conninfo()[source]

Marks

pytest.mark.mysql

pytest.mark.mysql(dbname, *, files=(), directories=(), queries=()):

Use this mark to specify extra data fixtures in a per-test manner.

Parameters:
  • dbname – Database name.

  • files – List of filenames to apply to the database.

  • directories – List of directories to apply to the database.

  • queries – List of queries to apply to the database.

Classes

class testsuite.databases.mysql.control.ConnectionWrapper[source]

MySQL database connection wrapper.

property conninfo: ConnectionInfo

classes.ConnectionInfo instance.

cursor(**kwargs) Cursor[source]

Returns cursor instance.

class testsuite.databases.mysql.classes.ConnectionInfo[source]

Mysql connection info class.

Parameters:
  • port – database port

  • hostname – database hostname

  • user – database user

  • password – database password

  • dbname – database name

replace(**kwargs) ConnectionInfo[source]

Returns new instance with attributes updated.