【发布时间】:2017-11-07 18:46:02
【问题描述】:
我正在使用 pytest 作为框架来测试我的应用程序,我也想使用 pytest factoryboy。到目前为止,我的 conftest.py 看起来很像这个例子:
import factory
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from model import model
engine = create_engine('sqlite://')
session = scoped_session(sessionmaker(bind=engine))
# Create tables
model.Base.metadata.create_all(engine)
class ExampleFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = model.ExampleClass
sqlalchemy_session = session
label = factory.Sequence(lambda n: u'object_%d' % n)
我有多个这样的工厂。问题是当我以这种方式使用工厂时,会话不会在每个单元测试中被拆除。我基本上使用一个大型会话来进行我拥有的大量单元测试。不是很理想。使用固定装置,我可以在每个单元测试中刷新一个会话。有没有办法使用 factoryboy pytest 做到这一点?
【问题讨论】:
标签: python unit-testing pytest factory-boy