Classes
class TestStuff:
numbers = [(1, 1, 2)]
def test_me(self):
for n1, n2, addition in self.numbers:
assert n1 + n2 == addition
Tests with parameters
def test_lots():
numbers = [(1, 1, 2), (2, 2, 4), (5, 5, 9)]
for args in numbers:
yield (adder,) + args
def adder(n1, n2, addition):
assert n1 + n2 == addition
Decorator fixtures
Setup, teardown, can be achieved with decorators:
def setup(func):
setup_stuff()
func(value_that_was_set_up)
teardown_stuff()
@setup
def test_here(some_value): ...
param
Dissecting the
param decorator; here's the original:
def test_to_roman():
for integer, numeral in known_values:
assert roman.toRoman(integer) == numeral
param: generators
def test_to_roman():
for integer, numeral in known_values:
yield inner_test_to_roman, integer, numeral
def inner_test_to_roman(integer, numeral):
assert roman.toRoman(integer) == numeral
param: decorator
def param(param_list):
def make_param_func(func):
def yielder():
for args in param_list:
yield (func,) + args
return yielder
return decorator
@param(known_values)
def test_to_roman(integer, numeral):
assert roman.toRoman(integer) == numeral
param: decorator 2
You must pass in a
param_list like
[(arg1, arg2),
(arg1, arg2)]. We might want to be able to pass in
[arg,
arg, arg] for functions that take one argument:
def yielder():
for args in param_list:
if not isinstance(args, tuple):
yield (func, args)
else:
yield (func,) + args
param: decorator 3
Sometimes we don't want to unpack list of arguments into multiple
tests.
if expand:
return yielder
else:
def runner():
for args in yielder():
args[0](*args[1:])
return runner
py.test
Find it at http://codespeak.net/py
Find this at http://svn.colorstudy.com/home/ianb/pytest_roman