我想在同一测试套件中使用多个灯具来测试各种情况下的SQLAlchemy模型。什么是最简单的方法?
答案 0 :(得分:1)
我使用在测试中调用的fixture方法:
class TestMyStuff(TestCase):
def setUp(self):
self.Session = Session(bind=engine)
def tearDown(self):
self.Session.rollback()
self.Session.close()
def _fixture_one(self):
self.Session.add_all([
User(name='ed')
])
self.Session.flush()
def _fixture_two(self):
self.Session.add_all([
Address(street='123 anywhere street')
])
self.Session.flush()
def test_some_user_thing(self):
self._fixture_one()
assert self.Session.query(User.name).first() == (('ed',))
def test_some_address_thing(self):
self._fixture_two()
assert self.Session.query(Address.street).\
first() == (('123 anywhere street',))