move examples to doc directory
--HG-- branch : trunk
This commit is contained in:
3
doc/example/funcarg/conftest.py
Normal file
3
doc/example/funcarg/conftest.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import py
|
||||
|
||||
collect_ignore = 'mysetup', 'mysetup2', 'test_simpleprovider.py', 'parametrize'
|
||||
16
doc/example/funcarg/costlysetup/conftest.py
Normal file
16
doc/example/funcarg/costlysetup/conftest.py
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
def pytest_funcarg__setup(request):
|
||||
return request.cached_setup(
|
||||
setup=lambda: CostlySetup(),
|
||||
teardown=lambda costlysetup: costlysetup.finalize(),
|
||||
scope="session",
|
||||
)
|
||||
|
||||
class CostlySetup:
|
||||
def __init__(self):
|
||||
import time
|
||||
time.sleep(5)
|
||||
self.timecostly = 1
|
||||
|
||||
def finalize(self):
|
||||
del self.timecostly
|
||||
1
doc/example/funcarg/costlysetup/sub1/__init__.py
Normal file
1
doc/example/funcarg/costlysetup/sub1/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
#
|
||||
3
doc/example/funcarg/costlysetup/sub1/test_quick.py
Normal file
3
doc/example/funcarg/costlysetup/sub1/test_quick.py
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
def test_quick():
|
||||
pass
|
||||
1
doc/example/funcarg/costlysetup/sub2/__init__.py
Normal file
1
doc/example/funcarg/costlysetup/sub2/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
#
|
||||
6
doc/example/funcarg/costlysetup/sub2/test_two.py
Normal file
6
doc/example/funcarg/costlysetup/sub2/test_two.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def test_something(setup):
|
||||
assert setup.timecostly == 1
|
||||
|
||||
def test_something_more(setup):
|
||||
assert setup.timecostly == 1
|
||||
|
||||
1
doc/example/funcarg/mysetup/__init__.py
Normal file
1
doc/example/funcarg/mysetup/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# XXX this file should not need to be here but is here for proper sys.path mangling
|
||||
9
doc/example/funcarg/mysetup/conftest.py
Normal file
9
doc/example/funcarg/mysetup/conftest.py
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
from mysetup.myapp import MyApp
|
||||
|
||||
def pytest_funcarg__mysetup(request):
|
||||
return MySetup()
|
||||
|
||||
class MySetup:
|
||||
def myapp(self):
|
||||
return MyApp()
|
||||
5
doc/example/funcarg/mysetup/myapp.py
Normal file
5
doc/example/funcarg/mysetup/myapp.py
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
class MyApp:
|
||||
def question(self):
|
||||
return 6 * 9
|
||||
|
||||
5
doc/example/funcarg/mysetup/test_sample.py
Normal file
5
doc/example/funcarg/mysetup/test_sample.py
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
def test_answer(mysetup):
|
||||
app = mysetup.myapp()
|
||||
answer = app.question()
|
||||
assert answer == 42
|
||||
1
doc/example/funcarg/mysetup2/__init__.py
Normal file
1
doc/example/funcarg/mysetup2/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# XXX this file should not need to be here but is here for proper sys.path mangling
|
||||
24
doc/example/funcarg/mysetup2/conftest.py
Normal file
24
doc/example/funcarg/mysetup2/conftest.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import py
|
||||
from mysetup2.myapp import MyApp
|
||||
|
||||
def pytest_funcarg__mysetup(request):
|
||||
return MySetup(request)
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--ssh", action="store", default=None,
|
||||
help="specify ssh host to run tests with")
|
||||
|
||||
|
||||
class MySetup:
|
||||
def __init__(self, request):
|
||||
self.config = request.config
|
||||
|
||||
def myapp(self):
|
||||
return MyApp()
|
||||
|
||||
def getsshconnection(self):
|
||||
host = self.config.option.ssh
|
||||
if host is None:
|
||||
py.test.skip("specify ssh host with --ssh")
|
||||
return execnet.SshGateway(host)
|
||||
|
||||
5
doc/example/funcarg/mysetup2/myapp.py
Normal file
5
doc/example/funcarg/mysetup2/myapp.py
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
class MyApp:
|
||||
def question(self):
|
||||
return 6 * 9
|
||||
|
||||
6
doc/example/funcarg/mysetup2/test_sample.py
Normal file
6
doc/example/funcarg/mysetup2/test_sample.py
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
def test_answer(mysetup):
|
||||
app = mysetup.myapp()
|
||||
answer = app.question()
|
||||
assert answer == 42
|
||||
|
||||
5
doc/example/funcarg/mysetup2/test_ssh.py
Normal file
5
doc/example/funcarg/mysetup2/test_ssh.py
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
class TestClass:
|
||||
def test_function(self, mysetup):
|
||||
conn = mysetup.getsshconnection()
|
||||
# work with conn
|
||||
17
doc/example/funcarg/parametrize/test_parametrize.py
Normal file
17
doc/example/funcarg/parametrize/test_parametrize.py
Normal file
@@ -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")
|
||||
25
doc/example/funcarg/parametrize/test_parametrize2.py
Normal file
25
doc/example/funcarg/parametrize/test_parametrize2.py
Normal file
@@ -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")
|
||||
|
||||
15
doc/example/funcarg/parametrize/test_parametrize3.py
Normal file
15
doc/example/funcarg/parametrize/test_parametrize3.py
Normal file
@@ -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)
|
||||
65
doc/example/funcarg/test_multi_python.py
Normal file
65
doc/example/funcarg/test_multi_python.py
Normal file
@@ -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))
|
||||
7
doc/example/funcarg/test_simpleprovider.py
Normal file
7
doc/example/funcarg/test_simpleprovider.py
Normal file
@@ -0,0 +1,7 @@
|
||||
# ./test_simpleprovider.py
|
||||
def pytest_funcarg__myfuncarg(request):
|
||||
return 42
|
||||
|
||||
def test_function(myfuncarg):
|
||||
assert myfuncarg == 17
|
||||
|
||||
15
doc/example/funcarg/urloption/conftest.py
Normal file
15
doc/example/funcarg/urloption/conftest.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# conftest.py
|
||||
import py
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
grp = parser.getgroup("testserver options")
|
||||
grp.addoption("--url", action="store", default=None,
|
||||
help="url for testserver")
|
||||
|
||||
def pytest_funcarg__url(request):
|
||||
url = request.config.getvalue("url")
|
||||
if url is None:
|
||||
py.test.skip("need --url")
|
||||
return url
|
||||
|
||||
Reference in New Issue
Block a user