2009-02-27 18:18:27 +08:00
|
|
|
import py
|
|
|
|
|
2009-03-19 01:54:45 +08:00
|
|
|
from py.__.test.outcome import Skipped
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestModule:
|
|
|
|
def test_module_file_not_found(self, testdir):
|
|
|
|
tmpdir = testdir.tmpdir
|
|
|
|
fn = tmpdir.join('nada','no')
|
|
|
|
col = py.test.collect.Module(fn, config=testdir.parseconfig(tmpdir))
|
|
|
|
py.test.raises(py.error.ENOENT, col.collect)
|
|
|
|
|
|
|
|
def test_failing_import(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("import alksdjalskdjalkjals")
|
|
|
|
py.test.raises(ImportError, modcol.collect)
|
|
|
|
py.test.raises(ImportError, modcol.collect)
|
|
|
|
py.test.raises(ImportError, modcol.run)
|
|
|
|
|
|
|
|
def test_syntax_error_in_module(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("this is a syntax error")
|
|
|
|
py.test.raises(SyntaxError, modcol.collect)
|
|
|
|
py.test.raises(SyntaxError, modcol.collect)
|
|
|
|
py.test.raises(SyntaxError, modcol.run)
|
|
|
|
|
2009-04-04 01:45:25 +08:00
|
|
|
def test_module_assertion_setup(self, testdir, monkeypatch):
|
2009-02-27 18:18:27 +08:00
|
|
|
modcol = testdir.getmodulecol("pass")
|
|
|
|
from py.__.magic import assertion
|
|
|
|
l = []
|
2009-04-04 01:45:25 +08:00
|
|
|
monkeypatch.setattr(assertion, "invoke", lambda: l.append(None))
|
|
|
|
modcol.setup()
|
2009-02-27 18:18:27 +08:00
|
|
|
x = l.pop()
|
|
|
|
assert x is None
|
2009-04-04 01:45:25 +08:00
|
|
|
monkeypatch.setattr(assertion, "revoke", lambda: l.append(None))
|
|
|
|
modcol.teardown()
|
2009-02-27 18:18:27 +08:00
|
|
|
x = l.pop()
|
|
|
|
assert x is None
|
|
|
|
|
|
|
|
def test_module_participates_as_plugin(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("")
|
|
|
|
modcol.setup()
|
2009-03-18 07:48:07 +08:00
|
|
|
assert modcol.config.pytestplugins.isregistered(modcol.obj)
|
2009-02-27 18:18:27 +08:00
|
|
|
modcol.teardown()
|
2009-03-18 07:48:07 +08:00
|
|
|
assert not modcol.config.pytestplugins.isregistered(modcol.obj)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_module_considers_pytestplugins_at_import(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',")
|
|
|
|
py.test.raises(ImportError, "modcol.obj")
|
|
|
|
|
2009-03-19 01:54:45 +08:00
|
|
|
def test_disabled_module(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
disabled = True
|
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError
|
|
|
|
def test_method():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
l = modcol.collect()
|
|
|
|
assert len(l) == 1
|
|
|
|
py.test.raises(Skipped, "modcol.setup()")
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestClass:
|
|
|
|
def test_disabled_class(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
class TestClass:
|
|
|
|
disabled = True
|
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
l = modcol.collect()
|
|
|
|
assert len(l) == 1
|
|
|
|
modcol = l[0]
|
|
|
|
assert isinstance(modcol, py.test.collect.Class)
|
2009-03-19 01:54:45 +08:00
|
|
|
l = modcol.collect()
|
|
|
|
assert len(l) == 1
|
|
|
|
py.test.raises(Skipped, "modcol.setup()")
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestGenerator:
|
|
|
|
def test_generative_functions(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
|
|
|
|
def test_gen():
|
|
|
|
yield func1, 17, 3*5
|
|
|
|
yield func1, 42, 6*7
|
|
|
|
""")
|
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, py.test.collect.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], py.test.collect.Function)
|
|
|
|
assert isinstance(gencolitems[1], py.test.collect.Function)
|
|
|
|
assert gencolitems[0].name == '[0]'
|
|
|
|
assert gencolitems[0].obj.func_name == 'func1'
|
|
|
|
|
|
|
|
def test_generative_methods(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
class TestGenMethods:
|
|
|
|
def test_gen(self):
|
|
|
|
yield func1, 17, 3*5
|
|
|
|
yield func1, 42, 6*7
|
|
|
|
""")
|
|
|
|
gencol = modcol.collect()[0].collect()[0].collect()[0]
|
|
|
|
assert isinstance(gencol, py.test.collect.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], py.test.collect.Function)
|
|
|
|
assert isinstance(gencolitems[1], py.test.collect.Function)
|
|
|
|
assert gencolitems[0].name == '[0]'
|
|
|
|
assert gencolitems[0].obj.func_name == 'func1'
|
|
|
|
|
|
|
|
def test_generative_functions_with_explicit_names(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
|
|
|
|
def test_gen():
|
|
|
|
yield "seventeen", func1, 17, 3*5
|
|
|
|
yield "fortytwo", func1, 42, 6*7
|
|
|
|
""")
|
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, py.test.collect.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], py.test.collect.Function)
|
|
|
|
assert isinstance(gencolitems[1], py.test.collect.Function)
|
|
|
|
assert gencolitems[0].name == "['seventeen']"
|
|
|
|
assert gencolitems[0].obj.func_name == 'func1'
|
|
|
|
assert gencolitems[1].name == "['fortytwo']"
|
|
|
|
assert gencolitems[1].obj.func_name == 'func1'
|
|
|
|
|
2009-03-19 22:34:33 +08:00
|
|
|
def test_generative_functions_unique_explicit_names(self, testdir):
|
|
|
|
# generative
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def func(): pass
|
|
|
|
def test_gen():
|
|
|
|
yield "name", func
|
|
|
|
yield "name", func
|
|
|
|
""")
|
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, py.test.collect.Generator)
|
|
|
|
py.test.raises(ValueError, "gencol.collect()")
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_generative_methods_with_explicit_names(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
class TestGenMethods:
|
|
|
|
def test_gen(self):
|
|
|
|
yield "m1", func1, 17, 3*5
|
|
|
|
yield "m2", func1, 42, 6*7
|
|
|
|
""")
|
|
|
|
gencol = modcol.collect()[0].collect()[0].collect()[0]
|
|
|
|
assert isinstance(gencol, py.test.collect.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], py.test.collect.Function)
|
|
|
|
assert isinstance(gencolitems[1], py.test.collect.Function)
|
|
|
|
assert gencolitems[0].name == "['m1']"
|
|
|
|
assert gencolitems[0].obj.func_name == 'func1'
|
|
|
|
assert gencolitems[1].name == "['m2']"
|
|
|
|
assert gencolitems[1].obj.func_name == 'func1'
|
|
|
|
|
|
|
|
def test_order_of_execution_generator_same_codeline(self, testdir, tmpdir):
|
|
|
|
o = testdir.makepyfile("""
|
|
|
|
def test_generative_order_of_execution():
|
|
|
|
test_list = []
|
|
|
|
expected_list = range(6)
|
|
|
|
|
|
|
|
def list_append(item):
|
|
|
|
test_list.append(item)
|
|
|
|
|
|
|
|
def assert_order_of_execution():
|
|
|
|
print 'expected order', expected_list
|
|
|
|
print 'but got ', test_list
|
|
|
|
assert test_list == expected_list
|
|
|
|
|
|
|
|
for i in expected_list:
|
|
|
|
yield list_append, i
|
|
|
|
yield assert_order_of_execution
|
|
|
|
""")
|
|
|
|
sorter = testdir.inline_run(o)
|
|
|
|
passed, skipped, failed = sorter.countoutcomes()
|
|
|
|
assert passed == 7
|
|
|
|
assert not skipped and not failed
|
|
|
|
|
|
|
|
def test_order_of_execution_generator_different_codeline(self, testdir):
|
|
|
|
o = testdir.makepyfile("""
|
|
|
|
def test_generative_tests_different_codeline():
|
|
|
|
test_list = []
|
|
|
|
expected_list = range(3)
|
|
|
|
|
|
|
|
def list_append_2():
|
|
|
|
test_list.append(2)
|
|
|
|
|
|
|
|
def list_append_1():
|
|
|
|
test_list.append(1)
|
|
|
|
|
|
|
|
def list_append_0():
|
|
|
|
test_list.append(0)
|
|
|
|
|
|
|
|
def assert_order_of_execution():
|
|
|
|
print 'expected order', expected_list
|
|
|
|
print 'but got ', test_list
|
|
|
|
assert test_list == expected_list
|
|
|
|
|
|
|
|
yield list_append_0
|
|
|
|
yield list_append_1
|
|
|
|
yield list_append_2
|
|
|
|
yield assert_order_of_execution
|
|
|
|
""")
|
|
|
|
sorter = testdir.inline_run(o) # .events_from_cmdline([o])
|
|
|
|
passed, skipped, failed = sorter.countoutcomes()
|
|
|
|
assert passed == 4
|
|
|
|
assert not skipped and not failed
|
|
|
|
|
|
|
|
class TestFunction:
|
|
|
|
def test_function_equality(self, tmpdir):
|
|
|
|
config = py.test.config._reparse([tmpdir])
|
|
|
|
f1 = py.test.collect.Function(name="name", config=config,
|
|
|
|
args=(1,), callobj=isinstance)
|
|
|
|
f2 = py.test.collect.Function(name="name", config=config,
|
|
|
|
args=(1,), callobj=callable)
|
|
|
|
assert not f1 == f2
|
|
|
|
assert f1 != f2
|
|
|
|
f3 = py.test.collect.Function(name="name", config=config,
|
|
|
|
args=(1,2), callobj=callable)
|
|
|
|
assert not f3 == f2
|
|
|
|
assert f3 != f2
|
|
|
|
|
|
|
|
assert not f3 == f1
|
|
|
|
assert f3 != f1
|
|
|
|
|
|
|
|
f1_b = py.test.collect.Function(name="name", config=config,
|
|
|
|
args=(1,), callobj=isinstance)
|
|
|
|
assert f1 == f1_b
|
|
|
|
assert not f1 != f1_b
|
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_lookupfails(self, testdir):
|
2009-03-26 17:14:09 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
class ConftestPlugin:
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__something(self, pyfuncitem):
|
2009-03-26 17:14:09 +08:00
|
|
|
return 42
|
|
|
|
""")
|
2009-03-03 06:43:31 +08:00
|
|
|
item = testdir.getitem("def test_func(some): pass")
|
2009-03-26 17:14:09 +08:00
|
|
|
exc = py.test.raises(LookupError, "item.lookup_allargs()")
|
|
|
|
s = str(exc.value)
|
|
|
|
assert s.find("something") != -1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_lookup_default(self, testdir):
|
2009-03-03 06:43:31 +08:00
|
|
|
item = testdir.getitem("def test_func(some, other=42): pass")
|
|
|
|
class Provider:
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__some(self, pyfuncitem):
|
2009-03-03 06:43:31 +08:00
|
|
|
return pyfuncitem.name
|
2009-03-18 07:48:07 +08:00
|
|
|
item.config.pytestplugins.register(Provider())
|
2009-03-03 06:43:31 +08:00
|
|
|
kw = item.lookup_allargs()
|
|
|
|
assert len(kw) == 1
|
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_lookup_default_gets_overriden(self, testdir):
|
2009-03-03 06:43:31 +08:00
|
|
|
item = testdir.getitem("def test_func(some=42, other=13): pass")
|
|
|
|
class Provider:
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__other(self, pyfuncitem):
|
2009-03-03 06:43:31 +08:00
|
|
|
return pyfuncitem.name
|
2009-03-18 07:48:07 +08:00
|
|
|
item.config.pytestplugins.register(Provider())
|
2009-03-03 06:43:31 +08:00
|
|
|
kw = item.lookup_allargs()
|
|
|
|
assert len(kw) == 1
|
|
|
|
name, value = kw.popitem()
|
|
|
|
assert name == "other"
|
|
|
|
assert value == item.name
|
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_basic(self, testdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
item = testdir.getitem("def test_func(some, other): pass")
|
|
|
|
class Provider:
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__some(self, pyfuncitem):
|
2009-02-27 18:18:27 +08:00
|
|
|
return pyfuncitem.name
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__other(self, pyfuncitem):
|
2009-02-27 18:18:27 +08:00
|
|
|
return 42
|
2009-03-18 07:48:07 +08:00
|
|
|
item.config.pytestplugins.register(Provider())
|
2009-02-27 18:18:27 +08:00
|
|
|
kw = item.lookup_allargs()
|
|
|
|
assert len(kw) == 2
|
|
|
|
assert kw['some'] == "test_func"
|
|
|
|
assert kw['other'] == 42
|
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_addfinalizer(self, testdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
item = testdir.getitem("def test_func(some): pass")
|
|
|
|
l = []
|
|
|
|
class Provider:
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__some(self, pyfuncitem):
|
2009-02-27 18:18:27 +08:00
|
|
|
pyfuncitem.addfinalizer(lambda: l.append(42))
|
|
|
|
return 3
|
2009-03-18 07:48:07 +08:00
|
|
|
item.config.pytestplugins.register(Provider())
|
2009-02-27 18:18:27 +08:00
|
|
|
kw = item.lookup_allargs()
|
|
|
|
assert len(kw) == 1
|
|
|
|
assert kw['some'] == 3
|
|
|
|
assert len(l) == 0
|
|
|
|
item.teardown()
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0] == 42
|
|
|
|
|
2009-03-26 17:16:30 +08:00
|
|
|
def test_funcarg_lookup_modulelevel(self, testdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
modcol = testdir.getmodulecol("""
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__something(pyfuncitem):
|
2009-02-27 18:18:27 +08:00
|
|
|
return pyfuncitem.name
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
def test_method(self, something):
|
|
|
|
pass
|
|
|
|
def test_func(something):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
item1, item2 = testdir.genitems([modcol])
|
|
|
|
modcol.setup()
|
2009-03-18 07:48:07 +08:00
|
|
|
assert modcol.config.pytestplugins.isregistered(modcol.obj)
|
2009-02-27 18:18:27 +08:00
|
|
|
kwargs = item1.lookup_allargs()
|
|
|
|
assert kwargs['something'] == "test_method"
|
|
|
|
kwargs = item2.lookup_allargs()
|
|
|
|
assert kwargs['something'] == "test_func"
|
|
|
|
modcol.teardown()
|
2009-03-18 07:48:07 +08:00
|
|
|
assert not modcol.config.pytestplugins.isregistered(modcol.obj)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestSorting:
|
|
|
|
def test_check_equality_and_cmp_basic(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
|
|
|
""")
|
|
|
|
fn1 = modcol.collect_by_name("test_pass")
|
|
|
|
assert isinstance(fn1, py.test.collect.Function)
|
|
|
|
fn2 = modcol.collect_by_name("test_pass")
|
|
|
|
assert isinstance(fn2, py.test.collect.Function)
|
|
|
|
|
|
|
|
assert fn1 == fn2
|
|
|
|
assert fn1 != modcol
|
|
|
|
assert cmp(fn1, fn2) == 0
|
|
|
|
assert hash(fn1) == hash(fn2)
|
|
|
|
|
|
|
|
fn3 = modcol.collect_by_name("test_fail")
|
|
|
|
assert isinstance(fn3, py.test.collect.Function)
|
|
|
|
assert not (fn1 == fn3)
|
|
|
|
assert fn1 != fn3
|
|
|
|
assert cmp(fn1, fn3) == -1
|
|
|
|
|
|
|
|
assert cmp(fn1, 10) == -1
|
|
|
|
assert cmp(fn2, 10) == -1
|
|
|
|
assert cmp(fn3, 10) == -1
|
|
|
|
for fn in fn1,fn2,fn3:
|
|
|
|
assert fn != 3
|
|
|
|
assert fn != modcol
|
|
|
|
assert fn != [1,2,3]
|
|
|
|
assert [1,2,3] != fn
|
|
|
|
assert modcol != fn
|
|
|
|
|
|
|
|
def test_allow_sane_sorting_for_decorators(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def dec(f):
|
|
|
|
g = lambda: f(2)
|
|
|
|
g.place_as = f
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def test_a(y):
|
|
|
|
pass
|
|
|
|
test_a = dec(test_a)
|
|
|
|
|
|
|
|
def test_b(y):
|
|
|
|
pass
|
|
|
|
test_b = dec(test_b)
|
|
|
|
""")
|
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 2
|
|
|
|
f1, f2 = colitems
|
|
|
|
assert cmp(f2, f1) > 0
|
|
|
|
|
|
|
|
class TestConftestCustomization:
|
|
|
|
def test_extra_python_files_and_functions(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="""
|
|
|
|
import py
|
|
|
|
class MyFunction(py.test.collect.Function):
|
|
|
|
pass
|
|
|
|
class Directory(py.test.collect.Directory):
|
|
|
|
def consider_file(self, path):
|
|
|
|
if path.check(fnmatch="check_*.py"):
|
|
|
|
return self.Module(path, parent=self)
|
|
|
|
return super(Directory, self).consider_file(path)
|
|
|
|
class myfuncmixin:
|
|
|
|
Function = MyFunction
|
|
|
|
def funcnamefilter(self, name):
|
|
|
|
return name.startswith('check_')
|
|
|
|
class Module(myfuncmixin, py.test.collect.Module):
|
|
|
|
def classnamefilter(self, name):
|
|
|
|
return name.startswith('CustomTestClass')
|
|
|
|
class Instance(myfuncmixin, py.test.collect.Instance):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
checkfile = testdir.makepyfile(check_file="""
|
|
|
|
def check_func():
|
|
|
|
assert 42 == 42
|
|
|
|
class CustomTestClass:
|
|
|
|
def check_method(self):
|
|
|
|
assert 23 == 23
|
|
|
|
""")
|
|
|
|
# check that directory collects "check_" files
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
col = config.getfsnode(checkfile.dirpath())
|
|
|
|
colitems = col.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
assert isinstance(colitems[0], py.test.collect.Module)
|
|
|
|
|
|
|
|
# check that module collects "check_" functions and methods
|
|
|
|
config = testdir.parseconfig(checkfile)
|
|
|
|
col = config.getfsnode(checkfile)
|
|
|
|
assert isinstance(col, py.test.collect.Module)
|
|
|
|
colitems = col.collect()
|
|
|
|
assert len(colitems) == 2
|
|
|
|
funccol = colitems[0]
|
|
|
|
assert isinstance(funccol, py.test.collect.Function)
|
|
|
|
assert funccol.name == "check_func"
|
|
|
|
clscol = colitems[1]
|
|
|
|
assert isinstance(clscol, py.test.collect.Class)
|
|
|
|
colitems = clscol.collect()[0].collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
assert colitems[0].name == "check_method"
|
|
|
|
|