From ebb0de4ff794c7a3d7d9373e10a8ea5a75c55bfc Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 12 May 2009 02:05:59 +0200 Subject: [PATCH] adding _id parameter to addcall, refinements --HG-- branch : trunk --- doc/test/funcargs.txt | 9 +++++++-- py/test/funcargs.py | 26 +++++++++++++++++++------- py/test/pycollect.py | 2 +- py/test/testing/test_funcargs.py | 25 ++++++++++++++++++------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/doc/test/funcargs.txt b/doc/test/funcargs.txt index 023818c2e..7dbaf2907 100644 --- a/doc/test/funcargs.txt +++ b/doc/test/funcargs.txt @@ -41,7 +41,7 @@ Let's look at a simple example of using funcargs within a test module: one can see the original provided value. -generating test runs with multiple function argument values +generating test functions call ---------------------------------------------------------------------- You can parametrize multiple runs of a test function by @@ -161,7 +161,12 @@ to generate tests with combinations of function argument values. Calling ``funcspec.addcall(**funcargs)`` will add a tests function call using the given dictionary of function arguments. This addition of a call -happens during test collection. +happens during test collection. + +Note that you can provide a name for your test run +by passing a ``_id=...`` parameter. This name +will be shown during verbose and traceback +reporting. Attributes of funcspec objects ++++++++++++++++++++++++++++++++++++++++ diff --git a/py/test/funcargs.py b/py/test/funcargs.py index fe1120c50..e9585f01a 100644 --- a/py/test/funcargs.py +++ b/py/test/funcargs.py @@ -25,6 +25,11 @@ def fillfuncargs(function): except request.Error: request._raiselookupfailed() +class CallSpec: + def __init__(self, id, funcargs): + self.id = id + self.funcargs = funcargs + class FuncSpecs: def __init__(self, function, config=None, cls=None, module=None): self.config = config @@ -35,24 +40,31 @@ class FuncSpecs: self.module = module self._calls = [] - def addcall(self, **kwargs): + def addcall(self, _id=None, **kwargs): for argname in kwargs: + if argname[0] == "_": + raise TypeError("argument %r is not a valid keyword." % argname) if argname not in self.funcargnames: raise ValueError("function %r has no funcarg %r" %( self.function, argname)) - self._calls.append(kwargs) + if _id is None: + _id = len(self._calls) + _id = str(_id) + #if _id in self._ids: + # raise ValueError("duplicate id %r" % _id) + self._calls.append(CallSpec(_id, kwargs)) class FunctionCollector(py.test.collect.Collector): - def __init__(self, name, parent, combinations): + def __init__(self, name, parent, calls): super(FunctionCollector, self).__init__(name, parent) - self.combinations = combinations + self.calls = calls self.obj = getattr(self.parent.obj, name) def collect(self): l = [] - for i, funcargs in py.builtin.enumerate(self.combinations): - function = self.parent.Function(name="%s[%s]" %(self.name, i), - parent=self, funcargs=funcargs, callobj=self.obj) + for call in self.calls: + function = self.parent.Function(name="%s[%s]" %(self.name, call.id), + parent=self, funcargs=call.funcargs, callobj=self.obj) l.append(function) return l diff --git a/py/test/pycollect.py b/py/test/pycollect.py index 1f6871e81..4c60a4a9b 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -178,7 +178,7 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): if not funcspec._calls: return self.Function(name, parent=self) return funcargs.FunctionCollector(name=name, - parent=self, combinations=funcspec._calls) + parent=self, calls=funcspec._calls) class Module(py.test.collect.File, PyCollectorMixin): def _getobj(self): diff --git a/py/test/testing/test_funcargs.py b/py/test/testing/test_funcargs.py index 78a865cb3..6f14bbea4 100644 --- a/py/test/testing/test_funcargs.py +++ b/py/test/testing/test_funcargs.py @@ -145,6 +145,17 @@ class TestFuncSpecs: assert funcspec.function is func assert funcspec.cls is None + def test_addcall_with_id(self): + def func(arg1): pass + funcspec = funcargs.FuncSpecs(func) + py.test.raises(TypeError, """ + funcspec.addcall(_xyz=10) + """) + funcspec.addcall(_id="hello", arg1=100) + call = funcspec._calls[0] + assert call.id == "hello" + assert call.funcargs == {'arg1': 100} + def test_addcall_basic(self): def func(arg1): pass funcspec = funcargs.FuncSpecs(func) @@ -153,7 +164,7 @@ class TestFuncSpecs: """) funcspec.addcall(arg1=100) assert len(funcspec._calls) == 1 - assert funcspec._calls[0] == {'arg1': 100} + assert funcspec._calls[0].funcargs == {'arg1': 100} def test_addcall_two(self): def func(arg1): pass @@ -161,8 +172,8 @@ class TestFuncSpecs: funcspec.addcall(arg1=100) funcspec.addcall(arg1=101) assert len(funcspec._calls) == 2 - assert funcspec._calls[0] == {'arg1': 100} - assert funcspec._calls[1] == {'arg1': 101} + assert funcspec._calls[0].funcargs == {'arg1': 100} + assert funcspec._calls[1].funcargs == {'arg1': 101} class TestGenfuncFunctional: def test_attributes(self, testdir): @@ -232,11 +243,11 @@ class TestGenfuncFunctional: class ConftestPlugin: def pytest_genfunc(self, funcspec): assert "arg1" in funcspec.funcargnames - funcspec.addcall(arg1=1, arg2=2) + funcspec.addcall(_id="world", arg1=1, arg2=2) """) p = testdir.makepyfile(""" def pytest_genfunc(funcspec): - funcspec.addcall(arg1=10, arg2=10) + funcspec.addcall(_id="hello", arg1=10, arg2=10) class TestClass: def test_myfunc(self, arg1, arg2): @@ -244,7 +255,7 @@ class TestGenfuncFunctional: """) result = testdir.runpytest("-v", p) assert result.stdout.fnmatch_lines([ - "*test_myfunc*0*PASS*", - "*test_myfunc*1*FAIL*", + "*test_myfunc*hello*PASS*", + "*test_myfunc*world*FAIL*", "*1 failed, 1 passed*" ])