Is there a way to automatically setup a clean db/table when running pytest
unit test cases along with peewee
?
Currently what i'm doing is as shown below,
@pytest.fixture(autouse=True)
def before_after(tmpdir):
"""Fixture to execute before and after a test is run"""
# Before:
MyTable.create_table(safe=True)
MyOtherTable.create_table(safe=True)
# MyTable.truncate_table()
# MyOtherTable.truncate_table()
yield # test
# After:
# MyTable.truncate_table()
# MyOtherTable.truncate_table()
MyTable.drop_table(safe=True)
MyOtherTable.drop_table(safe=True)
The table data persists across the tests. So is there any other way to run each test in its own isolated environment other than creating and dropping tables between tests?
You can begin a transaction at the beginning of each testcase and then roll it back after each case is over.
With regular Python unittest, you might do something like:
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.txn = db.transaction().__enter__()
def tearDown(self):
self.txn.rollback()