2011-06-06 78 views

回答

1

我使用被測試中調用夾具方法:

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',))