testing.mysqld
automatically setups a mysqld instance in a temporary directory, and destroys it after testing
Use easy_install (or pip):
$ easy_install testing.mysqld
And testing.mysqld
requires MySQL server in your PATH.
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()
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)
- Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
- pymysql
Apache License 2.0
- 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
- Fix bugs
- Support for relative mysql_install_db links
- Use absolute path for which command
- Add timeout on terminating mysqld
- Fix bugs
- Fix ImportError if caught SIGINT on py3
- Fix testing.mysqld.Mysqld#start() fails if mysql_install_db does not create database named "test"
- Use pymysql driver as default in Mysqld#url()
- Change behavior: Mysqld#stop() cleans workdir
- Fix caught AttributeError on object deletion
- Add mysqld.skipIfNotInstalled decorator (alias of skipIfNotFound)
- Suport python 2.6 and 3.2
- Add @skipIfNotFound decorator
- Fix it does not cleanup temporary directory if Mysqld object has been deleted
- Add charset parameter to Mysqld#url()
- Rename package: test.mysqld -> testing.mysqld
- Add Mysqld#url() method (for sqlalchemy)
- First release