move examples to doc directory
--HG-- branch : trunk
This commit is contained in:
@@ -1,122 +0,0 @@
|
||||
from py.test import raises
|
||||
import py
|
||||
|
||||
def otherfunc(a,b):
|
||||
assert a==b
|
||||
|
||||
def somefunc(x,y):
|
||||
otherfunc(x,y)
|
||||
|
||||
def otherfunc_multi(a,b):
|
||||
assert (a ==
|
||||
b)
|
||||
|
||||
def test_generative(param1, param2):
|
||||
assert param1 * 2 < param2
|
||||
|
||||
def pytest_generate_tests(metafunc):
|
||||
if 'param1' in metafunc.funcargnames:
|
||||
metafunc.addcall(funcargs=dict(param1=3, param2=6))
|
||||
|
||||
class TestFailing(object):
|
||||
def test_simple(self):
|
||||
def f():
|
||||
return 42
|
||||
def g():
|
||||
return 43
|
||||
|
||||
assert f() == g()
|
||||
|
||||
def test_simple_multiline(self):
|
||||
otherfunc_multi(
|
||||
42,
|
||||
6*9)
|
||||
|
||||
def test_not(self):
|
||||
def f():
|
||||
return 42
|
||||
assert not f()
|
||||
|
||||
def test_complex_error(self):
|
||||
def f():
|
||||
return 44
|
||||
def g():
|
||||
return 43
|
||||
somefunc(f(), g())
|
||||
|
||||
def test_z1_unpack_error(self):
|
||||
l = []
|
||||
a,b = l
|
||||
|
||||
def test_z2_type_error(self):
|
||||
l = 3
|
||||
a,b = l
|
||||
|
||||
def test_startswith(self):
|
||||
s = "123"
|
||||
g = "456"
|
||||
assert s.startswith(g)
|
||||
|
||||
def test_startswith_nested(self):
|
||||
def f():
|
||||
return "123"
|
||||
def g():
|
||||
return "456"
|
||||
assert f().startswith(g())
|
||||
|
||||
def test_global_func(self):
|
||||
assert isinstance(globf(42), float)
|
||||
|
||||
def test_instance(self):
|
||||
self.x = 6*7
|
||||
assert self.x != 42
|
||||
|
||||
def test_compare(self):
|
||||
assert globf(10) < 5
|
||||
|
||||
def test_try_finally(self):
|
||||
x = 1
|
||||
try:
|
||||
assert x == 0
|
||||
finally:
|
||||
x = 0
|
||||
|
||||
def test_raises(self):
|
||||
s = 'qwe'
|
||||
raises(TypeError, "int(s)")
|
||||
|
||||
def test_raises_doesnt(self):
|
||||
raises(IOError, "int('3')")
|
||||
|
||||
def test_raise(self):
|
||||
raise ValueError("demo error")
|
||||
|
||||
def test_tupleerror(self):
|
||||
a,b = [1]
|
||||
|
||||
def test_reinterpret_fails_with_print_for_the_fun_of_it(self):
|
||||
l = [1,2,3]
|
||||
print ("l is %r" % l)
|
||||
a,b = l.pop()
|
||||
|
||||
def test_some_error(self):
|
||||
if namenotexi:
|
||||
pass
|
||||
|
||||
def func1(self):
|
||||
assert 41 == 42
|
||||
|
||||
|
||||
# thanks to Matthew Scott for this test
|
||||
def test_dynamic_compile_shows_nicely():
|
||||
src = 'def foo():\n assert 1 == 0\n'
|
||||
name = 'abc-123'
|
||||
module = py.std.imp.new_module(name)
|
||||
code = py.code.compile(src, name, 'exec')
|
||||
py.builtin.exec_(code, module.__dict__)
|
||||
py.std.sys.modules[name] = module
|
||||
module.foo()
|
||||
|
||||
|
||||
def globf(x):
|
||||
return x+1
|
||||
@@ -1,7 +0,0 @@
|
||||
import py
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
if isinstance(item, py.test.collect.Function):
|
||||
mod = item.getparent(py.test.collect.Module).obj
|
||||
if hasattr(mod, 'hello'):
|
||||
py.builtin.print_("mod.hello", mod.hello)
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
hello = "world"
|
||||
|
||||
def test_func():
|
||||
pass
|
||||
@@ -1,14 +0,0 @@
|
||||
|
||||
import py
|
||||
failure_demo = py.magic.autopath().dirpath('failure_demo.py')
|
||||
|
||||
pytest_plugins = "pytest_pytester"
|
||||
|
||||
def test_failure_demo_fails_properly(testdir):
|
||||
reprec = testdir.inline_run(failure_demo)
|
||||
passed, skipped, failed = reprec.countoutcomes()
|
||||
assert passed == 0
|
||||
assert failed == 20, failed
|
||||
colreports = reprec.getreports("pytest_collectreport")
|
||||
failed = len([x.failed for x in colreports])
|
||||
assert failed == 4
|
||||
@@ -1,42 +0,0 @@
|
||||
def setup_module(module):
|
||||
module.TestStateFullThing.classcount = 0
|
||||
|
||||
class TestStateFullThing:
|
||||
def setup_class(cls):
|
||||
cls.classcount += 1
|
||||
|
||||
def teardown_class(cls):
|
||||
cls.classcount -= 1
|
||||
|
||||
def setup_method(self, method):
|
||||
self.id = eval(method.__name__[5:])
|
||||
|
||||
def test_42(self):
|
||||
assert self.classcount == 1
|
||||
assert self.id == 42
|
||||
|
||||
def test_23(self):
|
||||
assert self.classcount == 1
|
||||
assert self.id == 23
|
||||
|
||||
def teardown_module(module):
|
||||
assert module.TestStateFullThing.classcount == 0
|
||||
|
||||
""" For this example the control flow happens as follows::
|
||||
import test_setup_flow_example
|
||||
setup_module(test_setup_flow_example)
|
||||
setup_class(TestStateFullThing)
|
||||
instance = TestStateFullThing()
|
||||
setup_method(instance, instance.test_42)
|
||||
instance.test_42()
|
||||
setup_method(instance, instance.test_23)
|
||||
instance.test_23()
|
||||
teardown_class(TestStateFullThing)
|
||||
teardown_module(test_setup_flow_example)
|
||||
|
||||
Note that ``setup_class(TestStateFullThing)`` is called and not
|
||||
``TestStateFullThing.setup_class()`` which would require you
|
||||
to insert ``setup_class = classmethod(setup_class)`` to make
|
||||
your setup function callable.
|
||||
"""
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
import py
|
||||
|
||||
collect_ignore = 'mysetup', 'mysetup2', 'test_simpleprovider.py', 'parametrize'
|
||||
@@ -1,16 +0,0 @@
|
||||
|
||||
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 +0,0 @@
|
||||
#
|
||||
@@ -1,3 +0,0 @@
|
||||
|
||||
def test_quick():
|
||||
pass
|
||||
@@ -1 +0,0 @@
|
||||
#
|
||||
@@ -1,6 +0,0 @@
|
||||
def test_something(setup):
|
||||
assert setup.timecostly == 1
|
||||
|
||||
def test_something_more(setup):
|
||||
assert setup.timecostly == 1
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
# XXX this file should not need to be here but is here for proper sys.path mangling
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
from mysetup.myapp import MyApp
|
||||
|
||||
def pytest_funcarg__mysetup(request):
|
||||
return MySetup()
|
||||
|
||||
class MySetup:
|
||||
def myapp(self):
|
||||
return MyApp()
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
class MyApp:
|
||||
def question(self):
|
||||
return 6 * 9
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
def test_answer(mysetup):
|
||||
app = mysetup.myapp()
|
||||
answer = app.question()
|
||||
assert answer == 42
|
||||
@@ -1 +0,0 @@
|
||||
# XXX this file should not need to be here but is here for proper sys.path mangling
|
||||
@@ -1,24 +0,0 @@
|
||||
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)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
class MyApp:
|
||||
def question(self):
|
||||
return 6 * 9
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
|
||||
def test_answer(mysetup):
|
||||
app = mysetup.myapp()
|
||||
answer = app.question()
|
||||
assert answer == 42
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
class TestClass:
|
||||
def test_function(self, mysetup):
|
||||
conn = mysetup.getsshconnection()
|
||||
# work with conn
|
||||
@@ -1,17 +0,0 @@
|
||||
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")
|
||||
@@ -1,25 +0,0 @@
|
||||
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")
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
|
||||
# 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)
|
||||
@@ -1,65 +0,0 @@
|
||||
"""
|
||||
|
||||
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))
|
||||
@@ -1,7 +0,0 @@
|
||||
# ./test_simpleprovider.py
|
||||
def pytest_funcarg__myfuncarg(request):
|
||||
return 42
|
||||
|
||||
def test_function(myfuncarg):
|
||||
assert myfuncarg == 17
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
# 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
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
from py.xml import html
|
||||
|
||||
paras = "First Para", "Second para"
|
||||
|
||||
doc = html.html(
|
||||
html.head(
|
||||
html.meta(name="Content-Type", value="text/html; charset=latin1")),
|
||||
html.body(
|
||||
[html.p(p) for p in paras]))
|
||||
|
||||
print unicode(doc).encode('latin1')
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import py
|
||||
html = py.xml.html
|
||||
|
||||
class my(html):
|
||||
"a custom style"
|
||||
class body(html.body):
|
||||
style = html.Style(font_size = "120%")
|
||||
|
||||
class h2(html.h2):
|
||||
style = html.Style(background = "grey")
|
||||
|
||||
class p(html.p):
|
||||
style = html.Style(font_weight="bold")
|
||||
|
||||
doc = my.html(
|
||||
my.head(),
|
||||
my.body(
|
||||
my.h2("hello world"),
|
||||
my.p("bold as bold can")
|
||||
)
|
||||
)
|
||||
|
||||
print doc.unicode(indent=2)
|
||||
@@ -1,17 +0,0 @@
|
||||
|
||||
import py
|
||||
class ns(py.xml.Namespace):
|
||||
pass
|
||||
|
||||
doc = ns.books(
|
||||
ns.book(
|
||||
ns.author("May Day"),
|
||||
ns.title("python for java programmers"),),
|
||||
ns.book(
|
||||
ns.author("why", class_="somecssclass"),
|
||||
ns.title("Java for Python programmers"),),
|
||||
publisher="N.N",
|
||||
)
|
||||
print doc.unicode(indent=2).encode('utf8')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user