Skip to main content

automatically setups a mysqld instance in a temporary directory, and destroys it after testing

Project description

testing.mysqld automatically setups a mysqld instance in a temporary directory, and destroys it after testing

https://travis-ci.org/tk0miya/testing.mysqld.svg?branch=master https://coveralls.io/repos/tk0miya/testing.mysqld/badge.png?branch=master https://codeclimate.com/github/tk0miya/testing.mysqld/badges/gpa.svg

Install

Use easy_install (or pip):

$ easy_install testing.mysqld

And testing.mysqld requires MySQL server in your PATH.

Usage

Create MySQL instance using testing.mysqld.Mysqld:

import testing.mysqld
from sqlalchemy import create_engine

# Lanuch new MySQL server
with testing.mysqld.Mysqld() as mysqld:
    # connect to MySQL
    engine = create_engine(mysqld.url())

    # if you use mysqldb or other drivers:
    #   import _mysql
    #   db = _mysql.connect(**mysqld.dsn())

    #
    # do any tests using MySQL...
    #

# MySQL server is terminated here

testing.mysqld.Mysqld executes mysql_install_db and mysqld on instantiation. On deleting Mysqld object, it terminates MySQL instance and removes temporary directory.

If you want a database including tables and any fixtures for your apps, use copy_data_from keyword:

# uses a copy of specified data directory of MySQL.
mysqld = testing.mysqld.Mysqld(copy_data_from='/path/to/your/database')

You can specify parameters for MySQL with my_cnf keyword:

# boot MySQL server without socket listener (use unix-domain socket)
mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})

For example, you can setup new MySQL server for each testcases on setUp() method:

import unittest
import testing.mysqld

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})

    def tearDown(self):
        self.mysqld.stop()

To make your tests faster

testing.mysqld.Mysqld invokes initdb command on every instantiation. That is very simple. But, in many cases, it is very waste that generating brandnew database for each testcase.

To optimize the behavior, use testing.mysqld.MysqldFactory. The factory class is able to cache the generated database beyond the testcases, and it reduces the number of invocation of mysql_install_db command:

import unittest
import testing.mysqld

# Generate Mysqld class which shares the generated database
Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True)


def tearDownModule(self):
    # clear cached database at end of tests
    Mysqld.clear_cache()


class MyTestCase(unittest.TestCase):
    def setUp(self):
        # Use the generated Mysqld class instead of testing.mysqld.Mysqld
        self.mysqld = Mysqld()

    def tearDown(self):
        self.mysqld.stop()

If you want to insert fixtures to the cached database, use initdb_handler option:

# create initial data on create as fixtures into the database
def handler(mysqld):
    conn = psycopg2.connect(**mysqld.dsn())
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
    cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
    cursor.close()
    conn.commit()
    conn.close()

# Use `handler()` on initialize database
Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True,
                                      on_initialized=handler)

Requirements

  • Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5

  • pymysql

License

Apache License 2.0

History

1.3.0 (2016-02-03)

  • Add timeout to server invoker

  • Support MySQL-5.7

  • Add testing.mysqld.MysqldFactory

  • Depend on testing.common.database package

  • Assign port if networking not disabled

1.2.8 (2015-04-06)

  • Fix bugs

1.2.7 (2014-12-20)

  • Support for relative mysql_install_db links

  • Use absolute path for which command

1.2.6 (2014-06-19)

  • Add timeout on terminating mysqld

  • Fix bugs

1.2.5 (2014-06-11)

  • Fix ImportError if caught SIGINT on py3

1.2.4 (2014-02-13)

  • Fix testing.mysqld.Mysqld#start() fails if mysql_install_db does not create database named “test”

1.2.3 (2013-12-11)

  • Use pymysql driver as default in Mysqld#url()

1.2.2 (2013-12-06)

  • Change behavior: Mysqld#stop() cleans workdir

  • Fix caught AttributeError on object deletion

1.2.1 (2013-12-05)

  • Add mysqld.skipIfNotInstalled decorator (alias of skipIfNotFound)

  • Suport python 2.6 and 3.2

1.2.0 (2013-12-04)

  • Add @skipIfNotFound decorator

1.1.2 (2013-11-26)

  • Fix it does not cleanup temporary directory if Mysqld object has been deleted

1.1.1 (2013-11-25)

  • Add charset parameter to Mysqld#url()

1.1.0 (2013-11-22)

  • Rename package: test.mysqld -> testing.mysqld

  • Add Mysqld#url() method (for sqlalchemy)

1.0.0 (2013-10-17)

  • First release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testing.mysqld-1.3.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

testing.mysqld-1.3.0-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file testing.mysqld-1.3.0.tar.gz.

File metadata

File hashes

Hashes for testing.mysqld-1.3.0.tar.gz
Algorithm Hash digest
SHA256 937b372d10dbff0572286dfbaccdae5c554c58f9282e806a367b93a0937aa5ec
MD5 84f4e746046fe08b937d2dadc62a48fd
BLAKE2b-256 ec84b21455f75fc626d6f6f671d6c87657525caff565f21d6cb2898046985573

See more details on using hashes here.

File details

Details for the file testing.mysqld-1.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for testing.mysqld-1.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1a19da996e0849aacb0976300b83fb416ba80c4fa29d608cabd7563cb8f1756
MD5 235f0638acc2007b8dbe8777bb1df69f
BLAKE2b-256 1bb6feb7e1270110cdebc3f1af8dadfee8981ca2b673ad11abd68b4dfbdb9807

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page