我有一系列简单的集成测试,我想在每次测试后发出回滚。我面临的问题是,当我看到在控制台中发出回滚声明时(我的配置中有SQLALCHEMY_ECHO = True
),数据仍然存在于数据库中。显然,我希望每次测试后插入的数据都会从数据库中删除。
这是一个示例,只有足够的代码来触发我的问题 -
import pytest
from app import create_app
from app import db as _db
from app.main.models import Customer
@pytest.yield_fixture(scope='session')
def app():
app = create_app('testing')
app_ctx = app.app_context()
app_ctx.push()
yield app
app_ctx.pop()
@pytest.yield_fixture(scope='session')
def db(app):
_db.app = app
_db.create_all()
yield _db
print('drop_all()')
@pytest.yield_fixture(scope='function')
def session(db):
conn = db.engine.connect()
trans = conn.begin()
session = db.create_scoped_session(options={ 'bind': conn })
db.session = session
yield session
trans.rollback()
conn.close()
session.remove()
def test_foo(session, app):
cust = Customer(name='foo')
session.add(cust)
cust2 = Customer(name='bar')
session.add(cust2)
session.commit()
client = app.test_client()
response = client.get('/api/customers')
from json import loads
json_response = loads(response.data.decode('utf-8'))
custs = json_response['customers']
assert len(custs) == 2
assert custs[0]['name'] == 'foo'
SQLAlchemy log -
2015-09-24 13:56:27,601 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-09-24 13:56:27,667 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-09-24 13:56:27,669 INFO sqlalchemy.engine.base.Engine INSERT INTO customers (name) OUTPUT inserted.id VALUES (?)
2015-09-24 13:56:27,669 INFO sqlalchemy.engine.base.Engine ('foo')
2015-09-24 13:56:27,715 INFO sqlalchemy.engine.base.Engine INSERT INTO customers (name) OUTPUT inserted.id VALUES (?)
2015-09-24 13:56:27,715 INFO sqlalchemy.engine.base.Engine ('bar')
2015-09-24 13:56:27,749 INFO sqlalchemy.engine.base.Engine COMMIT
2015-09-24 13:56:27,779 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-09-24 13:56:27,779 INFO sqlalchemy.engine.base.Engine SELECT customers.id AS customers_id, customers.name AS customers_name FROM customers
2015-09-24 13:56:27,780 INFO sqlalchemy.engine.base.Engine ()
2015-09-24 13:56:27,827 INFO sqlalchemy.engine.base.Engine ROLLBACK
2015-09-24 13:56:27,827 INFO sqlalchemy.engine.base.Engine ROLLBACK
正如您所看到的,我有3个隐式事务正在启动,但只有2个回滚。为什么我在回滚测试会话期间开始的交易不是?
我已经阅读过关于在测试过程中回滚的SQLAlchemy的文档,但没有任何内容。