2018-11-09 03:35:53 +08:00
|
|
|
import py
|
|
|
|
|
2017-10-23 20:26:42 +08:00
|
|
|
import pytest
|
2017-10-24 00:49:49 +08:00
|
|
|
from _pytest import nodes
|
2020-05-01 19:40:17 +08:00
|
|
|
from _pytest.pytester import Testdir
|
2017-10-23 20:26:42 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"baseid, nodeid, expected",
|
|
|
|
(
|
|
|
|
("", "", True),
|
|
|
|
("", "foo", True),
|
|
|
|
("", "foo/bar", True),
|
2018-11-09 18:03:07 +08:00
|
|
|
("", "foo/bar::TestBaz", True),
|
2018-05-23 22:48:46 +08:00
|
|
|
("foo", "food", False),
|
2018-11-09 18:03:07 +08:00
|
|
|
("foo/bar::TestBaz", "foo/bar", False),
|
|
|
|
("foo/bar::TestBaz", "foo/bar::TestBop", False),
|
|
|
|
("foo/bar", "foo/bar::TestBop", True),
|
2018-05-23 22:48:46 +08:00
|
|
|
),
|
|
|
|
)
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_ischildnode(baseid: str, nodeid: str, expected: bool) -> None:
|
2017-10-24 00:49:49 +08:00
|
|
|
result = nodes.ischildnode(baseid, nodeid)
|
2017-10-23 20:26:42 +08:00
|
|
|
assert result is expected
|
2018-09-05 01:20:42 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_node_from_parent_disallowed_arguments() -> None:
|
2020-01-01 03:20:28 +08:00
|
|
|
with pytest.raises(TypeError, match="session is"):
|
2020-05-01 19:40:17 +08:00
|
|
|
nodes.Node.from_parent(None, session=None) # type: ignore[arg-type] # noqa: F821
|
2020-01-01 03:20:28 +08:00
|
|
|
with pytest.raises(TypeError, match="config is"):
|
2020-05-01 19:40:17 +08:00
|
|
|
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type] # noqa: F821
|
2020-01-01 03:20:28 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_std_warn_not_pytestwarning(testdir: Testdir) -> None:
|
2018-09-05 01:20:42 +08:00
|
|
|
items = testdir.getitems(
|
|
|
|
"""
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
with pytest.raises(ValueError, match=".*instance of PytestWarning.*"):
|
2018-09-05 02:07:52 +08:00
|
|
|
items[0].warn(UserWarning("some warning"))
|
2018-11-09 03:35:53 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test__check_initialpaths_for_relpath() -> None:
|
2018-11-09 03:35:53 +08:00
|
|
|
"""Ensure that it handles dirs, and does not always use dirname."""
|
|
|
|
cwd = py.path.local()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
class FakeSession1:
|
2018-11-09 03:35:53 +08:00
|
|
|
_initialpaths = [cwd]
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(FakeSession1, cwd) == ""
|
2018-11-09 03:35:53 +08:00
|
|
|
|
|
|
|
sub = cwd.join("file")
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
class FakeSession2:
|
2018-11-09 03:35:53 +08:00
|
|
|
_initialpaths = [cwd]
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(FakeSession2, sub) == "file"
|
2018-11-09 03:35:53 +08:00
|
|
|
|
|
|
|
outside = py.path.local("/outside")
|
2020-05-01 19:40:17 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(FakeSession2, outside) is None
|
2020-05-16 22:36:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_failure_with_changed_cwd(testdir):
|
|
|
|
"""
|
|
|
|
Test failure lines should use absolute paths if cwd has changed since
|
|
|
|
invocation, so the path is correct (#6428).
|
|
|
|
"""
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def private_dir():
|
|
|
|
out_dir = 'ddd'
|
|
|
|
os.mkdir(out_dir)
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(out_dir)
|
|
|
|
yield out_dir
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
def test_show_wrong_path(private_dir):
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([str(p) + ":*: AssertionError", "*1 failed in *"])
|