2016-03-06 03:58:44 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-10-04 08:46:44 +08:00
|
|
|
import os
|
2015-04-18 04:25:35 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import pytest
|
2017-12-17 22:19:01 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
|
|
|
from _pytest.nodes import Collector
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-07-21 10:11:14 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestModule(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_failing_import(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("import alksdjalskdjalkjals")
|
2016-04-17 18:43:04 +08:00
|
|
|
pytest.raises(Collector.CollectError, modcol.collect)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_import_duplicate(self, testdir):
|
|
|
|
a = testdir.mkdir("a")
|
|
|
|
b = testdir.mkdir("b")
|
|
|
|
p = a.ensure("test_whatever.py")
|
|
|
|
p.pyimport()
|
2018-05-23 22:48:46 +08:00
|
|
|
del sys.modules["test_whatever"]
|
2012-11-02 23:04:57 +08:00
|
|
|
b.ensure("test_whatever.py")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*import*mismatch*",
|
|
|
|
"*imported*test_whatever*",
|
|
|
|
"*%s*" % a.join("test_whatever.py"),
|
|
|
|
"*not the same*",
|
|
|
|
"*%s*" % b.join("test_whatever.py"),
|
|
|
|
"*HINT*",
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-09-17 04:12:20 +08:00
|
|
|
def test_import_prepend_append(self, testdir, monkeypatch):
|
2015-04-18 04:25:35 +08:00
|
|
|
syspath = list(sys.path)
|
|
|
|
monkeypatch.setattr(sys, "path", syspath)
|
|
|
|
root1 = testdir.mkdir("root1")
|
|
|
|
root2 = testdir.mkdir("root2")
|
|
|
|
root1.ensure("x456.py")
|
|
|
|
root2.ensure("x456.py")
|
|
|
|
p = root2.join("test_x456.py")
|
2015-09-17 04:12:20 +08:00
|
|
|
monkeypatch.syspath_prepend(str(root1))
|
2018-05-23 22:48:46 +08:00
|
|
|
p.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import x456
|
|
|
|
def test():
|
|
|
|
assert x456.__file__.startswith({!r})
|
|
|
|
""".format(
|
|
|
|
str(root2)
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
2015-04-18 04:25:35 +08:00
|
|
|
with root2.as_cwd():
|
2015-09-17 04:12:20 +08:00
|
|
|
reprec = testdir.inline_run("--import-mode=append")
|
|
|
|
reprec.assertoutcome(passed=0, failed=1)
|
2015-04-18 04:25:35 +08:00
|
|
|
reprec = testdir.inline_run()
|
2015-09-17 04:12:20 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2015-04-18 04:25:35 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_syntax_error_in_module(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("this is a syntax error")
|
|
|
|
pytest.raises(modcol.CollectError, modcol.collect)
|
|
|
|
pytest.raises(modcol.CollectError, modcol.collect)
|
|
|
|
|
|
|
|
def test_module_considers_pluginmanager_at_import(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',")
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ImportError, lambda: modcol.obj)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2016-04-17 18:43:04 +08:00
|
|
|
def test_invalid_test_module_name(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
a = testdir.mkdir("a")
|
|
|
|
a.ensure("test_one.part1.py")
|
2016-04-17 18:43:04 +08:00
|
|
|
result = testdir.runpytest("-rw")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ImportError while importing test module*test_one.part1*",
|
|
|
|
"Hint: make sure your test modules/packages have valid Python names.",
|
|
|
|
]
|
|
|
|
)
|
2016-04-17 18:43:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("verbose", [0, 1, 2])
|
2016-10-04 08:46:44 +08:00
|
|
|
def test_show_traceback_import_error(self, testdir, verbose):
|
|
|
|
"""Import errors when collecting modules should display the traceback (#1976).
|
|
|
|
|
|
|
|
With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
|
|
|
|
"""
|
2016-10-04 07:47:44 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
foo_traceback_import_error="""
|
|
|
|
from bar_traceback_import_error import NOT_AVAILABLE
|
|
|
|
""",
|
|
|
|
bar_traceback_import_error="",
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-10-04 07:47:44 +08:00
|
|
|
import foo_traceback_import_error
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = ("-v",) * verbose
|
2016-10-04 08:46:44 +08:00
|
|
|
result = testdir.runpytest(*args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ImportError while importing test module*",
|
|
|
|
"Traceback:",
|
|
|
|
"*from bar_traceback_import_error import NOT_AVAILABLE",
|
|
|
|
"*cannot import name *NOT_AVAILABLE*",
|
|
|
|
]
|
|
|
|
)
|
2016-10-04 07:47:44 +08:00
|
|
|
assert result.ret == 2
|
|
|
|
|
2016-10-04 08:46:44 +08:00
|
|
|
stdout = result.stdout.str()
|
2018-05-23 22:48:46 +08:00
|
|
|
for name in ("_pytest", os.path.join("py", "_path")):
|
2016-10-04 08:46:44 +08:00
|
|
|
if verbose == 2:
|
|
|
|
assert name in stdout
|
|
|
|
else:
|
|
|
|
assert name not in stdout
|
|
|
|
|
2017-03-30 01:38:14 +08:00
|
|
|
def test_show_traceback_import_error_unicode(self, testdir):
|
|
|
|
"""Check test modules collected which raise ImportError with unicode messages
|
|
|
|
are handled properly (#2336).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
u"""
|
2017-03-30 01:38:14 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
raise ImportError(u'Something bad happened ☺')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-03-30 01:38:14 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ImportError while importing test module*",
|
|
|
|
"Traceback:",
|
|
|
|
"*raise ImportError*Something bad happened*",
|
|
|
|
]
|
|
|
|
)
|
2017-03-30 01:38:14 +08:00
|
|
|
assert result.ret == 2
|
|
|
|
|
2016-04-17 18:43:04 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2014-03-12 05:10:18 +08:00
|
|
|
def test_class_with_init_warning(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass1(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-rw")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*cannot collect test class 'TestClass1' because it has a __init__ constructor"
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_class_subclassobject(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
class test(object):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 0*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-06-26 01:56:50 +08:00
|
|
|
def test_static_method(self, testdir):
|
2017-08-18 07:37:51 +08:00
|
|
|
"""Support for collecting staticmethod tests (#2528, #2699)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.getmodulecol(
|
|
|
|
"""
|
2017-08-18 07:37:51 +08:00
|
|
|
import pytest
|
2017-06-26 01:56:50 +08:00
|
|
|
class Test(object):
|
|
|
|
@staticmethod
|
|
|
|
def test_something():
|
|
|
|
pass
|
2017-08-18 07:37:51 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(self):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def test_fix(fix):
|
|
|
|
assert fix == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-06-26 01:56:50 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 2 items*", "*2 passed in*"])
|
2017-06-26 01:56:50 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_setup_teardown_class_as_classmethod(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_mod1="""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClassMethod(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
pass
|
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
@classmethod
|
|
|
|
def teardown_class(cls):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-10-07 16:23:46 +08:00
|
|
|
def test_issue1035_obj_has_getattr(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2015-10-07 16:23:46 +08:00
|
|
|
class Chameleon(object):
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return True
|
|
|
|
chameleon = Chameleon()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-10-07 16:23:46 +08:00
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 0
|
|
|
|
|
2016-07-23 23:37:58 +08:00
|
|
|
def test_issue1579_namedtuple(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-07-23 23:37:58 +08:00
|
|
|
import collections
|
|
|
|
|
|
|
|
TestCase = collections.namedtuple('TestCase', ['a'])
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-rw")
|
2016-07-23 23:37:58 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"*cannot collect test class 'TestCase' "
|
|
|
|
"because it has a __new__ constructor*"
|
|
|
|
)
|
|
|
|
|
2017-02-06 05:55:39 +08:00
|
|
|
def test_issue2234_property(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-06 05:55:39 +08:00
|
|
|
class TestCase(object):
|
|
|
|
@property
|
|
|
|
def prop(self):
|
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-02-06 05:55:39 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGenerator(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_generative_functions(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
|
|
|
|
def test_gen():
|
|
|
|
yield func1, 17, 3*5
|
|
|
|
yield func1, 42, 6*7
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, pytest.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], pytest.Function)
|
|
|
|
assert isinstance(gencolitems[1], pytest.Function)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[0].name == "[0]"
|
|
|
|
assert gencolitems[0].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_generative_methods(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGenMethods(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_gen(self):
|
|
|
|
yield func1, 17, 3*5
|
|
|
|
yield func1, 42, 6*7
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
gencol = modcol.collect()[0].collect()[0].collect()[0]
|
|
|
|
assert isinstance(gencol, pytest.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], pytest.Function)
|
|
|
|
assert isinstance(gencolitems[1], pytest.Function)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[0].name == "[0]"
|
|
|
|
assert gencolitems[0].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_generative_functions_with_explicit_names(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
|
|
|
|
|
|
|
def test_gen():
|
|
|
|
yield "seventeen", func1, 17, 3*5
|
|
|
|
yield "fortytwo", func1, 42, 6*7
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, pytest.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], pytest.Function)
|
|
|
|
assert isinstance(gencolitems[1], pytest.Function)
|
|
|
|
assert gencolitems[0].name == "['seventeen']"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[0].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
assert gencolitems[1].name == "['fortytwo']"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[1].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_generative_functions_unique_explicit_names(self, testdir):
|
|
|
|
# generative
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def func(): pass
|
|
|
|
def test_gen():
|
|
|
|
yield "name", func
|
|
|
|
yield "name", func
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 1
|
|
|
|
gencol = colitems[0]
|
|
|
|
assert isinstance(gencol, pytest.Generator)
|
|
|
|
pytest.raises(ValueError, "gencol.collect()")
|
|
|
|
|
|
|
|
def test_generative_methods_with_explicit_names(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def func1(arg, arg2):
|
|
|
|
assert arg == arg2
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGenMethods(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_gen(self):
|
|
|
|
yield "m1", func1, 17, 3*5
|
|
|
|
yield "m2", func1, 42, 6*7
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
gencol = modcol.collect()[0].collect()[0].collect()[0]
|
|
|
|
assert isinstance(gencol, pytest.Generator)
|
|
|
|
gencolitems = gencol.collect()
|
|
|
|
assert len(gencolitems) == 2
|
|
|
|
assert isinstance(gencolitems[0], pytest.Function)
|
|
|
|
assert isinstance(gencolitems[1], pytest.Function)
|
|
|
|
assert gencolitems[0].name == "['m1']"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[0].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
assert gencolitems[1].name == "['m2']"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert gencolitems[1].obj.__name__ == "func1"
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_order_of_execution_generator_same_codeline(self, testdir, tmpdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
o = testdir.makepyfile(
|
|
|
|
"""
|
2017-03-17 09:27:28 +08:00
|
|
|
from __future__ import print_function
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_generative_order_of_execution():
|
|
|
|
import py, pytest
|
|
|
|
test_list = []
|
|
|
|
expected_list = list(range(6))
|
|
|
|
|
|
|
|
def list_append(item):
|
|
|
|
test_list.append(item)
|
|
|
|
|
|
|
|
def assert_order_of_execution():
|
2017-03-17 09:27:28 +08:00
|
|
|
print('expected order', expected_list)
|
|
|
|
print('but got ', test_list)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert test_list == expected_list
|
|
|
|
|
|
|
|
for i in expected_list:
|
|
|
|
yield list_append, i
|
|
|
|
yield assert_order_of_execution
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec = testdir.inline_run(o)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert passed == 7
|
|
|
|
assert not skipped and not failed
|
|
|
|
|
|
|
|
def test_order_of_execution_generator_different_codeline(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
o = testdir.makepyfile(
|
|
|
|
"""
|
2017-03-17 09:27:28 +08:00
|
|
|
from __future__ import print_function
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_generative_tests_different_codeline():
|
|
|
|
import py, pytest
|
|
|
|
test_list = []
|
|
|
|
expected_list = 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():
|
2017-03-17 09:27:28 +08:00
|
|
|
print('expected order', expected_list)
|
|
|
|
print('but got ', test_list)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert test_list == expected_list
|
|
|
|
|
|
|
|
yield list_append_0
|
|
|
|
yield list_append_1
|
|
|
|
yield list_append_2
|
|
|
|
yield assert_order_of_execution
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec = testdir.inline_run(o)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert passed == 4
|
|
|
|
assert not skipped and not failed
|
|
|
|
|
|
|
|
def test_setupstate_is_preserved_134(self, testdir):
|
|
|
|
# yield-based tests are messy wrt to setupstate because
|
|
|
|
# during collection they already invoke setup functions
|
|
|
|
# and then again when they are run. For now, we want to make sure
|
|
|
|
# that the old 1.3.4 behaviour is preserved such that all
|
|
|
|
# yielded functions all share the same "self" instance that
|
|
|
|
# has been used during collection.
|
2018-05-23 22:48:46 +08:00
|
|
|
o = testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
setuplist = []
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def setup_method(self, func):
|
|
|
|
#print "setup_method", self, func
|
|
|
|
setuplist.append(self)
|
|
|
|
self.init = 42
|
|
|
|
|
|
|
|
def teardown_method(self, func):
|
|
|
|
self.init = None
|
|
|
|
|
|
|
|
def test_func1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func2(self):
|
|
|
|
yield self.func2
|
|
|
|
yield self.func2
|
|
|
|
|
|
|
|
def func2(self):
|
|
|
|
assert self.init
|
|
|
|
|
|
|
|
def test_setuplist():
|
|
|
|
# once for test_func2 during collection
|
|
|
|
# once for test_func1 during test run
|
|
|
|
# once for test_func2 during test run
|
|
|
|
#print setuplist
|
|
|
|
assert len(setuplist) == 3, len(setuplist)
|
|
|
|
assert setuplist[0] == setuplist[2], setuplist
|
|
|
|
assert setuplist[1] != setuplist[2], setuplist
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
reprec = testdir.inline_run(o, "-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert passed == 4
|
|
|
|
assert not skipped and not failed
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFunction(object):
|
2018-09-04 04:52:44 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def ignore_parametrized_marks_args(self):
|
|
|
|
"""Provides arguments to pytester.runpytest() to ignore the warning about marks being applied directly
|
|
|
|
to parameters.
|
|
|
|
"""
|
|
|
|
return ("-W", "ignore:Applying marks directly to parameters")
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_getmodulecollector(self, testdir):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
modcol = item.getparent(pytest.Module)
|
|
|
|
assert isinstance(modcol, pytest.Module)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert hasattr(modcol.obj, "test_func")
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2014-03-12 05:10:18 +08:00
|
|
|
def test_function_as_object_instance_ignored(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2014-03-12 05:10:18 +08:00
|
|
|
def __call__(self, tmpdir):
|
|
|
|
0/0
|
|
|
|
|
|
|
|
test_a = A()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-02 08:58:48 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 0 items",
|
|
|
|
"*test_function_as_object_instance_ignored.py:2: "
|
|
|
|
"*cannot collect 'test_a' because it is not a function.",
|
|
|
|
]
|
|
|
|
)
|
2014-03-12 05:10:18 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_function_equality(self, testdir, tmpdir):
|
2016-07-10 02:36:00 +08:00
|
|
|
from _pytest.fixtures import FixtureManager
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
session = testdir.Session(config)
|
|
|
|
session._fixturemanager = FixtureManager(session)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def func1():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def func2():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
f1 = pytest.Function(
|
|
|
|
name="name", parent=session, config=config, args=(1,), callobj=func1
|
|
|
|
)
|
2013-12-07 23:39:53 +08:00
|
|
|
assert f1 == f1
|
2018-05-23 22:48:46 +08:00
|
|
|
f2 = pytest.Function(name="name", config=config, callobj=func2, parent=session)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert f1 != f2
|
|
|
|
|
|
|
|
def test_issue197_parametrize_emptyset(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('arg', [])
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=1)
|
|
|
|
|
2013-10-02 14:08:26 +08:00
|
|
|
def test_single_tuple_unwraps_values(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-10-02 14:08:26 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize(('arg',), [(1,)])
|
|
|
|
def test_function(arg):
|
|
|
|
assert arg == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-10-02 14:08:26 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_issue213_parametrize_value_no_equal(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
raise ValueError("not possible")
|
|
|
|
@pytest.mark.parametrize('arg', [A()])
|
|
|
|
def test_function(arg):
|
|
|
|
assert arg.__class__.__name__ == "A"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-12-07 23:37:46 +08:00
|
|
|
reprec = testdir.inline_run("--fulltrace")
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2013-09-07 08:30:09 +08:00
|
|
|
def test_parametrize_with_non_hashable_values(self, testdir):
|
|
|
|
"""Test parametrization with non-hashable values."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-09-07 08:30:09 +08:00
|
|
|
archival_mapping = {
|
|
|
|
'1.0': {'tag': '1.0'},
|
|
|
|
'1.2.2a1': {'tag': 'release-1.2.2a1'},
|
|
|
|
}
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('key value'.split(),
|
|
|
|
archival_mapping.items())
|
|
|
|
def test_archival_to_version(key, value):
|
|
|
|
assert key in archival_mapping
|
|
|
|
assert value == archival_mapping[key]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-09-07 08:30:09 +08:00
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=2)
|
|
|
|
|
2013-12-04 04:05:19 +08:00
|
|
|
def test_parametrize_with_non_hashable_values_indirect(self, testdir):
|
|
|
|
"""Test parametrization with non-hashable values with indirect parametrization."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-12-04 04:05:19 +08:00
|
|
|
archival_mapping = {
|
|
|
|
'1.0': {'tag': '1.0'},
|
|
|
|
'1.2.2a1': {'tag': 'release-1.2.2a1'},
|
|
|
|
}
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def key(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def value(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('key value'.split(),
|
|
|
|
archival_mapping.items(), indirect=True)
|
|
|
|
def test_archival_to_version(key, value):
|
|
|
|
assert key in archival_mapping
|
|
|
|
assert value == archival_mapping[key]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-12-04 04:05:19 +08:00
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=2)
|
|
|
|
|
2013-12-10 21:27:29 +08:00
|
|
|
def test_parametrize_overrides_fixture(self, testdir):
|
|
|
|
"""Test parametrization when parameter overrides existing fixture with same name."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-12-10 21:27:29 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def value():
|
|
|
|
return 'value'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('value',
|
2015-03-03 03:18:06 +08:00
|
|
|
['overridden'])
|
|
|
|
def test_overridden_via_param(value):
|
|
|
|
assert value == 'overridden'
|
2015-08-08 08:09:08 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('somevalue', ['overridden'])
|
|
|
|
def test_not_overridden(value, somevalue):
|
|
|
|
assert value == 'value'
|
|
|
|
assert somevalue == 'overridden'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('other,value', [('foo', 'overridden')])
|
|
|
|
def test_overridden_via_multiparam(other, value):
|
|
|
|
assert other == 'foo'
|
|
|
|
assert value == 'overridden'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-12-10 21:27:29 +08:00
|
|
|
rec = testdir.inline_run()
|
2015-08-08 08:09:08 +08:00
|
|
|
rec.assertoutcome(passed=3)
|
2013-12-10 21:27:29 +08:00
|
|
|
|
2015-03-01 22:15:37 +08:00
|
|
|
def test_parametrize_overrides_parametrized_fixture(self, testdir):
|
|
|
|
"""Test parametrization when parameter overrides existing parametrized fixture with same name."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-03-01 22:15:37 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def value(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('value',
|
2015-03-03 03:18:06 +08:00
|
|
|
['overridden'])
|
|
|
|
def test_overridden_via_param(value):
|
|
|
|
assert value == 'overridden'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-03-01 22:15:37 +08:00
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
2018-06-28 19:32:29 +08:00
|
|
|
def test_parametrize_overrides_indirect_dependency_fixture(self, testdir):
|
|
|
|
"""Test parametrization when parameter overrides a fixture that a test indirectly depends on"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
fix3_instantiated = False
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix1(fix2):
|
|
|
|
return fix2 + '1'
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix2(fix3):
|
|
|
|
return fix3 + '2'
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix3():
|
|
|
|
global fix3_instantiated
|
|
|
|
fix3_instantiated = True
|
|
|
|
return '3'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('fix2', ['2'])
|
|
|
|
def test_it(fix1):
|
|
|
|
assert fix1 == '21'
|
|
|
|
assert not fix3_instantiated
|
|
|
|
"""
|
|
|
|
)
|
2015-03-01 22:15:37 +08:00
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
@pytest.mark.filterwarnings("ignore:Applying marks directly to parameters")
|
2017-07-21 10:11:14 +08:00
|
|
|
def test_parametrize_with_mark(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
items = testdir.getitems(
|
|
|
|
"""
|
2013-11-02 12:10:13 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.foo
|
|
|
|
@pytest.mark.parametrize('arg', [
|
|
|
|
1,
|
|
|
|
pytest.mark.bar(pytest.mark.baz(2))
|
|
|
|
])
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-11-02 12:10:13 +08:00
|
|
|
keywords = [item.keywords for item in items]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
"foo" in keywords[0]
|
|
|
|
and "bar" not in keywords[0]
|
|
|
|
and "baz" not in keywords[0]
|
|
|
|
)
|
|
|
|
assert "foo" in keywords[1] and "bar" in keywords[1] and "baz" in keywords[1]
|
2013-11-02 12:10:13 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_function_equality_with_callspec(self, testdir, tmpdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
items = testdir.getitems(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('arg', [1,2])
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert items[0] != items[1]
|
|
|
|
assert not (items[0] == items[1])
|
|
|
|
|
|
|
|
def test_pyfunc_call(self, testdir):
|
|
|
|
item = testdir.getitem("def test_func(): raise ValueError")
|
|
|
|
config = item.config
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class MyPlugin1(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_pyfunc_call(self, pyfuncitem):
|
|
|
|
raise ValueError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class MyPlugin2(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_pyfunc_call(self, pyfuncitem):
|
|
|
|
return True
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
config.pluginmanager.register(MyPlugin1())
|
|
|
|
config.pluginmanager.register(MyPlugin2())
|
2016-02-16 04:43:45 +08:00
|
|
|
config.hook.pytest_runtest_setup(item=item)
|
2012-11-02 23:04:57 +08:00
|
|
|
config.hook.pytest_pyfunc_call(pyfuncitem=item)
|
|
|
|
|
2015-07-26 20:39:13 +08:00
|
|
|
def test_multiple_parametrize(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2015-07-26 20:39:13 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('x', [0, 1])
|
|
|
|
@pytest.mark.parametrize('y', [2, 3])
|
|
|
|
def test1(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-26 20:39:13 +08:00
|
|
|
colitems = modcol.collect()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert colitems[0].name == "test1[2-0]"
|
|
|
|
assert colitems[1].name == "test1[2-1]"
|
|
|
|
assert colitems[2].name == "test1[3-0]"
|
|
|
|
assert colitems[3].name == "test1[3-1]"
|
2015-07-26 20:39:13 +08:00
|
|
|
|
2015-07-25 21:38:11 +08:00
|
|
|
def test_issue751_multiple_parametrize_with_ids(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2015-07-25 21:38:11 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('x', [0], ids=['c'])
|
|
|
|
@pytest.mark.parametrize('y', [0, 1], ids=['a', 'b'])
|
|
|
|
class Test(object):
|
|
|
|
def test1(self, x, y):
|
|
|
|
pass
|
|
|
|
def test2(self, x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-25 21:38:11 +08:00
|
|
|
colitems = modcol.collect()[0].collect()[0].collect()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert colitems[0].name == "test1[a-c]"
|
|
|
|
assert colitems[1].name == "test1[b-c]"
|
|
|
|
assert colitems[2].name == "test2[a-c]"
|
|
|
|
assert colitems[3].name == "test2[b-c]"
|
2015-07-25 21:38:11 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_skipif(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.skipif('True')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_skip_if(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 2 passed, 1 skipped in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_skip(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.skip('')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_skip(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 2 passed, 1 skipped in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_skipif_no_skip(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.skipif('False')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_skipif_no_skip(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 1 failed, 2 passed in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_xfail(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.xfail('True')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_xfail(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 2 passed, 1 xfailed in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_passed(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.xfail('True')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_xfail(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 2 passed, 1 xpassed in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
def test_parametrize_xfail_passed(self, testdir, ignore_parametrized_marks_args):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.xfail('False')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x', [0, 1, m(2)])
|
|
|
|
def test_passed(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest(*ignore_parametrized_marks_args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("* 3 passed in *")
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2016-08-04 09:56:12 +08:00
|
|
|
def test_function_original_name(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
items = testdir.getitems(
|
|
|
|
"""
|
2016-08-04 09:56:12 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('arg', [1,2])
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
assert [x.originalname for x in items] == ["test_func", "test_func"]
|
2016-08-04 09:56:12 +08:00
|
|
|
|
2015-07-25 21:38:11 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSorting(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_check_equality(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
fn1 = testdir.collect_by_name(modcol, "test_pass")
|
|
|
|
assert isinstance(fn1, pytest.Function)
|
|
|
|
fn2 = testdir.collect_by_name(modcol, "test_pass")
|
|
|
|
assert isinstance(fn2, pytest.Function)
|
|
|
|
|
|
|
|
assert fn1 == fn2
|
|
|
|
assert fn1 != modcol
|
2017-12-27 11:47:26 +08:00
|
|
|
if sys.version_info < (3, 0):
|
2018-05-24 00:12:04 +08:00
|
|
|
assert cmp(fn1, fn2) == 0 # NOQA
|
2012-11-02 23:04:57 +08:00
|
|
|
assert hash(fn1) == hash(fn2)
|
|
|
|
|
|
|
|
fn3 = testdir.collect_by_name(modcol, "test_fail")
|
|
|
|
assert isinstance(fn3, pytest.Function)
|
|
|
|
assert not (fn1 == fn3)
|
|
|
|
assert fn1 != fn3
|
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
for fn in fn1, fn2, fn3:
|
2012-11-02 23:04:57 +08:00
|
|
|
assert fn != 3
|
|
|
|
assert fn != modcol
|
2017-07-17 07:25:08 +08:00
|
|
|
assert fn != [1, 2, 3]
|
|
|
|
assert [1, 2, 3] != fn
|
2012-11-02 23:04:57 +08:00
|
|
|
assert modcol != fn
|
|
|
|
|
|
|
|
def test_allow_sane_sorting_for_decorators(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def dec(f):
|
|
|
|
g = lambda: f(2)
|
|
|
|
g.place_as = f
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def test_b(y):
|
|
|
|
pass
|
|
|
|
test_b = dec(test_b)
|
|
|
|
|
|
|
|
def test_a(y):
|
|
|
|
pass
|
|
|
|
test_a = dec(test_a)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
colitems = modcol.collect()
|
|
|
|
assert len(colitems) == 2
|
2018-05-23 22:48:46 +08:00
|
|
|
assert [item.name for item in colitems] == ["test_b", "test_a"]
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestConftestCustomization(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_pytest_pycollect_module(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
class MyModule(pytest.Module):
|
|
|
|
pass
|
|
|
|
def pytest_pycollect_makemodule(path, parent):
|
|
|
|
if path.basename == "test_xyz.py":
|
|
|
|
return MyModule(path, parent)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
testdir.makepyfile("def test_some(): pass")
|
|
|
|
testdir.makepyfile(test_xyz="def test_func(): pass")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*<Module*test_pytest*", "*<MyModule*xyz*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_customized_pymakemodule_issue205_subdir(self, testdir):
|
|
|
|
b = testdir.mkdir("a").mkdir("b")
|
2018-05-23 22:48:46 +08:00
|
|
|
b.join("conftest.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
|
|
def pytest_pycollect_makemodule():
|
|
|
|
outcome = yield
|
|
|
|
mod = outcome.get_result()
|
|
|
|
mod.obj.hello = "world"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
b.join("test_module.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_hello():
|
|
|
|
assert hello == "world"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_customized_pymakeitem(self, testdir):
|
|
|
|
b = testdir.mkdir("a").mkdir("b")
|
2018-05-23 22:48:46 +08:00
|
|
|
b.join("conftest.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
|
|
def pytest_pycollect_makeitem():
|
|
|
|
outcome = yield
|
|
|
|
if outcome.excinfo is None:
|
|
|
|
result = outcome.get_result()
|
|
|
|
if result:
|
|
|
|
for func in result:
|
|
|
|
func._some123 = "world"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
b.join("test_module.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def obj(request):
|
|
|
|
return request.node._some123
|
|
|
|
def test_hello(obj):
|
|
|
|
assert obj == "world"
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_pytest_pycollect_makeitem(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
class MyFunction(pytest.Function):
|
|
|
|
pass
|
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
|
|
|
if name == "some":
|
|
|
|
return MyFunction(name, collector)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
testdir.makepyfile("def some(): pass")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*MyFunction*some*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_makeitem_non_underscore(self, testdir, monkeypatch):
|
|
|
|
modcol = testdir.getmodulecol("def _hello(): pass")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(
|
|
|
|
pytest.Module, "makeitem", lambda self, name, obj: values.append(name)
|
|
|
|
)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = modcol.collect()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "_hello" not in values
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-04-20 03:26:56 +08:00
|
|
|
def test_issue2369_collect_module_fileext(self, testdir):
|
|
|
|
"""Ensure we can collect files with weird file extensions as Python
|
|
|
|
modules (#2369)"""
|
|
|
|
# We'll implement a little finder and loader to import files containing
|
|
|
|
# Python source code whose file extension is ".narf".
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2017-04-20 03:26:56 +08:00
|
|
|
import sys, os, imp
|
|
|
|
from _pytest.python import Module
|
|
|
|
|
2018-01-25 04:23:42 +08:00
|
|
|
class Loader(object):
|
2017-04-20 03:26:56 +08:00
|
|
|
def load_module(self, name):
|
|
|
|
return imp.load_source(name, name + ".narf")
|
2018-01-25 04:23:42 +08:00
|
|
|
class Finder(object):
|
2017-04-20 03:26:56 +08:00
|
|
|
def find_module(self, name, path=None):
|
|
|
|
if os.path.exists(name + ".narf"):
|
|
|
|
return Loader()
|
|
|
|
sys.meta_path.append(Finder())
|
|
|
|
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".narf":
|
2018-05-23 22:48:46 +08:00
|
|
|
return Module(path, parent)"""
|
|
|
|
)
|
|
|
|
testdir.makefile(
|
|
|
|
".narf",
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2017-04-20 03:26:56 +08:00
|
|
|
def test_something():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert 1 + 1 == 2""",
|
|
|
|
)
|
2017-04-20 03:26:56 +08:00
|
|
|
# Use runpytest_subprocess, since we're futzing with sys.meta_path.
|
|
|
|
result = testdir.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines("*1 passed*")
|
2017-04-20 03:26:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_setup_only_available_in_subdir(testdir):
|
|
|
|
sub1 = testdir.mkpydir("sub1")
|
|
|
|
sub2 = testdir.mkpydir("sub2")
|
2018-05-23 22:48:46 +08:00
|
|
|
sub1.join("conftest.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub1"
|
|
|
|
def pytest_runtest_call(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub1"
|
|
|
|
def pytest_runtest_teardown(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub1"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
sub2.join("conftest.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub2"
|
|
|
|
def pytest_runtest_call(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub2"
|
|
|
|
def pytest_runtest_teardown(item):
|
|
|
|
assert item.fspath.purebasename == "test_in_sub2"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
sub1.join("test_in_sub1.py").write("def test_1(): pass")
|
|
|
|
sub2.join("test_in_sub2.py").write("def test_2(): pass")
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest("-v", "-s")
|
|
|
|
result.assert_outcomes(passed=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_modulecol_roundtrip(testdir):
|
2018-04-17 02:44:05 +08:00
|
|
|
modcol = testdir.getmodulecol("pass", withinit=False)
|
2012-11-02 23:04:57 +08:00
|
|
|
trail = modcol.nodeid
|
|
|
|
newcol = modcol.session.perform_collect([trail], genitems=0)[0]
|
|
|
|
assert modcol.name == newcol.name
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestTracebackCutting(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_skip_simple(self):
|
|
|
|
excinfo = pytest.raises(pytest.skip.Exception, 'pytest.skip("xxx")')
|
|
|
|
assert excinfo.traceback[-1].frame.code.name == "skip"
|
|
|
|
assert excinfo.traceback[-1].ishidden()
|
|
|
|
|
|
|
|
def test_traceback_argsetup(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hello(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
raise ValueError("xyz")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
p = testdir.makepyfile("def test(hello): pass")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
out = result.stdout.str()
|
2016-07-12 09:03:53 +08:00
|
|
|
assert "xyz" in out
|
|
|
|
assert "conftest.py:5: ValueError" in out
|
2017-07-17 07:25:09 +08:00
|
|
|
numentries = out.count("_ _ _") # separator for traceback entries
|
2012-11-02 23:04:57 +08:00
|
|
|
assert numentries == 0
|
|
|
|
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fulltrace", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
out = result.stdout.str()
|
2016-07-12 09:03:53 +08:00
|
|
|
assert "conftest.py:5: ValueError" in out
|
2017-07-17 07:25:09 +08:00
|
|
|
numentries = out.count("_ _ _ _") # separator for traceback entries
|
2012-11-02 23:04:57 +08:00
|
|
|
assert numentries > 3
|
|
|
|
|
|
|
|
def test_traceback_error_during_import(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
x = 1
|
|
|
|
x = 2
|
|
|
|
x = 17
|
|
|
|
asd
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
out = result.stdout.str()
|
|
|
|
assert "x = 1" not in out
|
|
|
|
assert "x = 2" not in out
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines([" *asd*", "E*NameError*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fulltrace")
|
2012-11-02 23:04:57 +08:00
|
|
|
out = result.stdout.str()
|
|
|
|
assert "x = 1" in out
|
|
|
|
assert "x = 2" in out
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines([">*asd*", "E*NameError*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-09-30 09:29:43 +08:00
|
|
|
def test_traceback_filter_error_during_fixture_collection(self, testdir):
|
|
|
|
"""integration test for issue #995.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-09-30 09:29:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def fail_me(func):
|
|
|
|
ns = {}
|
|
|
|
exec('def w(): raise ValueError("fail me")', ns)
|
|
|
|
return ns['w']
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
@fail_me
|
|
|
|
def fail_fixture():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_failing_fixture(fail_fixture):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-30 09:29:43 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret != 0
|
|
|
|
out = result.stdout.str()
|
|
|
|
assert "INTERNALERROR>" not in out
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*ValueError: fail me*", "* 1 error in *"])
|
2015-09-30 09:29:43 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
def test_filter_traceback_generated_code(self):
|
|
|
|
"""test that filter_traceback() works with the fact that
|
|
|
|
py.code.Code.path attribute might return an str object.
|
|
|
|
In this case, one of the entries on the traceback was produced by
|
|
|
|
dynamically generated code.
|
|
|
|
See: https://bitbucket.org/pytest-dev/py/issues/71
|
2015-09-30 09:29:43 +08:00
|
|
|
This fixes #995.
|
|
|
|
"""
|
|
|
|
from _pytest.python import filter_traceback
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-09-30 09:29:43 +08:00
|
|
|
try:
|
|
|
|
ns = {}
|
2018-05-23 22:48:46 +08:00
|
|
|
exec("def foo(): raise ValueError", ns)
|
|
|
|
ns["foo"]()
|
2015-09-30 09:29:43 +08:00
|
|
|
except ValueError:
|
|
|
|
_, _, tb = sys.exc_info()
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
tb = _pytest._code.Traceback(tb)
|
2015-10-17 07:08:12 +08:00
|
|
|
assert isinstance(tb[-1].path, str)
|
2015-10-01 04:15:53 +08:00
|
|
|
assert not filter_traceback(tb[-1])
|
2015-09-30 09:29:43 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
def test_filter_traceback_path_no_longer_valid(self, testdir):
|
|
|
|
"""test that filter_traceback() works with the fact that
|
|
|
|
py.code.Code.path attribute might return an str object.
|
|
|
|
In this case, one of the files in the traceback no longer exists.
|
|
|
|
This fixes #1133.
|
|
|
|
"""
|
|
|
|
from _pytest.python import filter_traceback
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
testdir.syspathinsert()
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
filter_traceback_entry_as_str="""
|
2015-10-17 07:08:12 +08:00
|
|
|
def foo():
|
|
|
|
raise ValueError
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-10-17 07:08:12 +08:00
|
|
|
try:
|
|
|
|
import filter_traceback_entry_as_str
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
filter_traceback_entry_as_str.foo()
|
|
|
|
except ValueError:
|
|
|
|
_, _, tb = sys.exc_info()
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("filter_traceback_entry_as_str.py").remove()
|
2015-11-27 22:43:01 +08:00
|
|
|
tb = _pytest._code.Traceback(tb)
|
2015-10-17 07:08:12 +08:00
|
|
|
assert isinstance(tb[-1].path, str)
|
|
|
|
assert filter_traceback(tb[-1])
|
|
|
|
|
2015-09-30 09:29:43 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestReportInfo(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_itemreport_reportinfo(self, testdir, linecomp):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
class MyFunction(pytest.Function):
|
|
|
|
def reportinfo(self):
|
|
|
|
return "ABCDE", 42, "custom"
|
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
|
|
|
if name == "test_func":
|
|
|
|
return MyFunction(name, parent=collector)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2013-10-12 21:39:22 +08:00
|
|
|
item.config.pluginmanager.getplugin("runner")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert item.location == ("ABCDE", 42, "custom")
|
|
|
|
|
|
|
|
def test_func_reportinfo(self, testdir):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
fspath, lineno, modpath = item.reportinfo()
|
|
|
|
assert fspath == item.fspath
|
|
|
|
assert lineno == 0
|
|
|
|
assert modpath == "test_func"
|
|
|
|
|
|
|
|
def test_class_reportinfo(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
# lineno 0
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello(self): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
classcol = testdir.collect_by_name(modcol, "TestClass")
|
|
|
|
fspath, lineno, msg = classcol.reportinfo()
|
|
|
|
assert fspath == modcol.fspath
|
|
|
|
assert lineno == 1
|
|
|
|
assert msg == "TestClass"
|
|
|
|
|
|
|
|
def test_generator_reportinfo(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
# lineno 0
|
|
|
|
def test_gen():
|
|
|
|
def check(x):
|
|
|
|
assert x
|
|
|
|
yield check, 3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
gencol = testdir.collect_by_name(modcol, "test_gen")
|
|
|
|
fspath, lineno, modpath = gencol.reportinfo()
|
|
|
|
assert fspath == modcol.fspath
|
|
|
|
assert lineno == 1
|
|
|
|
assert modpath == "test_gen"
|
|
|
|
|
|
|
|
genitem = gencol.collect()[0]
|
|
|
|
fspath, lineno, modpath = genitem.reportinfo()
|
|
|
|
assert fspath == modcol.fspath
|
|
|
|
assert lineno == 2
|
|
|
|
assert modpath == "test_gen[0]"
|
|
|
|
"""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def test_genfunc():
|
|
|
|
def check(x):
|
|
|
|
pass
|
|
|
|
yield check, 3
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
|
2015-11-30 23:13:15 +08:00
|
|
|
def test_reportinfo_with_nasty_getattr(self, testdir):
|
|
|
|
# https://github.com/pytest-dev/pytest/issues/1204
|
2018-05-23 22:48:46 +08:00
|
|
|
modcol = testdir.getmodulecol(
|
|
|
|
"""
|
2015-11-30 23:13:15 +08:00
|
|
|
# lineno 0
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2015-11-30 23:13:15 +08:00
|
|
|
def __getattr__(self, name):
|
|
|
|
return "this is not an int"
|
|
|
|
|
|
|
|
def test_foo(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-30 23:13:15 +08:00
|
|
|
classcol = testdir.collect_by_name(modcol, "TestClass")
|
|
|
|
instance = classcol.collect()[0]
|
|
|
|
fspath, lineno, msg = instance.reportinfo()
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_customized_python_discovery(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
[pytest]
|
|
|
|
python_files=check_*.py
|
|
|
|
python_classes=Check
|
|
|
|
python_functions=check
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def check_simple():
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class CheckMyApp(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def check_meth(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
p2 = p.new(basename=p.basename.replace("test", "check"))
|
|
|
|
p.move(p2)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--collect-only", "-s")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*check_customized*", "*check_simple*", "*CheckMyApp*", "*check_meth*"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2012-11-28 00:58:08 +08:00
|
|
|
|
|
|
|
def test_customized_python_discovery_functions(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2012-11-28 00:58:08 +08:00
|
|
|
[pytest]
|
|
|
|
python_functions=_test
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-28 00:58:08 +08:00
|
|
|
def _test_underscore():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--collect-only", "-s")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*_test_underscore*"])
|
2012-11-28 00:58:08 +08:00
|
|
|
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-28 00:58:08 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2012-11-28 00:58:08 +08:00
|
|
|
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_collector_attributes(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
def pytest_pycollect_makeitem(collector):
|
|
|
|
assert collector.Function == pytest.Function
|
|
|
|
assert collector.Class == pytest.Class
|
|
|
|
assert collector.Instance == pytest.Instance
|
|
|
|
assert collector.Module == pytest.Module
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_customize_through_attributes(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
class MyFunction(pytest.Function):
|
|
|
|
pass
|
|
|
|
class MyInstance(pytest.Instance):
|
|
|
|
Function = MyFunction
|
|
|
|
class MyClass(pytest.Class):
|
|
|
|
Instance = MyInstance
|
|
|
|
|
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
|
|
|
if name.startswith("MyTestClass"):
|
|
|
|
return MyClass(name, parent=collector)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class MyTestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*MyClass*", "*MyInstance*", "*MyFunction*test_hello*"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_unorderable_types(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestJoinEmpty(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def make_test():
|
2017-02-17 02:41:51 +08:00
|
|
|
class Test(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
pass
|
|
|
|
Test.__name__ = "TestFoo"
|
|
|
|
return Test
|
|
|
|
TestFoo = make_test()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert "TypeError" not in result.stdout.str()
|
2015-08-28 02:35:46 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2015-07-03 10:13:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_collect_functools_partial(testdir):
|
|
|
|
"""
|
|
|
|
Test that collection of functools.partial object works, and arguments
|
|
|
|
to the wrapped functions are dealt correctly (see #811).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-07-03 10:13:59 +08:00
|
|
|
import functools
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix1():
|
|
|
|
return 'fix1'
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix2():
|
|
|
|
return 'fix2'
|
|
|
|
|
|
|
|
def check1(i, fix1):
|
|
|
|
assert i == 2
|
|
|
|
assert fix1 == 'fix1'
|
|
|
|
|
|
|
|
def check2(fix1, i):
|
|
|
|
assert i == 2
|
|
|
|
assert fix1 == 'fix1'
|
|
|
|
|
|
|
|
def check3(fix1, i, fix2):
|
|
|
|
assert i == 2
|
|
|
|
assert fix1 == 'fix1'
|
|
|
|
assert fix2 == 'fix2'
|
|
|
|
|
|
|
|
test_ok_1 = functools.partial(check1, i=2)
|
|
|
|
test_ok_2 = functools.partial(check1, i=2, fix1='fix1')
|
|
|
|
test_ok_3 = functools.partial(check1, 2)
|
|
|
|
test_ok_4 = functools.partial(check2, i=2)
|
|
|
|
test_ok_5 = functools.partial(check3, i=2)
|
|
|
|
test_ok_6 = functools.partial(check3, i=2, fix1='fix1')
|
|
|
|
|
|
|
|
test_fail_1 = functools.partial(check2, 2)
|
|
|
|
test_fail_2 = functools.partial(check3, 2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-03 10:13:59 +08:00
|
|
|
result = testdir.inline_run()
|
|
|
|
result.assertoutcome(passed=6, failed=2)
|
2015-09-23 07:06:55 +08:00
|
|
|
|
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2015-09-23 07:06:55 +08:00
|
|
|
def test_dont_collect_non_function_callable(testdir):
|
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/331
|
|
|
|
|
|
|
|
In this case an INTERNALERROR occurred trying to report the failure of
|
|
|
|
a test like this one because py test failed to get the source lines.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-09-23 07:06:55 +08:00
|
|
|
class Oh(object):
|
|
|
|
def __call__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test_a = Oh()
|
|
|
|
|
|
|
|
def test_real():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-rw")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*collected 1 item*",
|
2018-09-02 08:58:48 +08:00
|
|
|
"*test_dont_collect_non_function_callable.py:2: *cannot collect 'test_a' because it is not a function*",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*1 passed, 1 warnings in *",
|
|
|
|
]
|
|
|
|
)
|
2015-12-11 06:45:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_class_injection_does_not_break_collection(testdir):
|
2015-12-11 07:19:08 +08:00
|
|
|
"""Tests whether injection during collection time will terminate testing.
|
|
|
|
|
|
|
|
In this case the error should not occur if the TestClass itself
|
|
|
|
is modified during collection time, and the original method list
|
|
|
|
is still used for collection.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2015-12-11 06:45:36 +08:00
|
|
|
from test_inject import TestClass
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
TestClass.changed_var = {}
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_inject='''
|
2015-12-11 06:45:36 +08:00
|
|
|
class TestClass(object):
|
|
|
|
def test_injection(self):
|
|
|
|
"""Test being parametrized."""
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2015-12-11 06:45:36 +08:00
|
|
|
result = testdir.runpytest()
|
2018-06-26 21:35:27 +08:00
|
|
|
assert (
|
|
|
|
"RuntimeError: dictionary changed size during iteration"
|
|
|
|
not in result.stdout.str()
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2016-03-06 03:58:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_syntax_error_with_non_ascii_chars(testdir):
|
|
|
|
"""Fix decoding issue while formatting SyntaxErrors during collection (#578)
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
u"""
|
2016-03-06 03:58:44 +08:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
☃
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-03-06 03:58:44 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"])
|
2016-07-25 18:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_skip_duplicates_by_default(testdir):
|
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)
|
|
|
|
|
|
|
|
Ignore duplicate directories.
|
|
|
|
"""
|
|
|
|
a = testdir.mkdir("a")
|
|
|
|
fh = a.join("test_a.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
fh.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def test_real():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2016-07-25 18:40:57 +08:00
|
|
|
result = testdir.runpytest(a.strpath, a.strpath)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
2016-07-25 18:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_keep_duplicates(testdir):
|
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)
|
|
|
|
|
|
|
|
Use --keep-duplicates to collect tests from duplicate directories.
|
|
|
|
"""
|
|
|
|
a = testdir.mkdir("a")
|
|
|
|
fh = a.join("test_a.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
fh.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def test_real():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2016-07-25 18:40:57 +08:00
|
|
|
result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 2 item*"])
|
2018-08-03 02:18:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_package_collection_infinite_recursion(testdir):
|
|
|
|
testdir.copy_example("collect/package_infinite_recursion")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("*1 passed*")
|
2018-08-11 08:29:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_package_with_modules(testdir):
|
|
|
|
"""
|
|
|
|
.
|
|
|
|
└── root
|
|
|
|
├── __init__.py
|
|
|
|
├── sub1
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ └── sub1_1
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ └── test_in_sub1.py
|
|
|
|
└── sub2
|
|
|
|
└── test
|
|
|
|
└── test_in_sub2.py
|
|
|
|
|
|
|
|
"""
|
|
|
|
root = testdir.mkpydir("root")
|
|
|
|
sub1 = root.mkdir("sub1")
|
|
|
|
sub1.ensure("__init__.py")
|
|
|
|
sub1_test = sub1.mkdir("sub1_1")
|
|
|
|
sub1_test.ensure("__init__.py")
|
|
|
|
sub2 = root.mkdir("sub2")
|
|
|
|
sub2_test = sub2.mkdir("sub2")
|
|
|
|
|
|
|
|
sub1_test.join("test_in_sub1.py").write("def test_1(): pass")
|
|
|
|
sub2_test.join("test_in_sub2.py").write("def test_2(): pass")
|
|
|
|
|
|
|
|
# Execute from .
|
|
|
|
result = testdir.runpytest("-v", "-s")
|
|
|
|
result.assert_outcomes(passed=2)
|
|
|
|
|
|
|
|
# Execute from . with one argument "root"
|
|
|
|
result = testdir.runpytest("-v", "-s", "root")
|
|
|
|
result.assert_outcomes(passed=2)
|
|
|
|
|
|
|
|
# Chdir into package's root and execute with no args
|
|
|
|
root.chdir()
|
|
|
|
result = testdir.runpytest("-v", "-s")
|
|
|
|
result.assert_outcomes(passed=2)
|
2018-08-23 12:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_package_ordering(testdir):
|
|
|
|
"""
|
|
|
|
.
|
|
|
|
└── root
|
2018-08-25 07:23:50 +08:00
|
|
|
├── Test_root.py
|
2018-08-23 12:42:59 +08:00
|
|
|
├── __init__.py
|
|
|
|
├── sub1
|
2018-08-25 07:23:50 +08:00
|
|
|
│ ├── Test_sub1.py
|
2018-08-23 12:42:59 +08:00
|
|
|
│ └── __init__.py
|
|
|
|
└── sub2
|
|
|
|
└── test
|
2018-08-25 07:23:50 +08:00
|
|
|
└── test_sub2.py
|
2018-08-23 12:42:59 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
2018-08-25 07:23:50 +08:00
|
|
|
python_files=*.py
|
2018-08-23 12:42:59 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
root = testdir.mkpydir("root")
|
|
|
|
sub1 = root.mkdir("sub1")
|
|
|
|
sub1.ensure("__init__.py")
|
|
|
|
sub2 = root.mkdir("sub2")
|
|
|
|
sub2_test = sub2.mkdir("sub2")
|
|
|
|
|
2018-08-25 07:23:50 +08:00
|
|
|
root.join("Test_root.py").write("def test_1(): pass")
|
|
|
|
sub1.join("Test_sub1.py").write("def test_2(): pass")
|
|
|
|
sub2_test.join("test_sub2.py").write("def test_3(): pass")
|
2018-08-23 12:42:59 +08:00
|
|
|
|
|
|
|
# Execute from .
|
|
|
|
result = testdir.runpytest("-v", "-s")
|
|
|
|
result.assert_outcomes(passed=3)
|