automatically setups a mysqld instance in a temporary directory, and destroys it after testing
APACHE-2.0 License
testing.mysqld
automatically setups a mysqld instance in a temporary directory, and destroys it after testing
.. image:: https://travis-ci.org/tk0miya/testing.mysqld.svg?branch=master :target: https://travis-ci.org/tk0miya/testing.mysqld
.. image:: https://coveralls.io/repos/tk0miya/testing.mysqld/badge.png?branch=master :target: https://coveralls.io/r/tk0miya/testing.mysqld?branch=master
.. image:: https://codeclimate.com/github/tk0miya/testing.mysqld/badges/gpa.svg :target: https://codeclimate.com/github/tk0miya/testing.mysqld
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
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...
#
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::
mysqld = testing.mysqld.Mysqld(copy_data_from='/path/to/your/database')
You can specify parameters for MySQL with my_cnf
keyword::
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
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::
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()
handler()
on initialize databaseMysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True, on_initialized=handler)
Apache License 2.0
user
and password
argument to connect authorized databasetesting.common.database
package