adding exampels as per blog post

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-05-13 17:42:22 +02:00
parent 045a135786
commit 6bde251d68
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import py
def pytest_generate_tests(metafunc):
for funcargs in metafunc.cls.params[metafunc.function.__name__]:
metafunc.addcall(funcargs=funcargs)
class TestClass:
params = {
'test_equals': [dict(a=1, b=2), dict(a=3, b=3), dict(a=5, b=4)],
'test_zerodivision': [dict(a=1, b=0), dict(a=3, b=2)],
}
def test_equals(self, a, b):
assert a == b
def test_zerodivision(self, a, b):
py.test.raises(ZeroDivisionError, "a/b")

View File

@ -0,0 +1,25 @@
import py
# test support code
def params(funcarglist):
def wrapper(function):
function.funcarglist = funcarglist
return function
return wrapper
def pytest_generate_tests(metafunc):
for funcargs in getattr(metafunc.function, 'funcarglist', ()):
metafunc.addcall(funcargs=funcargs)
# actual test code
class TestClass:
@params([dict(a=1, b=2), dict(a=3, b=3), dict(a=5, b=4)], )
def test_equals(self, a, b):
assert a == b
@params([dict(a=1, b=0), dict(a=3, b=2)])
def test_zerodivision(self, a, b):
py.test.raises(ZeroDivisionError, "a/b")

View File

@ -0,0 +1,15 @@
# following hook can be put unchanged into a local or global plugin
def pytest_generate_tests(metafunc):
for scenario in metafunc.cls.scenarios:
metafunc.addcall(id=scenario[0], funcargs=scenario[1])
scenario1 = ('basic', {'attribute': 'value'})
scenario2 = ('advanced', {'attribute': 'value2'})
class TestSampleWithScenarios:
scenarios = [scenario1, scenario2]
def test_demo(self, attribute):
assert isinstance(attribute, str)