Skip to content

Commit 496e3b1

Browse files
committed
adding internal repr for debugging
adding an example for generating multi-args/multi python tests --HG-- branch : trunk
1 parent aed6612 commit 496e3b1

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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))

py/test/funcargs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __init__(self, funcargs, id, param):
2323
self.id = id
2424
if param is not _notexists:
2525
self.param = param
26+
def __repr__(self):
27+
return "<CallSpec id=%r param=%r funcargs=%r>" %(
28+
self.id, getattr(self, 'param', '?'), self.funcargs)
2629

2730
class Metafunc:
2831
def __init__(self, function, config=None, cls=None, module=None):

testing/pytest/test_funcargs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def f(self, arg1, arg2="hello"):
1717
if sys.version_info < (3,0):
1818
assert funcargs.getfuncargnames(A.f) == ['arg1']
1919

20+
def test_callspec_repr():
21+
cs = funcargs.CallSpec({}, 'hello', 1)
22+
repr(cs)
23+
cs = funcargs.CallSpec({}, 'hello', funcargs._notexists)
24+
repr(cs)
25+
2026
class TestFillFuncArgs:
2127
def test_funcarg_lookupfails(self, testdir):
2228
testdir.makeconftest("""
@@ -314,7 +320,6 @@ def test_check_test0_has_teardown_correct():
314320
"*3 passed*"
315321
])
316322

317-
318323
class TestMetafunc:
319324
def test_no_funcargs(self, testdir):
320325
def function(): pass

0 commit comments

Comments
 (0)