Integrity Tests: pytest
Learn how to write integrity tests using pytest.
Integrity tests using pytest
In some cases, periodically checking for data issues is insufficient. Instead, we can clean the data using functions. These must be tested on a separate instance of a database with the unclean data purposely put in place, using separate fixtures for clean and unclean test data. We need a new function that drops the data but keeps the schema, but be sure to only run this on the test connection.
Press + to interact
def drop_tables():for Table in [Employee, Project, Assignment]:session.query(Table).delete()
The fixture uses this to define a clean-data session.
Press + to interact
import jsonimport pytestfrom flask_orm2 import drop_tables, add_sample_data, clean_datafrom flask_orm2 import Employee, Project, Assignment, session@pytest.fixture(scope='module')def db_session_clean():'''Creates a database and populates it with clean data'''drop_tables() # drop tables, but keep schemaadd_sample_data() # populate uncleanlyclean_data()yield session
...
Access this course and 1400+ top-rated courses and projects.