2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2020-12-10 13:47:58 +08:00
|
|
|
import os
|
2015-04-18 04:25:35 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import _pytest._code
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2021-10-22 17:11:13 +08:00
|
|
|
from _pytest.main import Session
|
2020-12-10 13:47:58 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2017-12-17 22:19:01 +08:00
|
|
|
from _pytest.nodes import Collector
|
2020-12-10 13:47:58 +08:00
|
|
|
from _pytest.pytester import Pytester
|
|
|
|
from _pytest.python import Class
|
2021-11-13 20:03:44 +08:00
|
|
|
from _pytest.python import Function
|
2024-02-01 04:12:33 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-07-21 10:11:14 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestModule:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_failing_import(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_import_duplicate(self, pytester: Pytester) -> None:
|
|
|
|
a = pytester.mkdir("a")
|
|
|
|
b = pytester.mkdir("b")
|
|
|
|
p1 = a.joinpath("test_whatever.py")
|
|
|
|
p1.touch()
|
|
|
|
p2 = b.joinpath("test_whatever.py")
|
|
|
|
p2.touch()
|
|
|
|
# ensure we don't have it imported already
|
|
|
|
sys.modules.pop(p1.stem, None)
|
|
|
|
|
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*import*mismatch*",
|
|
|
|
"*imported*test_whatever*",
|
2024-05-01 00:06:26 +08:00
|
|
|
f"*{p1}*",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*not the same*",
|
2024-05-01 00:06:26 +08:00
|
|
|
f"*{p2}*",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*HINT*",
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_import_prepend_append(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
root1 = pytester.mkdir("root1")
|
|
|
|
root2 = pytester.mkdir("root2")
|
|
|
|
root1.joinpath("x456.py").touch()
|
|
|
|
root2.joinpath("x456.py").touch()
|
|
|
|
p = root2.joinpath("test_x456.py")
|
2015-09-17 04:12:20 +08:00
|
|
|
monkeypatch.syspath_prepend(str(root1))
|
2020-12-10 13:47:58 +08:00
|
|
|
p.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2024-02-01 04:12:33 +08:00
|
|
|
f"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import x456
|
|
|
|
def test():
|
2024-02-01 04:12:33 +08:00
|
|
|
assert x456.__file__.startswith({str(root2)!r})
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.chdir(root2)
|
|
|
|
reprec = pytester.inline_run("--import-mode=append")
|
2015-09-17 04:12:20 +08:00
|
|
|
reprec.assertoutcome(passed=0, failed=1)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-09-17 04:12:20 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2015-04-18 04:25:35 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_syntax_error_in_module(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol("this is a syntax error")
|
2012-11-02 23:04:57 +08:00
|
|
|
pytest.raises(modcol.CollectError, modcol.collect)
|
|
|
|
pytest.raises(modcol.CollectError, modcol.collect)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_module_considers_pluginmanager_at_import(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_invalid_test_module_name(self, pytester: Pytester) -> None:
|
|
|
|
a = pytester.mkdir("a")
|
|
|
|
a.joinpath("test_one.part1.py").touch()
|
|
|
|
result = pytester.runpytest()
|
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])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_traceback_import_error(
|
|
|
|
self, pytester: Pytester, verbose: int
|
|
|
|
) -> None:
|
2016-10-04 08:46:44 +08:00
|
|
|
"""Import errors when collecting modules should display the traceback (#1976).
|
|
|
|
|
|
|
|
With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2016-10-04 07:47:44 +08:00
|
|
|
foo_traceback_import_error="""
|
|
|
|
from bar_traceback_import_error import NOT_AVAILABLE
|
|
|
|
""",
|
|
|
|
bar_traceback_import_error="",
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-10-04 07:47:44 +08:00
|
|
|
import foo_traceback_import_error
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = ("-v",) * verbose
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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()
|
2020-06-13 22:29:01 +08:00
|
|
|
if verbose == 2:
|
|
|
|
assert "_pytest" in stdout
|
|
|
|
else:
|
|
|
|
assert "_pytest" not in stdout
|
2016-10-04 08:46:44 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_traceback_import_error_unicode(self, pytester: Pytester) -> None:
|
2017-03-30 01:38:14 +08:00
|
|
|
"""Check test modules collected which raise ImportError with unicode messages
|
|
|
|
are handled properly (#2336).
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile("raise ImportError('Something bad happened ☺')")
|
|
|
|
result = pytester.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
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestClass:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_with_init_warning(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2019-05-30 19:18:28 +08:00
|
|
|
"*cannot collect test class 'TestClass1' because it has "
|
|
|
|
"a __init__ constructor (from: test_class_with_init_warning.py)"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_with_new_warning(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-05-30 19:18:28 +08:00
|
|
|
"""
|
|
|
|
class TestClass1(object):
|
|
|
|
def __new__(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-05-30 19:18:28 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*cannot collect test class 'TestClass1' because it has "
|
|
|
|
"a __new__ constructor (from: test_class_with_new_warning.py)"
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_subclassobject(self, pytester: Pytester) -> None:
|
|
|
|
pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
class test(object):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 0*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_static_method(self, pytester: Pytester) -> None:
|
2017-08-18 07:37:51 +08:00
|
|
|
"""Support for collecting staticmethod tests (#2528, #2699)"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_teardown_class_as_classmethod(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue1035_obj_has_getattr(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue1579_namedtuple(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-23 23:37:58 +08:00
|
|
|
import collections
|
|
|
|
|
|
|
|
TestCase = collections.namedtuple('TestCase', ['a'])
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2016-07-23 23:37:58 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"*cannot collect test class 'TestCase' "
|
|
|
|
"because it has a __new__ constructor*"
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue2234_property(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2017-02-06 05:55:39 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestFunction:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_getmodulecollector(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2012-11-02 23:04:57 +08:00
|
|
|
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")
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_function_as_object_instance_ignored(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2021-02-21 21:10:00 +08:00
|
|
|
def __call__(self, tmp_path):
|
2014-03-12 05:10:18 +08:00
|
|
|
0/0
|
|
|
|
|
|
|
|
test_a = A()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-09-02 08:58:48 +08:00
|
|
|
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
|
|
|
|
2018-11-26 01:33:18 +08:00
|
|
|
@staticmethod
|
2020-12-10 13:47:58 +08:00
|
|
|
def make_function(pytester: Pytester, **kwargs: Any) -> Any:
|
2016-07-10 02:36:00 +08:00
|
|
|
from _pytest.fixtures import FixtureManager
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
config = pytester.parseconfigure()
|
2021-10-22 17:11:13 +08:00
|
|
|
session = Session.from_config(config)
|
2012-11-02 23:04:57 +08:00
|
|
|
session._fixturemanager = FixtureManager(session)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-01-01 03:20:28 +08:00
|
|
|
return pytest.Function.from_parent(parent=session, **kwargs)
|
2018-11-26 01:33:18 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_function_equality(self, pytester: Pytester) -> None:
|
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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
f1 = self.make_function(pytester, name="name", callobj=func1)
|
2013-12-07 23:39:53 +08:00
|
|
|
assert f1 == f1
|
2020-05-19 01:20:13 +08:00
|
|
|
f2 = self.make_function(
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester, name="name", callobj=func2, originalname="foobar"
|
2020-05-19 01:20:13 +08:00
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert f1 != f2
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_repr_produces_actual_test_id(self, pytester: Pytester) -> None:
|
2018-11-26 01:33:18 +08:00
|
|
|
f = self.make_function(
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester, name=r"test[\xe5]", callobj=self.test_repr_produces_actual_test_id
|
2018-11-26 01:33:18 +08:00
|
|
|
)
|
|
|
|
assert repr(f) == r"<Function test[\xe5]>"
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue197_parametrize_emptyset(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(skipped=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_single_tuple_unwraps_values(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-10-02 14:08:26 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue213_parametrize_value_no_equal(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("--fulltrace")
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_non_hashable_values(self, pytester: Pytester) -> None:
|
2013-09-07 08:30:09 +08:00
|
|
|
"""Test parametrization with non-hashable values."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
rec = pytester.inline_run()
|
2013-09-07 08:30:09 +08:00
|
|
|
rec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_non_hashable_values_indirect(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2013-12-04 04:05:19 +08:00
|
|
|
"""Test parametrization with non-hashable values with indirect parametrization."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
rec = pytester.inline_run()
|
2013-12-04 04:05:19 +08:00
|
|
|
rec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_overrides_fixture(self, pytester: Pytester) -> None:
|
2013-12-10 21:27:29 +08:00
|
|
|
"""Test parametrization when parameter overrides existing fixture with same name."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
rec = pytester.inline_run()
|
2015-08-08 08:09:08 +08:00
|
|
|
rec.assertoutcome(passed=3)
|
2013-12-10 21:27:29 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_overrides_parametrized_fixture(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2015-03-01 22:15:37 +08:00
|
|
|
"""Test parametrization when parameter overrides existing parametrized fixture with same name."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
rec = pytester.inline_run()
|
2015-03-01 22:15:37 +08:00
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_overrides_indirect_dependency_fixture(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2018-06-28 19:32:29 +08:00
|
|
|
"""Test parametrization when parameter overrides a fixture that a test indirectly depends on"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-06-28 19:32:29 +08:00
|
|
|
"""
|
|
|
|
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'])
|
2020-03-13 21:18:06 +08:00
|
|
|
def test_it(fix1):
|
2018-06-28 19:32:29 +08:00
|
|
|
assert fix1 == '21'
|
|
|
|
assert not fix3_instantiated
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
rec = pytester.inline_run()
|
2015-03-01 22:15:37 +08:00
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_mark(self, pytester: Pytester) -> None:
|
|
|
|
items = pytester.getitems(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-11-02 12:10:13 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.foo
|
|
|
|
@pytest.mark.parametrize('arg', [
|
|
|
|
1,
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(2, marks=[pytest.mark.baz, pytest.mark.bar])
|
2013-11-02 12:10:13 +08:00
|
|
|
])
|
|
|
|
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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_empty_string_arguments(self, pytester: Pytester) -> None:
|
|
|
|
items = pytester.getitems(
|
2020-01-29 05:27:54 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('v', ('', ' '))
|
|
|
|
@pytest.mark.parametrize('w', ('', ' '))
|
|
|
|
def test(v, w): ...
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
names = {item.name for item in items}
|
|
|
|
assert names == {"test[-]", "test[ -]", "test[- ]", "test[ - ]"}
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_function_equality_with_callspec(self, pytester: Pytester) -> None:
|
|
|
|
items = pytester.getitems(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pyfunc_call(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem("def test_func(): raise ValueError")
|
2012-11-02 23:04:57 +08:00
|
|
|
config = item.config
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MyPlugin1:
|
2020-01-17 02:42:29 +08:00
|
|
|
def pytest_pyfunc_call(self):
|
2012-11-02 23:04:57 +08:00
|
|
|
raise ValueError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MyPlugin2:
|
2020-01-17 02:42:29 +08:00
|
|
|
def pytest_pyfunc_call(self):
|
2012-11-02 23:04:57 +08:00
|
|
|
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)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_multiple_parametrize(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue751_multiple_parametrize_with_ids(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2021-11-13 20:03:44 +08:00
|
|
|
colitems = modcol.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_skipif(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.skipif('True')
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
@pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)])
|
2016-02-26 06:13:09 +08:00
|
|
|
def test_skip_if(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed, 1 skipped in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_skip(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.skip('')
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
@pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)])
|
2016-02-26 06:13:09 +08:00
|
|
|
def test_skip(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed, 1 skipped in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_skipif_no_skip(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 failed, 2 passed in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_xfail(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.xfail('True')
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
@pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)])
|
2016-02-26 06:13:09 +08:00
|
|
|
def test_xfail(x):
|
|
|
|
assert x < 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed, 1 xfailed in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_passed(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-26 06:13:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
m = pytest.mark.xfail('True')
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
@pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)])
|
2016-02-26 06:13:09 +08:00
|
|
|
def test_xfail(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed, 1 xpassed in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_xfail_passed(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 3 passed in *"])
|
2016-02-26 06:13:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_function_originalname(self, pytester: Pytester) -> None:
|
|
|
|
items = pytester.getitems(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-04 09:56:12 +08:00
|
|
|
import pytest
|
2020-05-19 01:08:47 +08:00
|
|
|
|
2016-08-04 09:56:12 +08:00
|
|
|
@pytest.mark.parametrize('arg', [1,2])
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2020-05-19 01:08:47 +08:00
|
|
|
|
|
|
|
def test_no_param():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-07-10 04:50:15 +08:00
|
|
|
originalnames = []
|
|
|
|
for x in items:
|
|
|
|
assert isinstance(x, pytest.Function)
|
|
|
|
originalnames.append(x.originalname)
|
|
|
|
assert originalnames == [
|
2020-05-19 01:08:47 +08:00
|
|
|
"test_func",
|
|
|
|
"test_func",
|
|
|
|
"test_no_param",
|
|
|
|
]
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_function_with_square_brackets(self, pytester: Pytester) -> None:
|
2020-05-19 01:08:47 +08:00
|
|
|
"""Check that functions with square brackets don't cause trouble."""
|
2020-12-10 13:47:58 +08:00
|
|
|
p1 = pytester.makepyfile(
|
2020-05-19 01:08:47 +08:00
|
|
|
"""
|
|
|
|
locals()["test_foo[name]"] = lambda: None
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", str(p1))
|
2020-05-19 01:08:47 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_function_with_square_brackets.py::test_foo[[]name[]] PASSED *",
|
|
|
|
"*= 1 passed in *",
|
|
|
|
]
|
|
|
|
)
|
2016-08-04 09:56:12 +08:00
|
|
|
|
2015-07-25 21:38:11 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSorting:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_check_equality(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
fn1 = pytester.collect_by_name(modcol, "test_pass")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert isinstance(fn1, pytest.Function)
|
2020-12-10 13:47:58 +08:00
|
|
|
fn2 = pytester.collect_by_name(modcol, "test_pass")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert isinstance(fn2, pytest.Function)
|
|
|
|
|
|
|
|
assert fn1 == fn2
|
|
|
|
assert fn1 != modcol
|
|
|
|
assert hash(fn1) == hash(fn2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
fn3 = pytester.collect_by_name(modcol, "test_fail")
|
2012-11-02 23:04:57 +08:00
|
|
|
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:
|
2020-07-10 14:44:14 +08:00
|
|
|
assert fn != 3 # type: ignore[comparison-overlap]
|
2012-11-02 23:04:57 +08:00
|
|
|
assert fn != modcol
|
2020-07-10 14:44:14 +08:00
|
|
|
assert fn != [1, 2, 3] # type: ignore[comparison-overlap]
|
|
|
|
assert [1, 2, 3] != fn # type: ignore[comparison-overlap]
|
2012-11-02 23:04:57 +08:00
|
|
|
assert modcol != fn
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_allow_sane_sorting_for_decorators(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2021-10-01 21:17:37 +08:00
|
|
|
def test_ordered_by_definition_order(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""\
|
|
|
|
class Test1:
|
2023-09-05 18:54:20 +08:00
|
|
|
def test_foo(self): pass
|
|
|
|
def test_bar(self): pass
|
2021-10-01 21:17:37 +08:00
|
|
|
class Test2:
|
2023-09-05 18:54:20 +08:00
|
|
|
def test_foo(self): pass
|
2021-10-01 21:17:37 +08:00
|
|
|
test_bar = Test1.test_bar
|
|
|
|
class Test3(Test2):
|
2023-09-05 18:54:20 +08:00
|
|
|
def test_baz(self): pass
|
2021-10-01 21:17:37 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*Class Test1*",
|
|
|
|
"*Function test_foo*",
|
|
|
|
"*Function test_bar*",
|
|
|
|
"*Class Test2*",
|
|
|
|
# previously the order was flipped due to Test1.test_bar reference
|
|
|
|
"*Function test_foo*",
|
|
|
|
"*Function test_bar*",
|
|
|
|
"*Class Test3*",
|
|
|
|
"*Function test_foo*",
|
|
|
|
"*Function test_bar*",
|
|
|
|
"*Function test_baz*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConftestCustomization:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pytest_pycollect_module(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
class MyModule(pytest.Module):
|
|
|
|
pass
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_pycollect_makemodule(module_path, parent):
|
|
|
|
if module_path.name == "test_xyz.py":
|
|
|
|
return MyModule.from_parent(path=module_path, parent=parent)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile("def test_some(): pass")
|
|
|
|
pytester.makepyfile(test_xyz="def test_func(): pass")
|
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_customized_pymakemodule_issue205_subdir(self, pytester: Pytester) -> None:
|
|
|
|
b = pytester.path.joinpath("a", "b")
|
|
|
|
b.mkdir(parents=True)
|
|
|
|
b.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2023-06-13 03:30:06 +08:00
|
|
|
@pytest.hookimpl(wrapper=True)
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_pycollect_makemodule():
|
2023-06-13 03:30:06 +08:00
|
|
|
mod = yield
|
2018-08-24 00:06:17 +08:00
|
|
|
mod.obj.hello = "world"
|
2023-06-13 03:30:06 +08:00
|
|
|
return mod
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
b.joinpath("test_module.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_hello():
|
|
|
|
assert hello == "world"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_customized_pymakeitem(self, pytester: Pytester) -> None:
|
|
|
|
b = pytester.path.joinpath("a", "b")
|
|
|
|
b.mkdir(parents=True)
|
|
|
|
b.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2023-06-13 03:30:06 +08:00
|
|
|
@pytest.hookimpl(wrapper=True)
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_pycollect_makeitem():
|
2023-06-13 03:30:06 +08:00
|
|
|
result = yield
|
|
|
|
if result:
|
|
|
|
for func in result:
|
|
|
|
func._some123 = "world"
|
|
|
|
return result
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
b.joinpath("test_module.py").write_text(
|
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"
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pytest_pycollect_makeitem(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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":
|
2020-07-23 08:36:51 +08:00
|
|
|
return MyFunction.from_parent(name=name, parent=collector)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile("def some(): pass")
|
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue2369_collect_module_fileext(self, pytester: Pytester) -> None:
|
2017-04-20 03:26:56 +08:00
|
|
|
"""Ensure we can collect files with weird file extensions as Python
|
|
|
|
modules (#2369)"""
|
2023-05-27 01:28:21 +08:00
|
|
|
# Implement a little meta path finder to import files containing
|
2017-04-20 03:26:56 +08:00
|
|
|
# Python source code whose file extension is ".narf".
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-05-27 01:28:21 +08:00
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
from importlib.util import spec_from_loader
|
|
|
|
from importlib.machinery import SourceFileLoader
|
2017-04-20 03:26:56 +08:00
|
|
|
from _pytest.python import Module
|
|
|
|
|
2023-05-27 01:28:21 +08:00
|
|
|
class MetaPathFinder:
|
|
|
|
def find_spec(self, fullname, path, target=None):
|
|
|
|
if os.path.exists(fullname + ".narf"):
|
|
|
|
return spec_from_loader(
|
|
|
|
fullname,
|
|
|
|
SourceFileLoader(fullname, fullname + ".narf"),
|
|
|
|
)
|
|
|
|
sys.meta_path.append(MetaPathFinder())
|
2017-04-20 03:26:56 +08:00
|
|
|
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_collect_file(file_path, parent):
|
|
|
|
if file_path.suffix == ".narf":
|
2023-05-27 01:28:21 +08:00
|
|
|
return Module.from_parent(path=file_path, parent=parent)
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makefile(
|
2018-05-23 22:48:46 +08:00
|
|
|
".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.
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2017-04-20 03:26:56 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_early_ignored_attributes(self, pytester: Pytester) -> None:
|
2020-08-21 22:27:06 +08:00
|
|
|
"""Builtin attributes should be ignored early on, even if
|
|
|
|
configuration would otherwise allow them.
|
|
|
|
|
|
|
|
This tests a performance optimization, not correctness, really,
|
|
|
|
although it tests PytestCollectionWarning is not raised, while
|
|
|
|
it would have been raised otherwise.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeini(
|
2020-08-21 22:27:06 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
python_classes=*
|
|
|
|
python_functions=*
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2020-08-21 22:27:06 +08:00
|
|
|
"""
|
|
|
|
class TestEmpty:
|
|
|
|
pass
|
|
|
|
test_empty = TestEmpty()
|
|
|
|
def test_real():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, rec = pytester.inline_genitems()
|
2020-08-21 22:27:06 +08:00
|
|
|
assert rec.ret == 0
|
|
|
|
assert len(items) == 1
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_only_available_in_subdir(pytester: Pytester) -> None:
|
|
|
|
sub1 = pytester.mkpydir("sub1")
|
|
|
|
sub2 = pytester.mkpydir("sub2")
|
|
|
|
sub1.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def pytest_runtest_setup(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub1"
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_runtest_call(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub1"
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_runtest_teardown(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub1"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
sub2.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def pytest_runtest_setup(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub2"
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_runtest_call(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub2"
|
2018-08-24 00:06:17 +08:00
|
|
|
def pytest_runtest_teardown(item):
|
2021-03-15 22:01:58 +08:00
|
|
|
assert item.path.stem == "test_in_sub2"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2023-06-20 19:55:40 +08:00
|
|
|
sub1.joinpath("test_in_sub1.py").write_text("def test_1(): pass", encoding="utf-8")
|
|
|
|
sub2.joinpath("test_in_sub2.py").write_text("def test_2(): pass", encoding="utf-8")
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", "-s")
|
2015-04-28 17:54:46 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_modulecol_roundtrip(pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.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
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTracebackCutting:
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_skip_simple(self):
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception) as excinfo:
|
|
|
|
pytest.skip("xxx")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert excinfo.traceback[-1].frame.code.name == "skip"
|
2023-04-13 04:17:54 +08:00
|
|
|
assert excinfo.traceback[-1].ishidden(excinfo)
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
assert excinfo.traceback[-2].frame.code.name == "test_skip_simple"
|
2023-04-13 04:17:54 +08:00
|
|
|
assert not excinfo.traceback[-2].ishidden(excinfo)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_traceback_argsetup(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
p = pytester.makepyfile("def test(hello): pass")
|
|
|
|
result = pytester.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
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_traceback_error_during_import(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
x = 1
|
|
|
|
x = 2
|
|
|
|
x = 17
|
|
|
|
asd
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_traceback_filter_error_during_fixture_collection(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Integration test for issue #995."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2015-09-30 09:29:43 +08:00
|
|
|
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
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_filter_traceback_generated_code(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that filter_traceback() works with the fact that
|
2019-01-21 03:59:48 +08:00
|
|
|
_pytest._code.code.Code.path attribute might return an str object.
|
2020-07-18 17:35:13 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
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.
|
|
|
|
"""
|
2020-07-31 14:46:56 +08:00
|
|
|
from _pytest._code import filter_traceback
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
tb = None
|
2015-09-30 09:29:43 +08:00
|
|
|
try:
|
2020-10-06 09:13:05 +08:00
|
|
|
ns: Dict[str, Any] = {}
|
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()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert tb is not None
|
|
|
|
traceback = _pytest._code.Traceback(tb)
|
|
|
|
assert isinstance(traceback[-1].path, str)
|
|
|
|
assert not filter_traceback(traceback[-1])
|
2015-09-30 09:29:43 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_filter_traceback_path_no_longer_valid(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that filter_traceback() works with the fact that
|
2019-01-21 03:59:48 +08:00
|
|
|
_pytest._code.code.Code.path attribute might return an str object.
|
2020-07-18 17:35:13 +08:00
|
|
|
|
2015-10-17 07:08:12 +08:00
|
|
|
In this case, one of the files in the traceback no longer exists.
|
|
|
|
This fixes #1133.
|
|
|
|
"""
|
2020-07-31 14:46:56 +08:00
|
|
|
from _pytest._code import filter_traceback
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
tb = None
|
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()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert tb is not None
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.path.joinpath("filter_traceback_entry_as_str.py").unlink()
|
2020-05-01 19:40:17 +08:00
|
|
|
traceback = _pytest._code.Traceback(tb)
|
|
|
|
assert isinstance(traceback[-1].path, str)
|
|
|
|
assert filter_traceback(traceback[-1])
|
2015-10-17 07:08:12 +08:00
|
|
|
|
2015-09-30 09:29:43 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestReportInfo:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_itemreport_reportinfo(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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":
|
2019-10-17 03:52:04 +08:00
|
|
|
return MyFunction.from_parent(name=name, parent=collector)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
item = pytester.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")
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_reportinfo(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-10-09 19:06:28 +08:00
|
|
|
path, lineno, modpath = item.reportinfo()
|
|
|
|
assert os.fspath(path) == str(item.path)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert lineno == 0
|
|
|
|
assert modpath == "test_func"
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_reportinfo(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
classcol = pytester.collect_by_name(modcol, "TestClass")
|
|
|
|
assert isinstance(classcol, Class)
|
2021-10-09 19:06:28 +08:00
|
|
|
path, lineno, msg = classcol.reportinfo()
|
|
|
|
assert os.fspath(path) == str(modcol.path)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert lineno == 1
|
|
|
|
assert msg == "TestClass"
|
|
|
|
|
2018-09-14 02:44:02 +08:00
|
|
|
@pytest.mark.filterwarnings(
|
|
|
|
"ignore:usage of Generator.Function is deprecated, please use pytest.Function instead"
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_reportinfo_with_nasty_getattr(self, pytester: Pytester) -> None:
|
2015-11-30 23:13:15 +08:00
|
|
|
# https://github.com/pytest-dev/pytest/issues/1204
|
2020-12-10 13:47:58 +08:00
|
|
|
modcol = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-11-30 23:13:15 +08:00
|
|
|
# lineno 0
|
2021-11-13 20:03:44 +08:00
|
|
|
class TestClass:
|
2015-11-30 23:13:15 +08:00
|
|
|
def __getattr__(self, name):
|
|
|
|
return "this is not an int"
|
|
|
|
|
2021-11-13 20:03:44 +08:00
|
|
|
def __class_getattr__(cls, name):
|
|
|
|
return "this is not an int"
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def intest_foo(self):
|
2015-11-30 23:13:15 +08:00
|
|
|
pass
|
2021-11-13 20:03:44 +08:00
|
|
|
|
|
|
|
def test_bar(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
classcol = pytester.collect_by_name(modcol, "TestClass")
|
|
|
|
assert isinstance(classcol, Class)
|
2021-11-13 20:03:44 +08:00
|
|
|
path, lineno, msg = classcol.reportinfo()
|
2024-02-02 22:08:22 +08:00
|
|
|
func = next(iter(classcol.collect()))
|
2021-11-13 20:03:44 +08:00
|
|
|
assert isinstance(func, Function)
|
|
|
|
path, lineno, msg = func.reportinfo()
|
2015-11-30 23:13:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_customized_python_discovery(pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
p2 = p.with_name(p.name.replace("test", "check"))
|
|
|
|
p.rename(p2)
|
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_customized_python_discovery_functions(pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-28 00:58:08 +08:00
|
|
|
[pytest]
|
|
|
|
python_functions=_test
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-28 00:58:08 +08:00
|
|
|
def _test_underscore():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_unorderable_types(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*TypeError*")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2015-07-03 10:13:59 +08:00
|
|
|
|
|
|
|
|
2021-05-04 20:27:21 +08:00
|
|
|
@pytest.mark.filterwarnings("default::pytest.PytestCollectionWarning")
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_dont_collect_non_function_callable(pytester: Pytester) -> None:
|
2015-09-23 07:06:55 +08:00
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/331
|
|
|
|
|
|
|
|
In this case an INTERNALERROR occurred trying to report the failure of
|
2019-10-26 21:00:04 +08:00
|
|
|
a test like this one because pytest failed to get the source lines.
|
2015-09-23 07:06:55 +08:00
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
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*",
|
2019-10-27 23:02:37 +08:00
|
|
|
"*1 passed, 1 warning in *",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2015-12-11 06:45:36 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_injection_does_not_break_collection(pytester: Pytester) -> None:
|
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.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_syntax_error_with_non_ascii_chars(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Fix decoding issue while formatting SyntaxErrors during collection (#578)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile("☃")
|
|
|
|
result = pytester.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
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_collect_error_with_fulltrace(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("assert 0")
|
|
|
|
result = pytester.runpytest("--fulltrace")
|
2019-11-21 01:58:02 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 0 items / 1 error",
|
|
|
|
"",
|
|
|
|
"*= ERRORS =*",
|
2020-05-25 19:53:56 +08:00
|
|
|
"*_ ERROR collecting test_collect_error_with_fulltrace.py _*",
|
2019-11-21 01:58:02 +08:00
|
|
|
"",
|
|
|
|
"> assert 0",
|
|
|
|
"E assert 0",
|
|
|
|
"",
|
2020-05-25 19:53:56 +08:00
|
|
|
"test_collect_error_with_fulltrace.py:1: AssertionError",
|
2019-11-21 01:58:02 +08:00
|
|
|
"*! Interrupted: 1 error during collection !*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_skip_duplicates_by_default(pytester: Pytester) -> None:
|
2016-07-25 18:40:57 +08:00
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)
|
|
|
|
|
|
|
|
Ignore duplicate directories.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
a = pytester.mkdir("a")
|
|
|
|
fh = a.joinpath("test_a.py")
|
|
|
|
fh.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def test_real():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(str(a), str(a))
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
2016-07-25 18:40:57 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_keep_duplicates(pytester: Pytester) -> None:
|
2016-07-25 18:40:57 +08:00
|
|
|
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)
|
|
|
|
|
|
|
|
Use --keep-duplicates to collect tests from duplicate directories.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
a = pytester.mkdir("a")
|
|
|
|
fh = a.joinpath("test_a.py")
|
|
|
|
fh.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
def test_real():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--keep-duplicates", str(a), str(a))
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 2 item*"])
|
2018-08-03 02:18:36 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_collection_infinite_recursion(pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example("collect/package_infinite_recursion")
|
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2018-08-11 08:29:30 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_collection_init_given_as_argument(pytester: Pytester) -> None:
|
2023-05-27 19:42:19 +08:00
|
|
|
"""Regression test for #3749, #8976, #9263, #9313.
|
|
|
|
|
|
|
|
Specifying an __init__.py file directly should collect only the __init__.py
|
|
|
|
Module, not the entire package.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
p = pytester.copy_example("collect/package_init_given_as_arg")
|
2023-05-27 19:42:19 +08:00
|
|
|
items, hookrecorder = pytester.inline_genitems(p / "pkg" / "__init__.py")
|
|
|
|
assert len(items) == 1
|
|
|
|
assert items[0].name == "test_init"
|
2018-09-21 04:52:35 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_with_modules(pytester: Pytester) -> None:
|
2018-08-11 08:29:30 +08:00
|
|
|
"""
|
|
|
|
.
|
|
|
|
└── root
|
|
|
|
├── __init__.py
|
|
|
|
├── sub1
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ └── sub1_1
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ └── test_in_sub1.py
|
|
|
|
└── sub2
|
|
|
|
└── test
|
|
|
|
└── test_in_sub2.py
|
|
|
|
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
root = pytester.mkpydir("root")
|
|
|
|
sub1 = root.joinpath("sub1")
|
|
|
|
sub1_test = sub1.joinpath("sub1_1")
|
|
|
|
sub1_test.mkdir(parents=True)
|
|
|
|
for d in (sub1, sub1_test):
|
|
|
|
d.joinpath("__init__.py").touch()
|
2018-08-11 08:29:30 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
sub2 = root.joinpath("sub2")
|
|
|
|
sub2_test = sub2.joinpath("test")
|
|
|
|
sub2_test.mkdir(parents=True)
|
|
|
|
|
2023-06-20 19:55:40 +08:00
|
|
|
sub1_test.joinpath("test_in_sub1.py").write_text(
|
|
|
|
"def test_1(): pass", encoding="utf-8"
|
|
|
|
)
|
|
|
|
sub2_test.joinpath("test_in_sub2.py").write_text(
|
|
|
|
"def test_2(): pass", encoding="utf-8"
|
|
|
|
)
|
2018-08-11 08:29:30 +08:00
|
|
|
|
|
|
|
# Execute from .
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", "-s")
|
2018-08-11 08:29:30 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
|
|
|
|
|
|
|
# Execute from . with one argument "root"
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", "-s", "root")
|
2018-08-11 08:29:30 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
|
|
|
|
|
|
|
# Chdir into package's root and execute with no args
|
2020-12-10 13:47:58 +08:00
|
|
|
os.chdir(root)
|
|
|
|
result = pytester.runpytest("-v", "-s")
|
2018-08-11 08:29:30 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2018-08-23 12:42:59 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_ordering(pytester: Pytester) -> None:
|
2018-08-23 12:42:59 +08:00
|
|
|
"""
|
|
|
|
.
|
|
|
|
└── 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
|
|
|
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeini(
|
2018-08-23 12:42:59 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
2018-08-25 07:23:50 +08:00
|
|
|
python_files=*.py
|
2018-08-23 12:42:59 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
root = pytester.mkpydir("root")
|
|
|
|
sub1 = root.joinpath("sub1")
|
|
|
|
sub1.mkdir()
|
|
|
|
sub1.joinpath("__init__.py").touch()
|
|
|
|
sub2 = root.joinpath("sub2")
|
|
|
|
sub2_test = sub2.joinpath("test")
|
|
|
|
sub2_test.mkdir(parents=True)
|
|
|
|
|
2023-06-20 19:55:40 +08:00
|
|
|
root.joinpath("Test_root.py").write_text("def test_1(): pass", encoding="utf-8")
|
|
|
|
sub1.joinpath("Test_sub1.py").write_text("def test_2(): pass", encoding="utf-8")
|
|
|
|
sub2_test.joinpath("test_sub2.py").write_text(
|
|
|
|
"def test_3(): pass", encoding="utf-8"
|
|
|
|
)
|
2018-08-23 12:42:59 +08:00
|
|
|
|
|
|
|
# Execute from .
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", "-s")
|
2018-08-23 12:42:59 +08:00
|
|
|
result.assert_outcomes(passed=3)
|
2023-06-02 21:03:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_collection_hierarchy(pytester: Pytester) -> None:
|
|
|
|
"""A general test checking that a filesystem hierarchy is collected as
|
|
|
|
expected in various scenarios.
|
|
|
|
|
|
|
|
top/
|
|
|
|
├── aaa
|
|
|
|
│ ├── pkg
|
|
|
|
│ │ ├── __init__.py
|
|
|
|
│ │ └── test_pkg.py
|
|
|
|
│ └── test_aaa.py
|
|
|
|
├── test_a.py
|
|
|
|
├── test_b
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ └── test_b.py
|
|
|
|
├── test_c.py
|
|
|
|
└── zzz
|
|
|
|
├── dir
|
|
|
|
│ └── test_dir.py
|
|
|
|
├── __init__.py
|
|
|
|
└── test_zzz.py
|
|
|
|
"""
|
|
|
|
pytester.makepyfile(
|
|
|
|
**{
|
|
|
|
"top/aaa/test_aaa.py": "def test_it(): pass",
|
|
|
|
"top/aaa/pkg/__init__.py": "",
|
|
|
|
"top/aaa/pkg/test_pkg.py": "def test_it(): pass",
|
|
|
|
"top/test_a.py": "def test_it(): pass",
|
|
|
|
"top/test_b/__init__.py": "",
|
|
|
|
"top/test_b/test_b.py": "def test_it(): pass",
|
|
|
|
"top/test_c.py": "def test_it(): pass",
|
|
|
|
"top/zzz/__init__.py": "",
|
|
|
|
"top/zzz/test_zzz.py": "def test_it(): pass",
|
|
|
|
"top/zzz/dir/test_dir.py": "def test_it(): pass",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
full = [
|
|
|
|
"<Dir test_collection_hierarchy*>",
|
|
|
|
" <Dir top>",
|
|
|
|
" <Dir aaa>",
|
|
|
|
" <Package pkg>",
|
|
|
|
" <Module test_pkg.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_aaa.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_a.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Package test_b>",
|
|
|
|
" <Module test_b.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_c.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Package zzz>",
|
|
|
|
" <Dir dir>",
|
|
|
|
" <Module test_dir.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_zzz.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
]
|
|
|
|
result = pytester.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines(full, consecutive=True)
|
|
|
|
result = pytester.runpytest("top", "--collect-only")
|
|
|
|
result.stdout.fnmatch_lines(full, consecutive=True)
|
|
|
|
result = pytester.runpytest("top", "top", "--collect-only")
|
|
|
|
result.stdout.fnmatch_lines(full, consecutive=True)
|
|
|
|
|
|
|
|
result = pytester.runpytest(
|
|
|
|
"top/aaa", "top/aaa/pkg", "--collect-only", "--keep-duplicates"
|
|
|
|
)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"<Dir test_collection_hierarchy*>",
|
|
|
|
" <Dir top>",
|
|
|
|
" <Dir aaa>",
|
|
|
|
" <Package pkg>",
|
|
|
|
" <Module test_pkg.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_aaa.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Package pkg>",
|
|
|
|
" <Module test_pkg.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = pytester.runpytest(
|
|
|
|
"top/aaa/pkg", "top/aaa", "--collect-only", "--keep-duplicates"
|
|
|
|
)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"<Dir test_collection_hierarchy*>",
|
|
|
|
" <Dir top>",
|
|
|
|
" <Dir aaa>",
|
|
|
|
" <Package pkg>",
|
|
|
|
" <Module test_pkg.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Function test_it>",
|
|
|
|
" <Module test_aaa.py>",
|
|
|
|
" <Function test_it>",
|
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|