Last active
March 28, 2017 03:19
-
-
Save s7anley/ef5e3f01dc51e2471ee4481f4cb7fc25 to your computer and use it in GitHub Desktop.
Reusable setup and teardown for pytest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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