2016-06-09 14:22:11 +08:00
|
|
|
import os
|
2015-07-19 01:40:00 +08:00
|
|
|
import sys
|
2017-12-27 11:47:26 +08:00
|
|
|
import types
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-02-08 06:59:10 +08:00
|
|
|
import attr
|
2017-12-07 05:55:04 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import pytest
|
2019-07-03 07:46:44 +08:00
|
|
|
from _pytest.compat import importlib_metadata
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-06-08 21:56:40 +08:00
|
|
|
from _pytest.pathlib import symlink_or_skip
|
2020-10-12 23:13:06 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def prepend_pythonpath(*dirs) -> str:
|
2018-10-12 21:15:35 +08:00
|
|
|
cur = os.getenv("PYTHONPATH")
|
|
|
|
if cur:
|
|
|
|
dirs += (cur,)
|
|
|
|
return os.pathsep.join(str(p) for p in dirs)
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestGeneralUsage:
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_config_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example("conftest_usageerror/conftest.py")
|
|
|
|
result = pytester.runpytest(pytester.path)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*ERROR: hello"])
|
2018-11-09 08:31:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*pytest_unconfigure_called"])
|
2009-03-17 18:29:45 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_root_conftest_syntax_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(conftest="raise SyntaxError\n")
|
|
|
|
result = pytester.runpytest()
|
2011-11-08 02:08:41 +08:00
|
|
|
result.stderr.fnmatch_lines(["*raise SyntaxError*"])
|
|
|
|
assert result.ret != 0
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_early_hook_error_issue38_1(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-04-17 18:20:11 +08:00
|
|
|
def pytest_sessionstart():
|
|
|
|
0 / 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(pytester.path)
|
2011-04-17 18:20:11 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
# tracestyle is native by default for hook failures
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(pytester.path, "--fulltrace")
|
2011-04-17 18:20:11 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
# tracestyle is native by default for hook failures
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"]
|
|
|
|
)
|
2011-04-17 18:20:11 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_early_hook_configure_error_issue38(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-04-17 18:20:13 +08:00
|
|
|
def pytest_configure():
|
|
|
|
0 / 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(pytester.path)
|
2011-04-17 18:20:13 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
# here we get it on stderr
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]
|
|
|
|
)
|
2011-04-17 18:20:13 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_file_not_found(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("asd")
|
2010-11-01 15:16:10 +08:00
|
|
|
assert result.ret != 0
|
2020-08-15 21:31:17 +08:00
|
|
|
result.stderr.fnmatch_lines(["ERROR: file or directory not found: asd"])
|
2010-11-01 15:16:10 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_file_not_found_unconfigure_issue143(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-05-18 05:11:23 +08:00
|
|
|
def pytest_configure():
|
|
|
|
print("---configure")
|
|
|
|
def pytest_unconfigure():
|
|
|
|
print("---unconfigure")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("-s", "asd")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2020-08-15 21:31:17 +08:00
|
|
|
result.stderr.fnmatch_lines(["ERROR: file or directory not found: asd"])
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"])
|
2012-10-01 16:14:54 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_config_preparse_plugin_option(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest_xyz="""
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", dest="xyz", action="store")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
test_one="""
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_option(pytestconfig):
|
|
|
|
assert pytestconfig.option.xyz == "123"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True)
|
2009-04-18 02:09:29 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2009-04-18 02:09:29 +08:00
|
|
|
|
2019-02-08 06:59:10 +08:00
|
|
|
@pytest.mark.parametrize("load_cov_early", [True, False])
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_early_load_setuptools_name(
|
|
|
|
self, pytester: Pytester, monkeypatch, load_cov_early
|
|
|
|
) -> None:
|
2020-02-04 09:59:20 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(mytestplugin1_module="")
|
|
|
|
pytester.makepyfile(mytestplugin2_module="")
|
|
|
|
pytester.makepyfile(mycov_module="")
|
|
|
|
pytester.syspathinsert()
|
2019-02-08 06:59:10 +08:00
|
|
|
|
|
|
|
loaded = []
|
|
|
|
|
|
|
|
@attr.s
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyEntryPoint:
|
2019-02-08 06:59:10 +08:00
|
|
|
name = attr.ib()
|
|
|
|
module = attr.ib()
|
2019-04-07 08:32:47 +08:00
|
|
|
group = "pytest11"
|
2019-02-08 06:59:10 +08:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
__import__(self.module)
|
|
|
|
loaded.append(self.name)
|
|
|
|
return sys.modules[self.module]
|
|
|
|
|
|
|
|
entry_points = [
|
|
|
|
DummyEntryPoint("myplugin1", "mytestplugin1_module"),
|
|
|
|
DummyEntryPoint("myplugin2", "mytestplugin2_module"),
|
|
|
|
DummyEntryPoint("mycov", "mycov_module"),
|
|
|
|
]
|
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
@attr.s
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyDist:
|
2019-04-07 08:32:47 +08:00
|
|
|
entry_points = attr.ib()
|
|
|
|
files = ()
|
|
|
|
|
|
|
|
def my_dists():
|
|
|
|
return (DummyDist(entry_points),)
|
2019-02-08 06:59:10 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
monkeypatch.setattr(importlib_metadata, "distributions", my_dists)
|
2019-02-08 06:59:10 +08:00
|
|
|
params = ("-p", "mycov") if load_cov_early else ()
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.runpytest_inprocess(*params)
|
2019-02-08 06:59:10 +08:00
|
|
|
if load_cov_early:
|
|
|
|
assert loaded == ["mycov", "myplugin1", "myplugin2"]
|
|
|
|
else:
|
|
|
|
assert loaded == ["myplugin1", "myplugin2", "mycov"]
|
|
|
|
|
2020-06-13 22:29:01 +08:00
|
|
|
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_assertion_rewrite(self, pytester: Pytester, import_mode) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_this():
|
|
|
|
x = 0
|
|
|
|
assert x
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(p, f"--import-mode={import_mode}")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["> assert x", "E assert 0"])
|
2008-08-16 23:26:59 +08:00
|
|
|
assert result.ret == 1
|
2009-03-17 15:03:49 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_nested_import_error(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2008-08-16 23:26:59 +08:00
|
|
|
import import_fails
|
|
|
|
def test_this():
|
|
|
|
assert import_fails.a == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(import_fails="import does_not_work")
|
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ImportError while importing test module*",
|
|
|
|
"*No module named *does_not_work*",
|
|
|
|
]
|
|
|
|
)
|
2016-06-20 21:05:50 +08:00
|
|
|
assert result.ret == 2
|
2009-08-06 21:26:45 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_not_collectable_arguments(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile("")
|
|
|
|
p2 = pytester.makefile(".pyc", "123")
|
|
|
|
result = pytester.runpytest(p1, p2)
|
2019-11-07 02:51:58 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
2022-03-29 18:33:13 +08:00
|
|
|
f"ERROR: found no collectors for {p2}",
|
2019-11-07 02:51:58 +08:00
|
|
|
"",
|
|
|
|
]
|
|
|
|
)
|
2009-12-29 23:29:48 +08:00
|
|
|
|
2018-12-12 06:02:36 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_better_reporting_on_conftest_load_failure(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2018-10-12 21:01:30 +08:00
|
|
|
"""Show a user-friendly traceback on conftest import failures (#486, #3332)"""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile("")
|
|
|
|
conftest = pytester.makeconftest(
|
2018-10-12 21:01:30 +08:00
|
|
|
"""
|
|
|
|
def foo():
|
|
|
|
import qwerty
|
|
|
|
foo()
|
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--help")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-04-03 02:42:41 +08:00
|
|
|
*--version*
|
|
|
|
*warning*conftest.py*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2020-02-15 00:00:01 +08:00
|
|
|
assert result.stdout.lines == []
|
|
|
|
assert result.stderr.lines == [
|
2020-10-03 04:16:22 +08:00
|
|
|
f"ImportError while loading conftest '{conftest}'.",
|
2020-02-15 00:00:01 +08:00
|
|
|
"conftest.py:3: in <module>",
|
|
|
|
" foo()",
|
|
|
|
"conftest.py:2: in foo",
|
|
|
|
" import qwerty",
|
2020-10-03 03:06:21 +08:00
|
|
|
"E ModuleNotFoundError: No module named 'qwerty'",
|
2020-02-15 00:00:01 +08:00
|
|
|
]
|
2009-12-29 23:29:48 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_early_skip(self, pytester: Pytester) -> None:
|
|
|
|
pytester.mkdir("xyz")
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2020-08-18 04:55:37 +08:00
|
|
|
def pytest_collect_file():
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("early")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 skip*"])
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_issue88_initial_file_multinodes(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example("issue88_initial_file_multinodes")
|
|
|
|
p = pytester.makepyfile("def test_hello(): pass")
|
|
|
|
result = pytester.runpytest(p, "--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"])
|
2010-05-14 18:02:43 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_issue93_initialnode_importing_capturing(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-10-25 05:43:35 +08:00
|
|
|
import sys
|
2018-11-22 16:15:14 +08:00
|
|
|
print("should not be seen")
|
2010-10-25 05:43:35 +08:00
|
|
|
sys.stderr.write("stder42\\n")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*should not be seen*")
|
2010-10-25 05:43:35 +08:00
|
|
|
assert "stderr42" not in result.stderr.str()
|
2010-08-02 02:43:02 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_conftest_printing_shows_if_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-11-22 16:15:14 +08:00
|
|
|
print("should be seen")
|
2010-10-25 05:26:14 +08:00
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2010-10-25 05:26:14 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
assert "should be seen" in result.stdout.str()
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_issue109_sibling_conftests_not_loaded(self, pytester: Pytester) -> None:
|
|
|
|
sub1 = pytester.mkdir("sub1")
|
|
|
|
sub2 = pytester.mkdir("sub2")
|
|
|
|
sub1.joinpath("conftest.py").write_text("assert 0")
|
|
|
|
result = pytester.runpytest(sub2)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2020-11-25 20:42:47 +08:00
|
|
|
sub2.joinpath("__init__.py").touch()
|
|
|
|
p = sub2.joinpath("test_hello.py")
|
|
|
|
p.touch()
|
|
|
|
result = pytester.runpytest(p)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(sub1)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2010-10-04 22:19:01 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_directory_skipped(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-10-04 22:19:01 +08:00
|
|
|
def pytest_ignore_collect():
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("intentional")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile("def test_hello(): pass")
|
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 skipped*"])
|
2010-09-28 21:24:36 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_multiple_items_per_collector_byid(self, pytester: Pytester) -> None:
|
|
|
|
c = pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyItem(pytest.Item):
|
2010-09-28 21:24:36 +08:00
|
|
|
def runtest(self):
|
|
|
|
pass
|
2010-11-13 16:05:11 +08:00
|
|
|
class MyCollector(pytest.File):
|
2010-09-28 21:24:36 +08:00
|
|
|
def collect(self):
|
2020-07-23 08:36:51 +08:00
|
|
|
return [MyItem.from_parent(name="xyz", parent=self)]
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_collect_file(file_path, parent):
|
|
|
|
if file_path.name.startswith("conftest"):
|
|
|
|
return MyCollector.from_parent(path=file_path, parent=parent)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(c.name + "::" + "xyz")
|
2010-09-28 21:24:36 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 pass*"])
|
2010-09-28 21:24:36 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_skip_on_generated_funcarg_id(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-10-13 18:26:14 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('x', [3], ids=['hello-123'])
|
2010-10-13 18:26:14 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2018-11-22 16:15:14 +08:00
|
|
|
print(item.keywords)
|
2010-10-13 18:26:14 +08:00
|
|
|
if 'hello-123' in item.keywords:
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("hello")
|
2010-10-13 18:26:14 +08:00
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
p = pytester.makepyfile("""def test_func(x): pass""")
|
|
|
|
res = pytester.runpytest(p)
|
2010-10-13 18:26:14 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
res.stdout.fnmatch_lines(["*1 skipped*"])
|
2011-09-12 14:57:35 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_direct_addressing_selects(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-06 16:58:04 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('i', [1, 2], ids=["1", "2"])
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_func(i):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p.name + "::" + "test_func[1]")
|
2010-11-06 16:58:04 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
res.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_direct_addressing_notfound(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p.name + "::" + "test_notfound")
|
2010-11-06 16:58:04 +08:00
|
|
|
assert res.ret
|
|
|
|
res.stderr.fnmatch_lines(["*ERROR*not found*"])
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_docstring_on_hookspec(self) -> None:
|
2010-12-09 18:00:31 +08:00
|
|
|
from _pytest import hookspec
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-12-09 18:00:31 +08:00
|
|
|
for name, value in vars(hookspec).items():
|
|
|
|
if name.startswith("pytest_"):
|
|
|
|
assert value.__doc__, "no docstring for %s" % name
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_initialization_error_issue49(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-06-01 20:54:34 +08:00
|
|
|
def pytest_configure():
|
|
|
|
x
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2017-07-17 07:25:09 +08:00
|
|
|
assert result.ret == 3 # internal error
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"])
|
|
|
|
assert "sessionstarttime" not in result.stderr.str()
|
2011-06-01 20:54:34 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"])
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_issue134_report_error_when_collecting_member(
|
|
|
|
self, pytester: Pytester, lookfor
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
test_fun="""
|
2013-02-14 19:21:42 +08:00
|
|
|
def test_a():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
def"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(lookfor)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*SyntaxError*"])
|
|
|
|
if "::" in lookfor:
|
|
|
|
result.stderr.fnmatch_lines(["*ERROR*"])
|
2013-02-14 19:21:42 +08:00
|
|
|
assert result.ret == 4 # usage error only if item not found
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_report_all_failed_collections_initargs(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2019-04-19 05:18:59 +08:00
|
|
|
"""
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2019-04-19 05:18:59 +08:00
|
|
|
|
|
|
|
def pytest_sessionfinish(exitstatus):
|
2019-06-07 18:58:51 +08:00
|
|
|
assert exitstatus == ExitCode.USAGE_ERROR
|
2019-04-19 05:18:59 +08:00
|
|
|
print("pytest_sessionfinish_called")
|
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(test_a="def", test_b="def")
|
|
|
|
result = pytester.runpytest("test_a.py::a", "test_b.py::b")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"])
|
2019-04-19 05:18:59 +08:00
|
|
|
result.stdout.fnmatch_lines(["pytest_sessionfinish_called"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2013-09-09 04:26:51 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_namespace_import_doesnt_confuse_import_hook(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Ref #383.
|
|
|
|
|
|
|
|
Python 3.3's namespace package messed with our import hooks.
|
2017-03-21 09:01:22 +08:00
|
|
|
Importing a module that didn't exist, even if the ImportError was
|
|
|
|
gracefully handled, would make our test crash.
|
|
|
|
"""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.mkdir("not_a_package")
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-11-16 03:03:57 +08:00
|
|
|
try:
|
|
|
|
from not_a_package import doesnt_exist
|
|
|
|
except ImportError:
|
|
|
|
# We handle the import error gracefully here
|
|
|
|
pass
|
2014-01-18 17:40:20 +08:00
|
|
|
|
2013-11-16 03:03:57 +08:00
|
|
|
def test_whatever():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p.name)
|
2013-11-16 03:03:57 +08:00
|
|
|
assert res.ret == 0
|
2013-02-14 19:21:42 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_unknown_option(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--qwlkej")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
"""
|
2014-04-01 20:19:58 +08:00
|
|
|
*unrecognized*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-04-01 20:19:58 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_getsourcelines_error_issue553(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2015-04-28 17:54:46 +08:00
|
|
|
monkeypatch.setattr("inspect.getsourcelines", None)
|
2020-11-25 20:42:47 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-04-16 06:31:22 +08:00
|
|
|
def raise_error(obj):
|
2020-03-27 23:40:23 +08:00
|
|
|
raise OSError('source code not available')
|
2015-04-16 06:31:22 +08:00
|
|
|
|
|
|
|
import inspect
|
|
|
|
inspect.getsourcelines = raise_error
|
|
|
|
|
|
|
|
def test_foo(invalid_fixture):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
res.stdout.fnmatch_lines(
|
|
|
|
["*source code not available*", "E*fixture 'invalid_fixture' not found"]
|
|
|
|
)
|
2015-04-16 06:31:22 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_plugins_given_as_strings(
|
|
|
|
self, pytester: Pytester, monkeypatch, _sys_snapshot
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that str values passed to main() as `plugins` arg are
|
|
|
|
interpreted as module names to be imported and registered (#855)."""
|
2015-07-19 01:40:00 +08:00
|
|
|
with pytest.raises(ImportError) as excinfo:
|
2020-11-25 20:42:47 +08:00
|
|
|
pytest.main([str(pytester.path)], plugins=["invalid.module"])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "invalid" in str(excinfo.value)
|
2015-07-19 01:40:00 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
p = pytester.path.joinpath("test_test_plugins_given_as_strings.py")
|
|
|
|
p.write_text("def test_foo(): pass")
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("myplugin")
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setitem(sys.modules, "myplugin", mod)
|
2020-11-25 20:42:47 +08:00
|
|
|
assert pytest.main(args=[str(pytester.path)], plugins=["myplugin"]) == 0
|
2015-07-19 01:40:00 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_parametrized_with_bytes_regex(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-03-06 01:23:13 +08:00
|
|
|
import re
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('r', [re.compile(b'foo')])
|
|
|
|
def test_stuff(r):
|
|
|
|
pass
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
res.stdout.fnmatch_lines(["*1 passed*"])
|
2016-03-06 01:23:13 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_parametrized_with_null_bytes(self, pytester: Pytester) -> None:
|
2017-11-29 05:45:13 +08:00
|
|
|
"""Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)"""
|
2020-11-25 20:42:47 +08:00
|
|
|
p = pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2017-08-02 07:49:07 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-06-05 08:48:06 +08:00
|
|
|
@pytest.mark.parametrize("data", [b"\\x00", "\\x00", 'ação'])
|
2017-08-02 07:49:07 +08:00
|
|
|
def test_foo(data):
|
|
|
|
assert data
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
res = pytester.runpytest(p)
|
2017-11-29 05:45:13 +08:00
|
|
|
res.assert_outcomes(passed=3)
|
2017-08-02 07:49:07 +08:00
|
|
|
|
2013-02-14 19:21:42 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestInvocationVariants:
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_earlyinit(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
assert hasattr(pytest, 'mark')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpython(p)
|
2010-11-06 06:37:31 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pydoc(self, pytester: Pytester) -> None:
|
2021-10-16 06:57:12 +08:00
|
|
|
result = pytester.runpython_c("import pytest;help(pytest)")
|
2010-11-13 18:10:45 +08:00
|
|
|
assert result.ret == 0
|
2021-10-16 06:57:12 +08:00
|
|
|
s = result.stdout.str()
|
|
|
|
assert "MarkGenerator" in s
|
2010-11-13 18:10:45 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_import_star_pytest(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-13 18:10:45 +08:00
|
|
|
from pytest import *
|
|
|
|
#Item
|
|
|
|
#File
|
|
|
|
main
|
|
|
|
skip
|
|
|
|
xfail
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpython(p)
|
2010-11-06 06:37:31 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_double_pytestcmdline(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
run="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.main()
|
|
|
|
pytest.main()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_hello():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpython(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"])
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_python_minus_m_invocation_ok(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile("def test_hello(): pass")
|
|
|
|
res = pytester.run(sys.executable, "-m", "pytest", str(p1))
|
2010-11-06 06:37:31 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_python_minus_m_invocation_fail(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile("def test_fail(): 0/0")
|
|
|
|
res = pytester.run(sys.executable, "-m", "pytest", str(p1))
|
2010-11-06 06:37:31 +08:00
|
|
|
assert res.ret == 1
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_python_pytest_package(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile("def test_pass(): pass")
|
|
|
|
res = pytester.run(sys.executable, "-m", "pytest", str(p1))
|
2010-11-06 06:37:31 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
res.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-06-21 05:34:41 +08:00
|
|
|
def test_invoke_with_invalid_type(self) -> None:
|
2018-12-15 01:19:48 +08:00
|
|
|
with pytest.raises(
|
main: `args` must be a list, not tuple (#6791)
Passing in a tuple crashes in `_prepareconfig`:
def test_invoke_with_tuple(self):
> pytest.main(("-h",))
src/_pytest/config/__init__.py:82: in main
config = _prepareconfig(args, plugins)
src/_pytest/config/__init__.py:229: in _prepareconfig
return pluginmanager.hook.pytest_cmdline_parse(
…
src/_pytest/helpconfig.py:98: in pytest_cmdline_parse
config = outcome.get_result() # type: Config
src/_pytest/config/__init__.py:808: in pytest_cmdline_parse
self.parse(args)
src/_pytest/config/__init__.py:1017: in parse
self._preparse(args, addopts=addopts)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def _preparse(self, args: List[str], addopts: bool = True) -> None:
…
if addopts:
ini_addopts = self.getini("addopts")
if ini_addopts:
> args[:] = self._validate_args(ini_addopts, "via addopts config") + args
E TypeError: can only concatenate list (not "tuple") to list
addopts = True
args = ('-h',)
env_addopts = ''
ini_addopts = ['-rfEX', …]
src/_pytest/config/__init__.py:956: TypeError: can only concatenate list (not "tuple") to list
Might be worth handling (converting it to a list for example), but it
was documented to be a list to begin with when removing support for
strings (a7e401656).
2020-02-22 20:16:46 +08:00
|
|
|
TypeError, match="expected to be a list of strings, got: '-h'"
|
2018-12-15 01:19:48 +08:00
|
|
|
):
|
2020-06-21 05:34:41 +08:00
|
|
|
pytest.main("-h") # type: ignore[arg-type]
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_invoke_with_path(self, pytester: Pytester, capsys) -> None:
|
|
|
|
retcode = pytest.main([str(pytester.path)])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert retcode == ExitCode.NO_TESTS_COLLECTED
|
2010-11-06 06:37:31 +08:00
|
|
|
out, err = capsys.readouterr()
|
2011-09-12 14:57:35 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_invoke_plugin_api(self, capsys) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class MyPlugin:
|
2010-11-06 06:37:31 +08:00
|
|
|
def pytest_addoption(self, parser):
|
|
|
|
parser.addoption("--myopt")
|
2010-11-18 21:56:16 +08:00
|
|
|
|
2013-09-29 04:23:00 +08:00
|
|
|
pytest.main(["-h"], plugins=[MyPlugin()])
|
2010-11-06 06:37:31 +08:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "--myopt" in out
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pyargs_importerror(self, pytester: Pytester, monkeypatch) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
|
2020-11-25 20:42:47 +08:00
|
|
|
path = pytester.mkpydir("tpkg")
|
|
|
|
path.joinpath("test_hello.py").write_text("raise ImportError")
|
2011-09-05 23:38:22 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)
|
2011-09-05 23:38:22 +08:00
|
|
|
assert result.ret != 0
|
2016-06-08 21:17:55 +08:00
|
|
|
|
2019-10-27 23:02:37 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected*0*items*/*1*error"])
|
2011-09-05 23:38:22 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pyargs_only_imported_once(self, pytester: Pytester) -> None:
|
|
|
|
pkg = pytester.mkpydir("foo")
|
|
|
|
pkg.joinpath("test_foo.py").write_text(
|
|
|
|
"print('hello from test_foo')\ndef test(): pass"
|
|
|
|
)
|
|
|
|
pkg.joinpath("conftest.py").write_text(
|
2019-06-23 06:02:32 +08:00
|
|
|
"def pytest_configure(config): print('configuring')"
|
|
|
|
)
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(
|
|
|
|
"--pyargs", "foo.test_foo", "-s", syspathinsert=True
|
|
|
|
)
|
2019-06-23 06:02:32 +08:00
|
|
|
# should only import once
|
|
|
|
assert result.outlines.count("hello from test_foo") == 1
|
|
|
|
# should only configure once
|
|
|
|
assert result.outlines.count("configuring") == 1
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None:
|
|
|
|
pytester.path.joinpath("conftest.py").touch()
|
|
|
|
pytester.path.joinpath("t.py").write_text("def test(): pass")
|
|
|
|
result = pytester.runpytest("--pyargs", "t.py")
|
2019-06-27 23:32:32 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_cmdline_python_package(self, pytester: Pytester, monkeypatch) -> None:
|
2017-03-22 09:32:41 +08:00
|
|
|
import warnings
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
|
2020-11-25 20:42:47 +08:00
|
|
|
path = pytester.mkpydir("tpkg")
|
|
|
|
path.joinpath("test_hello.py").write_text("def test_hello(): pass")
|
|
|
|
path.joinpath("test_world.py").write_text("def test_world(): pass")
|
|
|
|
result = pytester.runpytest("--pyargs", "tpkg")
|
2010-11-07 05:17:33 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)
|
2010-11-07 05:17:33 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2011-10-27 04:40:08 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
empty_package = pytester.mkpydir("empty_package")
|
2018-10-12 21:15:35 +08:00
|
|
|
monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep)
|
2017-03-22 09:32:41 +08:00
|
|
|
# the path which is not a package raises a warning on pypy;
|
|
|
|
# no idea why only pypy and not normal python warn about it here
|
|
|
|
with warnings.catch_warnings():
|
2018-05-23 22:48:46 +08:00
|
|
|
warnings.simplefilter("ignore", ImportWarning)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--pyargs", ".")
|
2011-10-27 04:40:08 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2011-10-27 04:40:08 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
monkeypatch.setenv("PYTHONPATH", str(pytester), prepend=os.pathsep)
|
|
|
|
result = pytester.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True)
|
2011-01-28 04:11:21 +08:00
|
|
|
assert result.ret != 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*not*found*test_missing*"])
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_cmdline_python_namespace_package(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test --pyargs option with namespace packages (#1567).
|
2019-03-22 20:22:57 +08:00
|
|
|
|
|
|
|
Ref: https://packaging.python.org/guides/packaging-namespace-packages/
|
2016-06-08 21:17:55 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
2016-06-08 21:17:55 +08:00
|
|
|
|
|
|
|
search_path = []
|
|
|
|
for dirname in "hello", "world":
|
2020-11-25 20:42:47 +08:00
|
|
|
d = pytester.mkdir(dirname)
|
2016-06-08 21:17:55 +08:00
|
|
|
search_path.append(d)
|
2020-11-25 20:42:47 +08:00
|
|
|
ns = d.joinpath("ns_pkg")
|
|
|
|
ns.mkdir()
|
|
|
|
ns.joinpath("__init__.py").write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
"__import__('pkg_resources').declare_namespace(__name__)"
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
lib = ns.joinpath(dirname)
|
|
|
|
lib.mkdir()
|
|
|
|
lib.joinpath("__init__.py").touch()
|
|
|
|
lib.joinpath(f"test_{dirname}.py").write_text(
|
2020-10-03 04:16:22 +08:00
|
|
|
f"def test_{dirname}(): pass\ndef test_other():pass"
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-08 21:17:55 +08:00
|
|
|
|
|
|
|
# The structure of the test directory is now:
|
|
|
|
# .
|
|
|
|
# ├── hello
|
2017-12-09 06:48:58 +08:00
|
|
|
# │ └── ns_pkg
|
|
|
|
# │ ├── __init__.py
|
|
|
|
# │ └── hello
|
|
|
|
# │ ├── __init__.py
|
|
|
|
# │ └── test_hello.py
|
2016-06-08 21:17:55 +08:00
|
|
|
# └── world
|
|
|
|
# └── ns_pkg
|
|
|
|
# ├── __init__.py
|
|
|
|
# └── world
|
|
|
|
# ├── __init__.py
|
|
|
|
# └── test_world.py
|
|
|
|
|
2018-10-12 21:15:35 +08:00
|
|
|
# NOTE: the different/reversed ordering is intentional here.
|
|
|
|
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))
|
2016-06-08 21:17:55 +08:00
|
|
|
for p in search_path:
|
|
|
|
monkeypatch.syspath_prepend(p)
|
|
|
|
|
|
|
|
# mixed module and filenames:
|
2018-10-12 21:15:35 +08:00
|
|
|
monkeypatch.chdir("world")
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")
|
2016-06-08 21:17:55 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2018-11-09 03:35:53 +08:00
|
|
|
"test_hello.py::test_hello*PASSED*",
|
|
|
|
"test_hello.py::test_other*PASSED*",
|
|
|
|
"ns_pkg/world/test_world.py::test_world*PASSED*",
|
|
|
|
"ns_pkg/world/test_world.py::test_other*PASSED*",
|
|
|
|
"*4 passed in*",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2016-06-08 21:17:55 +08:00
|
|
|
|
|
|
|
# specify tests within a module
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.chdir()
|
|
|
|
result = pytester.runpytest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"--pyargs", "-v", "ns_pkg.world.test_world::test_other"
|
|
|
|
)
|
2016-06-08 21:17:55 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_world.py::test_other*PASSED*", "*1 passed*"]
|
|
|
|
)
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_invoke_test_and_doctestmodules(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-22 07:55:45 +08:00
|
|
|
"""
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(str(p) + "::test", "--doctest-modules")
|
2018-08-22 07:55:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_cmdline_python_package_symlink(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2017-12-07 05:55:04 +08:00
|
|
|
"""
|
2020-06-08 21:56:40 +08:00
|
|
|
--pyargs with packages with path containing symlink can have conftest.py in
|
|
|
|
their package (#2985)
|
2017-12-07 05:55:04 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
2017-12-07 05:55:04 +08:00
|
|
|
|
|
|
|
dirname = "lib"
|
2020-11-25 20:42:47 +08:00
|
|
|
d = pytester.mkdir(dirname)
|
|
|
|
foo = d.joinpath("foo")
|
|
|
|
foo.mkdir()
|
|
|
|
foo.joinpath("__init__.py").touch()
|
|
|
|
lib = foo.joinpath("bar")
|
|
|
|
lib.mkdir()
|
|
|
|
lib.joinpath("__init__.py").touch()
|
|
|
|
lib.joinpath("test_bar.py").write_text(
|
2018-10-11 01:28:31 +08:00
|
|
|
"def test_bar(): pass\ndef test_other(a_fixture):pass"
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
lib.joinpath("conftest.py").write_text(
|
2018-10-11 01:28:31 +08:00
|
|
|
"import pytest\n@pytest.fixture\ndef a_fixture():pass"
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-12-07 05:55:04 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
d_local = pytester.mkdir("symlink_root")
|
2020-06-08 21:56:40 +08:00
|
|
|
symlink_location = d_local / "lib"
|
|
|
|
symlink_or_skip(d, symlink_location, target_is_directory=True)
|
2017-12-07 05:55:04 +08:00
|
|
|
|
|
|
|
# The structure of the test directory is now:
|
|
|
|
# .
|
2020-06-08 21:56:40 +08:00
|
|
|
# ├── symlink_root
|
2017-12-12 16:26:40 +08:00
|
|
|
# │ └── lib -> ../lib
|
2017-12-07 05:55:04 +08:00
|
|
|
# └── lib
|
|
|
|
# └── foo
|
|
|
|
# ├── __init__.py
|
|
|
|
# └── bar
|
|
|
|
# ├── __init__.py
|
|
|
|
# ├── conftest.py
|
2017-12-12 16:26:40 +08:00
|
|
|
# └── test_bar.py
|
2017-12-07 05:55:04 +08:00
|
|
|
|
2018-10-12 21:15:35 +08:00
|
|
|
# NOTE: the different/reversed ordering is intentional here.
|
2020-06-08 21:56:40 +08:00
|
|
|
search_path = ["lib", os.path.join("symlink_root", "lib")]
|
2018-10-12 21:15:35 +08:00
|
|
|
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))
|
2017-12-07 05:55:04 +08:00
|
|
|
for p in search_path:
|
|
|
|
monkeypatch.syspath_prepend(p)
|
|
|
|
|
|
|
|
# module picked up in symlink-ed directory:
|
2020-06-08 21:56:40 +08:00
|
|
|
# It picks up symlink_root/lib/foo/bar (symlink) via sys.path.
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest("--pyargs", "-v", "foo.bar")
|
|
|
|
pytester.chdir()
|
2017-12-07 05:55:04 +08:00
|
|
|
assert result.ret == 0
|
2020-06-08 21:56:40 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"symlink_root/lib/foo/bar/test_bar.py::test_bar PASSED*",
|
|
|
|
"symlink_root/lib/foo/bar/test_bar.py::test_other PASSED*",
|
|
|
|
"*2 passed*",
|
|
|
|
]
|
|
|
|
)
|
2017-12-07 05:55:04 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_cmdline_python_package_not_exists(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--pyargs", "tpkgwhatv")
|
2010-11-07 05:17:33 +08:00
|
|
|
assert result.ret
|
2020-08-15 21:31:17 +08:00
|
|
|
result.stderr.fnmatch_lines(["ERROR*module*or*package*not*found*"])
|
2010-11-07 06:45:48 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail(reason="decide: feature or bug")
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_noclass_discovery_if_not_testcase(self, pytester: Pytester) -> None:
|
|
|
|
testpath = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-07 06:45:48 +08:00
|
|
|
import unittest
|
|
|
|
class TestHello(object):
|
|
|
|
def test_hello(self):
|
|
|
|
assert self.attr
|
|
|
|
|
2010-11-07 08:10:15 +08:00
|
|
|
class RealTest(unittest.TestCase, TestHello):
|
2010-11-07 06:45:48 +08:00
|
|
|
attr = 42
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
reprec = pytester.inline_run(testpath)
|
2010-11-07 06:45:48 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2010-11-18 01:24:28 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_doctest_id(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makefile(
|
2018-05-23 22:48:46 +08:00
|
|
|
".txt",
|
|
|
|
"""
|
2010-11-18 01:24:28 +08:00
|
|
|
>>> x=3
|
|
|
|
>>> x
|
|
|
|
4
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2019-10-23 06:21:29 +08:00
|
|
|
testid = "test_doctest_id.txt::test_doctest_id.txt"
|
|
|
|
expected_lines = [
|
|
|
|
"*= FAILURES =*",
|
|
|
|
"*_ ?doctest? test_doctest_id.txt _*",
|
|
|
|
"FAILED test_doctest_id.txt::test_doctest_id.txt",
|
|
|
|
"*= 1 failed in*",
|
|
|
|
]
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(testid, "-rf", "--tb=short")
|
2019-10-23 06:21:29 +08:00
|
|
|
result.stdout.fnmatch_lines(expected_lines)
|
|
|
|
|
|
|
|
# Ensure that re-running it will still handle it as
|
|
|
|
# doctest.DocTestFailure, which was not the case before when
|
|
|
|
# re-importing doctest, but not creating a new RUNNER_CLASS.
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest(testid, "-rf", "--tb=short")
|
2019-10-23 06:21:29 +08:00
|
|
|
result.stdout.fnmatch_lines(expected_lines)
|
2010-11-18 01:24:28 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_core_backward_compatibility(self) -> None:
|
2015-06-18 05:57:45 +08:00
|
|
|
"""Test backward compatibility for get_plugin_manager function. See #787."""
|
|
|
|
import _pytest.config
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2018-06-26 21:35:27 +08:00
|
|
|
assert (
|
|
|
|
type(_pytest.config.get_plugin_manager())
|
|
|
|
is _pytest.config.PytestPluginManager
|
|
|
|
)
|
2015-06-18 05:57:45 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_has_plugin(self, request) -> None:
|
2015-08-10 06:30:49 +08:00
|
|
|
"""Test hasplugin function of the plugin manager (#932)."""
|
2018-05-23 22:48:46 +08:00
|
|
|
assert request.config.pluginmanager.hasplugin("python")
|
2015-08-10 06:30:49 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestDurations:
|
2011-11-09 02:12:16 +08:00
|
|
|
source = """
|
2020-05-23 03:10:51 +08:00
|
|
|
from _pytest import timing
|
2013-03-28 17:21:03 +08:00
|
|
|
def test_something():
|
|
|
|
pass
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_2():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(0.010)
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_1():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(0.002)
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_3():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(0.020)
|
2011-11-09 02:12:16 +08:00
|
|
|
"""
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_calls(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=10")
|
2011-11-09 02:12:16 +08:00
|
|
|
assert result.ret == 0
|
2020-03-29 20:20:09 +08:00
|
|
|
|
2020-04-07 04:05:15 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
["*durations*", "*call*test_3*", "*call*test_2*"]
|
|
|
|
)
|
2020-03-29 20:20:09 +08:00
|
|
|
|
2018-10-14 05:36:20 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2020-04-07 04:05:15 +08:00
|
|
|
["(8 durations < 0.005s hidden. Use -vv to show these durations.)"]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2011-11-09 02:12:16 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_calls_show_2(self, pytester: Pytester, mock_timing) -> None:
|
2020-05-23 03:10:51 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=2")
|
2011-11-09 02:12:16 +08:00
|
|
|
assert result.ret == 0
|
2020-04-07 04:05:15 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
lines = result.stdout.get_lines_after("*slowest*durations*")
|
2019-02-12 20:39:25 +08:00
|
|
|
assert "4 passed" in lines[2]
|
2011-11-09 02:12:16 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_calls_showall(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=0")
|
2011-11-09 02:12:16 +08:00
|
|
|
assert result.ret == 0
|
2020-03-29 20:20:09 +08:00
|
|
|
|
2020-04-07 04:05:15 +08:00
|
|
|
tested = "3"
|
2020-03-29 20:20:09 +08:00
|
|
|
for x in tested:
|
2018-10-08 10:19:48 +08:00
|
|
|
for y in ("call",): # 'setup', 'call', 'teardown':
|
|
|
|
for line in result.stdout.lines:
|
|
|
|
if ("test_%s" % x) in line and y in line:
|
|
|
|
break
|
|
|
|
else:
|
2020-10-03 04:16:22 +08:00
|
|
|
raise AssertionError(f"not found {x} {y}")
|
2018-10-08 10:19:48 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_calls_showall_verbose(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=0", "-vv")
|
2018-10-08 10:19:48 +08:00
|
|
|
assert result.ret == 0
|
2020-04-07 04:05:15 +08:00
|
|
|
|
2011-11-09 02:12:16 +08:00
|
|
|
for x in "123":
|
2018-05-23 22:48:46 +08:00
|
|
|
for y in ("call",): # 'setup', 'call', 'teardown':
|
2011-11-09 02:12:16 +08:00
|
|
|
for line in result.stdout.lines:
|
|
|
|
if ("test_%s" % x) in line and y in line:
|
|
|
|
break
|
|
|
|
else:
|
2020-10-03 04:16:22 +08:00
|
|
|
raise AssertionError(f"not found {x} {y}")
|
2011-11-09 02:12:16 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_with_deselected(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=2", "-k test_3")
|
2011-11-09 02:12:16 +08:00
|
|
|
assert result.ret == 0
|
2020-04-07 04:05:15 +08:00
|
|
|
|
2020-03-29 20:20:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["*durations*", "*call*test_3*"])
|
2011-11-09 02:12:16 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_with_failing_collection(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
pytester.makepyfile(test_collecterror="""xyz""")
|
|
|
|
result = pytester.runpytest_inprocess("--durations=2", "-k test_1")
|
2016-06-20 21:05:50 +08:00
|
|
|
assert result.ret == 2
|
2020-04-07 04:05:15 +08:00
|
|
|
|
2019-10-27 23:02:37 +08:00
|
|
|
result.stdout.fnmatch_lines(["*Interrupted: 1 error during collection*"])
|
2016-06-20 21:05:50 +08:00
|
|
|
# Collection errors abort test execution, therefore no duration is
|
|
|
|
# output
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*duration*")
|
2011-11-09 03:00:25 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_with_not(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("-k not 1")
|
2016-04-01 13:20:22 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2011-11-09 02:12:16 +08:00
|
|
|
|
2020-04-07 04:05:15 +08:00
|
|
|
class TestDurationsWithFixture:
|
2011-11-09 02:12:16 +08:00
|
|
|
source = """
|
2018-10-08 10:19:48 +08:00
|
|
|
import pytest
|
2020-05-23 03:10:51 +08:00
|
|
|
from _pytest import timing
|
2018-10-08 10:19:48 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def setup_fixt():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(2)
|
2018-10-08 10:19:48 +08:00
|
|
|
|
|
|
|
def test_1(setup_fixt):
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(5)
|
2011-11-09 02:12:16 +08:00
|
|
|
"""
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_setup_function(self, pytester: Pytester, mock_timing) -> None:
|
|
|
|
pytester.makepyfile(self.source)
|
|
|
|
result = pytester.runpytest_inprocess("--durations=10")
|
2011-11-09 02:12:16 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
"""
|
2013-09-30 22:09:26 +08:00
|
|
|
*durations*
|
2020-05-23 03:10:51 +08:00
|
|
|
5.00s call *test_1*
|
|
|
|
2.00s setup *test_1*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-07-12 08:06:35 +08:00
|
|
|
|
2016-08-23 04:54:17 +08:00
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_zipimport_hook(pytester: Pytester) -> None:
|
2016-08-23 04:54:17 +08:00
|
|
|
"""Test package loader is being used correctly (see #1837)."""
|
2018-05-23 22:48:46 +08:00
|
|
|
zipapp = pytest.importorskip("zipapp")
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.path.joinpath("app").mkdir()
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"app/foo.py": """
|
2016-08-23 04:54:17 +08:00
|
|
|
import pytest
|
|
|
|
def main():
|
2019-06-25 11:51:33 +08:00
|
|
|
pytest.main(['--pyargs', 'foo'])
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
}
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
target = pytester.path.joinpath("foo.zip")
|
|
|
|
zipapp.create_archive(
|
|
|
|
str(pytester.path.joinpath("app")), str(target), main="foo:main"
|
|
|
|
)
|
|
|
|
result = pytester.runpython(target)
|
2016-08-23 04:54:17 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*not found*foo*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR>*")
|
2017-04-06 18:54:47 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_import_plugin_unicode_name(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(myplugin="")
|
|
|
|
pytester.makepyfile("def test(): pass")
|
|
|
|
pytester.makeconftest("pytest_plugins = ['myplugin']")
|
|
|
|
r = pytester.runpytest()
|
2017-04-06 18:54:47 +08:00
|
|
|
assert r.ret == 0
|
2017-03-08 08:02:52 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pytest_plugins_as_module(pytester: Pytester) -> None:
|
2019-02-06 10:02:39 +08:00
|
|
|
"""Do not raise an error if pytest_plugins attribute is a module (#3899)"""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2019-02-06 10:02:39 +08:00
|
|
|
**{
|
|
|
|
"__init__.py": "",
|
|
|
|
"pytest_plugins.py": "",
|
|
|
|
"conftest.py": "from . import pytest_plugins",
|
|
|
|
"test_foo.py": "def test(): pass",
|
|
|
|
}
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
2019-02-06 10:02:39 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_deferred_hook_checking(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Check hooks as late as possible (#1821)."""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"plugin.py": """
|
2018-01-25 04:23:42 +08:00
|
|
|
class Hooks(object):
|
2017-03-08 08:02:52 +08:00
|
|
|
def pytest_my_hook(self, config):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.pluginmanager.add_hookspecs(Hooks)
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"conftest.py": """
|
2017-03-08 08:02:52 +08:00
|
|
|
pytest_plugins = ['plugin']
|
|
|
|
def pytest_my_hook(config):
|
|
|
|
return 40
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_foo.py": """
|
2017-03-08 08:02:52 +08:00
|
|
|
def test(request):
|
|
|
|
assert request.config.hook.pytest_my_hook(config=request.config) == [40]
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed *"])
|
2017-12-14 05:37:45 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_fixture_values_leak(pytester: Pytester) -> None:
|
2019-06-25 23:23:14 +08:00
|
|
|
"""Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected
|
|
|
|
life-times (#2981).
|
|
|
|
"""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-25 23:23:14 +08:00
|
|
|
"""
|
|
|
|
import attr
|
|
|
|
import gc
|
|
|
|
import pytest
|
|
|
|
import weakref
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class SomeObj(object):
|
|
|
|
name = attr.ib()
|
|
|
|
|
|
|
|
fix_of_test1_ref = None
|
|
|
|
session_ref = None
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def session_fix():
|
|
|
|
global session_ref
|
|
|
|
obj = SomeObj(name='session-fixture')
|
|
|
|
session_ref = weakref.ref(obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(session_fix):
|
|
|
|
global fix_of_test1_ref
|
|
|
|
obj = SomeObj(name='local-fixture')
|
|
|
|
fix_of_test1_ref = weakref.ref(obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def test1(fix):
|
|
|
|
assert fix_of_test1_ref() is fix
|
|
|
|
|
|
|
|
def test2():
|
|
|
|
gc.collect()
|
|
|
|
# fixture "fix" created during test1 must have been destroyed by now
|
|
|
|
assert fix_of_test1_ref() is None
|
|
|
|
"""
|
|
|
|
)
|
2019-06-25 23:46:56 +08:00
|
|
|
# Running on subprocess does not activate the HookRecorder
|
|
|
|
# which holds itself a reference to objects in case of the
|
|
|
|
# pytest_assert_reprcompare hook
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-06-25 23:23:14 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed *"])
|
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_fixture_order_respects_scope(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Ensure that fixtures are created according to scope order (#2405)."""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-03-14 08:08:21 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def clean_data():
|
|
|
|
data.clear()
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def add_data():
|
|
|
|
data.update(value=True)
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('clean_data')
|
|
|
|
def test_value():
|
|
|
|
assert data.get('value')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2018-03-14 08:08:21 +08:00
|
|
|
assert result.ret == 0
|
2018-04-10 07:43:14 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_frame_leak_on_failing_test(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Pytest would leak garbage referencing the frames of tests that failed
|
|
|
|
that could never be reclaimed (#2798).
|
2018-04-10 07:43:14 +08:00
|
|
|
|
|
|
|
Unfortunately it was not possible to remove the actual circles because most of them
|
|
|
|
are made of traceback objects which cannot be weakly referenced. Those objects at least
|
|
|
|
can be eventually claimed by the garbage collector.
|
|
|
|
"""
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-04-10 07:43:14 +08:00
|
|
|
import gc
|
|
|
|
import weakref
|
|
|
|
|
|
|
|
class Obj:
|
|
|
|
pass
|
|
|
|
|
|
|
|
ref = None
|
|
|
|
|
|
|
|
def test1():
|
|
|
|
obj = Obj()
|
|
|
|
global ref
|
|
|
|
ref = weakref.ref(obj)
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test2():
|
|
|
|
gc.collect()
|
|
|
|
assert ref() is None
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"])
|
2018-08-05 02:14:00 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_fixture_mock_integration(pytester: Pytester) -> None:
|
2018-08-05 02:14:00 +08:00
|
|
|
"""Test that decorators applied to fixture are left working (#3774)"""
|
2020-11-25 20:42:47 +08:00
|
|
|
p = pytester.copy_example("acceptance/fixture_mock_integration.py")
|
|
|
|
result = pytester.runpytest(p)
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2018-09-03 04:42:47 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_usage_error_code(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("-unknown-option-")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2019-02-26 07:06:26 +08:00
|
|
|
|
|
|
|
|
2021-05-04 20:27:21 +08:00
|
|
|
@pytest.mark.filterwarnings("default::pytest.PytestUnhandledCoroutineWarning")
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_warn_on_async_function(pytester: Pytester) -> None:
|
2020-07-02 01:08:56 +08:00
|
|
|
# In the below we .close() the coroutine only to avoid
|
|
|
|
# "RuntimeWarning: coroutine 'test_2' was never awaited"
|
|
|
|
# which messes with other tests.
|
2020-11-25 20:42:47 +08:00
|
|
|
pytester.makepyfile(
|
2019-02-26 07:06:26 +08:00
|
|
|
test_async="""
|
|
|
|
async def test_1():
|
|
|
|
pass
|
|
|
|
async def test_2():
|
|
|
|
pass
|
2019-08-15 19:03:06 +08:00
|
|
|
def test_3():
|
2020-07-02 01:08:56 +08:00
|
|
|
coro = test_2()
|
|
|
|
coro.close()
|
|
|
|
return coro
|
2019-02-26 07:06:26 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2019-02-26 07:06:26 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_async.py::test_1",
|
|
|
|
"test_async.py::test_2",
|
2019-08-15 19:03:06 +08:00
|
|
|
"test_async.py::test_3",
|
2019-08-13 04:58:10 +08:00
|
|
|
"*async def functions are not natively supported*",
|
2019-08-15 19:03:06 +08:00
|
|
|
"*3 skipped, 3 warnings in*",
|
2019-02-26 07:06:26 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
# ensure our warning message appears only once
|
|
|
|
assert (
|
2019-08-13 04:58:10 +08:00
|
|
|
result.stdout.str().count("async def functions are not natively supported") == 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-04 20:27:21 +08:00
|
|
|
@pytest.mark.filterwarnings("default::pytest.PytestUnhandledCoroutineWarning")
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_warn_on_async_gen_function(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-08-13 04:58:10 +08:00
|
|
|
test_async="""
|
|
|
|
async def test_1():
|
|
|
|
yield
|
|
|
|
async def test_2():
|
|
|
|
yield
|
2019-08-15 19:03:06 +08:00
|
|
|
def test_3():
|
|
|
|
return test_2()
|
2019-02-26 07:06:26 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest()
|
2019-02-26 07:06:26 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_async.py::test_1",
|
|
|
|
"test_async.py::test_2",
|
2019-08-15 19:03:06 +08:00
|
|
|
"test_async.py::test_3",
|
2019-08-13 04:58:10 +08:00
|
|
|
"*async def functions are not natively supported*",
|
2019-08-15 19:03:06 +08:00
|
|
|
"*3 skipped, 3 warnings in*",
|
2019-02-26 07:06:26 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
# ensure our warning message appears only once
|
|
|
|
assert (
|
2019-08-13 04:58:10 +08:00
|
|
|
result.stdout.str().count("async def functions are not natively supported") == 1
|
2019-02-26 07:06:26 +08:00
|
|
|
)
|
2019-10-06 21:34:23 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_pdb_can_be_rewritten(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-10-06 21:34:23 +08:00
|
|
|
**{
|
|
|
|
"conftest.py": """
|
|
|
|
import pytest
|
|
|
|
pytest.register_assert_rewrite("pdb")
|
|
|
|
""",
|
|
|
|
"__init__.py": "",
|
|
|
|
"pdb.py": """
|
|
|
|
def check():
|
|
|
|
assert 1 == 2
|
|
|
|
""",
|
|
|
|
"test_pdb.py": """
|
|
|
|
def test():
|
|
|
|
import pdb
|
|
|
|
assert pdb.check()
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# Disable debugging plugin itself to avoid:
|
|
|
|
# > INTERNALERROR> AttributeError: module 'pdb' has no attribute 'set_trace'
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest_subprocess("-p", "no:debugging", "-vv")
|
2019-10-06 21:34:23 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
" def check():",
|
|
|
|
"> assert 1 == 2",
|
|
|
|
"E assert 1 == 2",
|
|
|
|
"",
|
|
|
|
"pdb.py:2: AssertionError",
|
|
|
|
"*= 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert result.ret == 1
|
2019-12-04 06:15:13 +08:00
|
|
|
|
|
|
|
|
2020-11-25 20:42:47 +08:00
|
|
|
def test_tee_stdio_captures_and_live_prints(pytester: Pytester) -> None:
|
|
|
|
testpath = pytester.makepyfile(
|
2019-12-04 06:15:13 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def test_simple():
|
|
|
|
print ("@this is stdout@")
|
|
|
|
print ("@this is stderr@", file=sys.stderr)
|
|
|
|
"""
|
|
|
|
)
|
2020-11-25 20:42:47 +08:00
|
|
|
result = pytester.runpytest_subprocess(
|
2020-01-16 15:14:46 +08:00
|
|
|
testpath,
|
|
|
|
"--capture=tee-sys",
|
|
|
|
"--junitxml=output.xml",
|
|
|
|
"-o",
|
|
|
|
"junit_logging=all",
|
2019-12-04 06:15:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# ensure stdout/stderr were 'live printed'
|
|
|
|
result.stdout.fnmatch_lines(["*@this is stdout@*"])
|
|
|
|
result.stderr.fnmatch_lines(["*@this is stderr@*"])
|
|
|
|
|
|
|
|
# now ensure the output is in the junitxml
|
2020-11-25 20:42:47 +08:00
|
|
|
with open(pytester.path.joinpath("output.xml")) as f:
|
2019-12-04 06:15:13 +08:00
|
|
|
fullXml = f.read()
|
2020-01-16 15:14:46 +08:00
|
|
|
assert "@this is stdout@\n" in fullXml
|
|
|
|
assert "@this is stderr@\n" in fullXml
|
2020-05-08 02:20:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform == "win32",
|
|
|
|
reason="Windows raises `OSError: [Errno 22] Invalid argument` instead",
|
|
|
|
)
|
2020-10-12 23:13:06 +08:00
|
|
|
def test_no_brokenpipeerror_message(pytester: Pytester) -> None:
|
2021-12-27 20:23:15 +08:00
|
|
|
"""Ensure that the broken pipe error message is suppressed.
|
2020-05-08 02:20:09 +08:00
|
|
|
|
|
|
|
In some Python versions, it reaches sys.unraisablehook, in others
|
|
|
|
a BrokenPipeError exception is propagated, but either way it prints
|
|
|
|
to stderr on shutdown, so checking nothing is printed is enough.
|
|
|
|
"""
|
2020-10-12 23:13:06 +08:00
|
|
|
popen = pytester.popen((*pytester._getpytestargs(), "--help"))
|
2020-05-08 02:20:09 +08:00
|
|
|
popen.stdout.close()
|
|
|
|
ret = popen.wait()
|
|
|
|
assert popen.stderr.read() == b""
|
|
|
|
assert ret == 1
|
2020-11-20 23:54:03 +08:00
|
|
|
|
|
|
|
# Cleanup.
|
|
|
|
popen.stderr.close()
|