自动在临时目录中设置mysqld实例,并在测试后销毁它
项目描述
testing.mysqld 自动在临时目录中设置mysqld实例,并在测试后销毁它
安装
使用easy_install(或pip)
$ easy_install testing.mysqld
并且 testing.mysqld 需要MySQL服务器在您的PATH中。
用法
使用 testing.mysqld.Mysqld 创建MySQL实例
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 在实例化时执行 mysql_install_db 和 mysqld。在删除Mysqld对象时,它终止MySQL实例并删除临时目录。
如果您需要一个包含表和任何固定件的数据库,请使用 copy_data_from 关键字
# uses a copy of specified data directory of MySQL. mysqld = testing.mysqld.Mysqld(copy_data_from='/path/to/your/database')
您可以使用 my_cnf 关键字指定MySQL的参数
# boot MySQL server without socket listener (use unix-domain socket) mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
例如,您可以在setUp()方法中为每个测试用例设置新的MySQL服务器
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 在每次实例化时调用 initdb 命令。这很简单。但在许多情况下,为每个测试用例生成全新的数据库是非常浪费的。
为了优化行为,请使用 testing.mysqld.MysqldFactory。该工厂类能够将生成的数据库缓存到测试用例之外,并减少 mysql_install_db 命令的调用次数
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()
如果您想将固定装置插入缓存数据库,请使用 initdb_handler 选项
# 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.7, 3.3, 3.4, 3.5
pymysql
许可证
Apache License 2.0
历史记录
1.4.0 (2016-08-20)
删除 py26, py32 支持
允许使用 user 和 password 参数连接授权数据库
依赖 testing.common.database >= 2.0.0
1.3.0 (2016-02-03)
为服务器调用程序添加超时
支持 MySQL-5.7
添加 testing.mysqld.MysqldFactory
依赖 testing.common.database 包
如果未禁用网络,则分配端口
1.2.8 (2015-04-06)
修复错误
1.2.7 (2014-12-20)
支持相对 mysql_install_db 链接
为 which 命令使用绝对路径
1.2.6 (2014-06-19)
在终止 mysqld 时添加超时
修复错误
1.2.5 (2014-06-11)
修复在 py3 中捕获 SIGINT 时导入错误
1.2.4 (2014-02-13)
修复如果 mysql_install_db 没有创建名为“test”的数据库时,testing.mysqld.Mysqld#start() 失败的问题
1.2.3 (2013-12-11)
在 Mysqld#url() 中使用 pymysql 驱动器作为默认选项
1.2.2 (2013-12-06)
更改行为:Mysqld#stop() 清理工作目录
修复对象删除时捕获的 AttributeError
1.2.1 (2013-12-05)
添加 mysqld.skipIfNotInstalled 装饰器(skipIfNotFound 的别名)
支持 python 2.6 和 3.2
1.2.0 (2013-12-04)
添加 @skipIfNotFound 装饰器
1.1.2 (2013-11-26)
修复如果 Mysqld 对象已被删除,则不会清理临时目录的问题
1.1.1 (2013-11-25)
将字符集参数添加到 Mysqld#url()
1.1.0 (2013-11-22)
重命名包:test.mysqld -> testing.mysqld
添加 Mysqld#url() 方法(用于 sqlalchemy)
1.0.0 (2013-10-17)
首次发布