diff --git a/example/funcarg/parametrize/test_parametrize.py b/example/funcarg/parametrize/test_parametrize.py new file mode 100644 index 000000000..f0d2b7832 --- /dev/null +++ b/example/funcarg/parametrize/test_parametrize.py @@ -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") diff --git a/example/funcarg/parametrize/test_parametrize2.py b/example/funcarg/parametrize/test_parametrize2.py new file mode 100644 index 000000000..149691c4f --- /dev/null +++ b/example/funcarg/parametrize/test_parametrize2.py @@ -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") + diff --git a/example/funcarg/parametrize/test_parametrize3.py b/example/funcarg/parametrize/test_parametrize3.py new file mode 100644 index 000000000..7636f8c09 --- /dev/null +++ b/example/funcarg/parametrize/test_parametrize3.py @@ -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)