2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-10-26 05:45:10 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2024-02-01 04:12:33 +08:00
|
|
|
import pytest
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_version_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
|
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
|
|
|
|
result = pytester.runpytest("--version", "--version")
|
2009-08-19 21:45:01 +08:00
|
|
|
assert result.ret == 0
|
2021-01-17 19:43:36 +08:00
|
|
|
result.stdout.fnmatch_lines([f"*pytest*{pytest.__version__}*imported from*"])
|
2015-05-06 03:53:04 +08:00
|
|
|
if pytestconfig.pluginmanager.list_plugin_distinfo():
|
2021-01-17 19:43:36 +08:00
|
|
|
result.stdout.fnmatch_lines(["*setuptools registered plugins:", "*at*"])
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_version_less_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
|
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
|
|
|
|
result = pytester.runpytest("--version")
|
2020-05-23 22:27:58 +08:00
|
|
|
assert result.ret == 0
|
2021-01-17 19:43:36 +08:00
|
|
|
result.stdout.fnmatch_lines([f"pytest {pytest.__version__}"])
|
2020-05-23 22:27:58 +08:00
|
|
|
|
|
|
|
|
2021-06-14 19:28:45 +08:00
|
|
|
def test_versions():
|
|
|
|
"""Regression check for the public version attributes in pytest."""
|
|
|
|
assert isinstance(pytest.__version__, str)
|
|
|
|
assert isinstance(pytest.version_tuple, tuple)
|
|
|
|
|
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_help(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--help")
|
2009-08-19 21:45:01 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2022-06-01 03:32:51 +08:00
|
|
|
-m MARKEXPR Only run tests matching given mark expression. For
|
|
|
|
example: -m 'mark1 and not mark2'.
|
|
|
|
Reporting:
|
2020-02-25 23:05:17 +08:00
|
|
|
--durations=N *
|
2022-06-01 03:32:51 +08:00
|
|
|
-V, --version Display pytest version and information about plugins.
|
2020-12-27 22:55:21 +08:00
|
|
|
When given twice, also display information about
|
|
|
|
plugins.
|
2012-09-22 17:44:56 +08:00
|
|
|
*setup.cfg*
|
|
|
|
*minversion*
|
2016-06-21 22:16:57 +08:00
|
|
|
*to see*markers*pytest --markers*
|
|
|
|
*to see*fixtures*pytest --fixtures*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_none_help_param_raises_exception(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that a None help param raises a TypeError."""
|
2020-10-26 05:45:10 +08:00
|
|
|
pytester.makeconftest(
|
2020-07-29 15:48:38 +08:00
|
|
|
"""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("test_ini", None, default=True, type="bool")
|
|
|
|
"""
|
|
|
|
)
|
2020-10-26 05:45:10 +08:00
|
|
|
result = pytester.runpytest("--help")
|
2020-07-29 15:48:38 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*TypeError: help argument cannot be None for test_ini*"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_empty_help_param(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that an empty help param is displayed correctly."""
|
2020-10-26 05:45:10 +08:00
|
|
|
pytester.makeconftest(
|
2020-07-29 15:48:38 +08:00
|
|
|
"""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("test_ini", "", default=True, type="bool")
|
|
|
|
"""
|
|
|
|
)
|
2020-10-26 05:45:10 +08:00
|
|
|
result = pytester.runpytest("--help")
|
2020-07-29 15:48:38 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
lines = [
|
|
|
|
" required_plugins (args):",
|
2022-06-01 03:32:51 +08:00
|
|
|
" Plugins that must be present for pytest to run*",
|
2020-07-29 15:48:38 +08:00
|
|
|
" test_ini (bool):*",
|
2022-06-01 03:32:51 +08:00
|
|
|
"Environment variables:",
|
2020-07-29 15:48:38 +08:00
|
|
|
]
|
|
|
|
result.stdout.fnmatch_lines(lines, consecutive=True)
|
|
|
|
|
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_hookvalidation_unknown(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-04-22 17:57:57 +08:00
|
|
|
def pytest_hello(xyz):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-26 05:45:10 +08:00
|
|
|
result = pytester.runpytest()
|
2010-04-22 17:57:57 +08:00
|
|
|
assert result.ret != 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*unknown hook*pytest_hello*"])
|
2010-04-22 17:57:57 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_hookvalidation_optional(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(optionalhook=True)
|
2010-04-22 17:57:57 +08:00
|
|
|
def pytest_hello(xyz):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-26 05:45:10 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2010-04-22 17:57:57 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_traceconfig(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--traceconfig")
|
2021-10-16 06:57:12 +08:00
|
|
|
result.stdout.fnmatch_lines(["*using*pytest*", "*active plugins*"])
|
2011-07-15 01:11:50 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_debug(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest_subprocess("--debug")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2020-10-26 05:45:10 +08:00
|
|
|
p = pytester.path.joinpath("pytestdebug.log")
|
|
|
|
assert "pytest_sessionstart" in p.read_text("utf-8")
|
2011-07-15 01:11:50 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-10-26 05:45:10 +08:00
|
|
|
def test_PYTEST_DEBUG(pytester: Pytester, monkeypatch) -> None:
|
2011-07-15 01:11:50 +08:00
|
|
|
monkeypatch.setenv("PYTEST_DEBUG", "1")
|
2020-10-26 05:45:10 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*pytest_plugin_registered*", "*manager*PluginManager*"]
|
|
|
|
)
|