Skip to content

Instantly share code, notes, and snippets.

@s7anley
Last active March 28, 2017 03:19
Show Gist options
  • Save s7anley/ef5e3f01dc51e2471ee4481f4cb7fc25 to your computer and use it in GitHub Desktop.
Save s7anley/ef5e3f01dc51e2471ee4481f4cb7fc25 to your computer and use it in GitHub Desktop.
Reusable setup and teardown for pytest
import pytest
def populate_tables(tables):
pass
def truncate_tables(tables):
pass
@pytest.fixture()
def database_fixture(request):
tables = request.param.get('tables', {})
populate_tables(tables)
yield # Teardown
truncate_tables(tables.keys())
@pytest.mark.parametrize('database_fixture', [
# Only one use case, since we are using values in fixture instead of
# testing method itself
(
{
'tables': {
'user': {
'id': 1,
'name': 'Foobar'
}
}
},
)
], indirect=True)
def test_setup_and_teardown(database_fixture):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment