Skip to content

Instantly share code, notes, and snippets.

@hpk42
Created August 2, 2012 07:56
Show Gist options
  • Save hpk42/3235053 to your computer and use it in GitHub Desktop.
Save hpk42/3235053 to your computer and use it in GitHub Desktop.
# ---------------------------------------------------------
# the new internal code for @pytest.setup would look like this:
# ---------------------------------------------------------
class SetupFunc:
def __init__(self, scope):
self.scope = scope
def __call__(self, func):
try:
globs = func.__globals__
except AttributeError:
globs = func.func_globals
l = globs.setdefault("_pytestsetup", [])
l.append((self.scope, func))
def setup_session(func):
return SetupFunc("session")(func)
def setup_module(func):
return SetupFunc("module")(func)
def setup_class(func):
return SetupFunc("class")(func)
def setup_function(func):
return SetupFunc("function")(func)
# ---------------------------------------------------------
# some examples using this code
# ---------------------------------------------------------
# example of conftest.py
@pytest.setup_session
def dbinit(db):
# do something with db funcarg
# example test_module.py
@pytest.setup_module
def filltable(db):
# create table for this module
# the last example could be moved to a conftest.py and look
# at module-specific parameters:
# content of conftest.py
@pytest.setup_module
def filltable(testcontext, db):
tabledef = getattr(testcontext.module, "tabledef", None)
if tabledef is not None:
# create a table based on tabledef info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment