|
| 1 | +""" |
| 2 | +
|
| 3 | +module containing a parametrized tests testing cross-python |
| 4 | +serialization via the pickle module. |
| 5 | +""" |
| 6 | +import py |
| 7 | + |
| 8 | +pythonlist = ['python2.3', 'python2.4', 'python2.5', 'python2.6'] |
| 9 | +# 'jython' 'python3.1'] |
| 10 | + |
| 11 | +def pytest_generate_tests(metafunc): |
| 12 | + if 'python1' in metafunc.funcargnames: |
| 13 | + assert 'python2' in metafunc.funcargnames |
| 14 | + for obj in metafunc.function.multiarg.obj: |
| 15 | + for py1 in pythonlist: |
| 16 | + for py2 in pythonlist: |
| 17 | + metafunc.addcall(id="%s-%s-%s" % (py1, py2, obj), |
| 18 | + param=(py1, py2, obj)) |
| 19 | + |
| 20 | +@py.test.mark.multiarg(obj=[42, {}, {1:3},]) |
| 21 | +def test_basic_objects(python1, python2, obj): |
| 22 | + python1.dumps(obj) |
| 23 | + python2.load_and_is_true("obj == %s" % obj) |
| 24 | + |
| 25 | +def pytest_funcarg__python1(request): |
| 26 | + tmpdir = request.getfuncargvalue("tmpdir") |
| 27 | + picklefile = tmpdir.join("data.pickle") |
| 28 | + return Python(request.param[0], picklefile) |
| 29 | + |
| 30 | +def pytest_funcarg__python2(request): |
| 31 | + python1 = request.getfuncargvalue("python1") |
| 32 | + return Python(request.param[1], python1.picklefile) |
| 33 | + |
| 34 | +def pytest_funcarg__obj(request): |
| 35 | + return request.param[2] |
| 36 | + |
| 37 | +class Python: |
| 38 | + def __init__(self, version, picklefile): |
| 39 | + self.pythonpath = py.path.local.sysfind(version) |
| 40 | + if not self.pythonpath: |
| 41 | + py.test.skip("%r not found" %(version,)) |
| 42 | + self.picklefile = picklefile |
| 43 | + def dumps(self, obj): |
| 44 | + dumpfile = self.picklefile.dirpath("dump.py") |
| 45 | + dumpfile.write(py.code.Source(""" |
| 46 | + import pickle |
| 47 | + f = open(%r, 'wb') |
| 48 | + s = pickle.dump(%r, f) |
| 49 | + f.close() |
| 50 | + """ % (str(self.picklefile), obj))) |
| 51 | + py.process.cmdexec("%s %s" %(self.pythonpath, dumpfile)) |
| 52 | + |
| 53 | + def load_and_is_true(self, expression): |
| 54 | + loadfile = self.picklefile.dirpath("load.py") |
| 55 | + loadfile.write(py.code.Source(""" |
| 56 | + import pickle |
| 57 | + f = open(%r, 'rb') |
| 58 | + obj = pickle.load(f) |
| 59 | + f.close() |
| 60 | + res = eval(%r) |
| 61 | + if not res: |
| 62 | + raise SystemExit(1) |
| 63 | + """ % (str(self.picklefile), expression))) |
| 64 | + print loadfile |
| 65 | + py.process.cmdexec("%s %s" %(self.pythonpath, loadfile)) |
0 commit comments