Establish the session in class methods.

These are only called once, not before and after every test.
communication
junos 2021-02-02 15:44:53 +01:00
parent dfb4236769
commit 0bc66ce4b9
1 changed files with 9 additions and 7 deletions

View File

@ -8,14 +8,16 @@ from setup import db_uri
class DatabaseConnection(unittest.TestCase): class DatabaseConnection(unittest.TestCase):
def setUp(self): @classmethod
self.engine = create_engine(db_uri, echo=True) def setUpClass(cls) -> None:
Session = sessionmaker(bind=self.engine) cls.engine = create_engine(db_uri, echo=True)
self.session = Session() Session = sessionmaker(bind=cls.engine)
cls.session = Session()
def tearDown(self): @classmethod
self.engine.dispose() def tearDownClass(cls) -> None:
self.session.close() cls.engine.dispose()
cls.session.close()
def test_connection(self): def test_connection(self):
with self.engine.connect() as connection: with self.engine.connect() as connection: