diff --git a/example/funcarg/test_multi_python.py b/example/funcarg/test_multi_python.py new file mode 100644 index 000000000..9c89f94ba --- /dev/null +++ b/example/funcarg/test_multi_python.py @@ -0,0 +1,65 @@ +""" + +module containing a parametrized tests testing cross-python +serialization via the pickle module. +""" +import py + +pythonlist = ['python2.3', 'python2.4', 'python2.5', 'python2.6'] +# 'jython' 'python3.1'] + +def pytest_generate_tests(metafunc): + if 'python1' in metafunc.funcargnames: + assert 'python2' in metafunc.funcargnames + for obj in metafunc.function.multiarg.obj: + for py1 in pythonlist: + for py2 in pythonlist: + metafunc.addcall(id="%s-%s-%s" % (py1, py2, obj), + param=(py1, py2, obj)) + +@py.test.mark.multiarg(obj=[42, {}, {1:3},]) +def test_basic_objects(python1, python2, obj): + python1.dumps(obj) + python2.load_and_is_true("obj == %s" % obj) + +def pytest_funcarg__python1(request): + tmpdir = request.getfuncargvalue("tmpdir") + picklefile = tmpdir.join("data.pickle") + return Python(request.param[0], picklefile) + +def pytest_funcarg__python2(request): + python1 = request.getfuncargvalue("python1") + return Python(request.param[1], python1.picklefile) + +def pytest_funcarg__obj(request): + return request.param[2] + +class Python: + def __init__(self, version, picklefile): + self.pythonpath = py.path.local.sysfind(version) + if not self.pythonpath: + py.test.skip("%r not found" %(version,)) + self.picklefile = picklefile + def dumps(self, obj): + dumpfile = self.picklefile.dirpath("dump.py") + dumpfile.write(py.code.Source(""" + import pickle + f = open(%r, 'wb') + s = pickle.dump(%r, f) + f.close() + """ % (str(self.picklefile), obj))) + py.process.cmdexec("%s %s" %(self.pythonpath, dumpfile)) + + def load_and_is_true(self, expression): + loadfile = self.picklefile.dirpath("load.py") + loadfile.write(py.code.Source(""" + import pickle + f = open(%r, 'rb') + obj = pickle.load(f) + f.close() + res = eval(%r) + if not res: + raise SystemExit(1) + """ % (str(self.picklefile), expression))) + print loadfile + py.process.cmdexec("%s %s" %(self.pythonpath, loadfile)) diff --git a/py/test/funcargs.py b/py/test/funcargs.py index 4f9d6df68..bbf043ae3 100644 --- a/py/test/funcargs.py +++ b/py/test/funcargs.py @@ -23,6 +23,9 @@ class CallSpec: self.id = id if param is not _notexists: self.param = param + def __repr__(self): + return "" %( + self.id, getattr(self, 'param', '?'), self.funcargs) class Metafunc: def __init__(self, function, config=None, cls=None, module=None): diff --git a/testing/pytest/test_funcargs.py b/testing/pytest/test_funcargs.py index 144f0c1f8..795b9bc07 100644 --- a/testing/pytest/test_funcargs.py +++ b/testing/pytest/test_funcargs.py @@ -17,6 +17,12 @@ def test_getfuncargnames(): if sys.version_info < (3,0): assert funcargs.getfuncargnames(A.f) == ['arg1'] +def test_callspec_repr(): + cs = funcargs.CallSpec({}, 'hello', 1) + repr(cs) + cs = funcargs.CallSpec({}, 'hello', funcargs._notexists) + repr(cs) + class TestFillFuncArgs: def test_funcarg_lookupfails(self, testdir): testdir.makeconftest(""" @@ -314,7 +320,6 @@ class TestRequestCachedSetup: "*3 passed*" ]) - class TestMetafunc: def test_no_funcargs(self, testdir): def function(): pass