test_ok1/testing/test_config.py

1113 lines
35 KiB
Python
Raw Normal View History

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
2017-11-29 00:29:52 +08:00
import sys
2017-12-27 11:47:26 +08:00
import textwrap
import _pytest._code
import pytest
2018-06-05 16:20:36 +08:00
from _pytest.config import _iter_rewritable_modules
from _pytest.config.findpaths import determine_setup
from _pytest.config.findpaths import get_common_ancestor
from _pytest.config.findpaths import getcfg
from _pytest.main import EXIT_NOTESTSCOLLECTED
class TestParseIni(object):
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize(
"section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")]
)
def test_getcfg_and_config(self, testdir, tmpdir, section, filename):
sub = tmpdir.mkdir("sub")
sub.chdir()
2018-05-23 22:48:46 +08:00
tmpdir.join(filename).write(
textwrap.dedent(
"""\
[{section}]
name = value
""".format(
2018-05-23 22:48:46 +08:00
section=section
)
)
)
rootdir, inifile, cfg = getcfg([sub])
2018-05-23 22:48:46 +08:00
assert cfg["name"] == "value"
config = testdir.parseconfigure(sub)
2018-05-23 22:48:46 +08:00
assert config.inicfg["name"] == "value"
def test_getcfg_empty_path(self):
"""correctly handle zero length arguments (a la pytest '')"""
2018-05-23 22:48:46 +08:00
getcfg([""])
def test_append_parse_args(self, testdir, tmpdir, monkeypatch):
2018-05-23 22:48:46 +08:00
monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"')
tmpdir.join("pytest.ini").write(
textwrap.dedent(
"""\
[pytest]
addopts = --verbose
2018-05-23 22:48:46 +08:00
"""
)
)
config = testdir.parseconfig(tmpdir)
2018-05-23 22:48:46 +08:00
assert config.option.color == "no"
assert config.option.reportchars == "s"
assert config.option.tbstyle == "short"
assert config.option.verbose
def test_tox_ini_wrong_version(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makefile(
".ini",
tox="""
[pytest]
minversion=9.0
2018-05-23 22:48:46 +08:00
""",
)
result = testdir.runpytest()
assert result.ret != 0
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["*tox.ini:2*requires*9.0*actual*"])
@pytest.mark.parametrize(
"section, name",
[("tool:pytest", "setup.cfg"), ("pytest", "tox.ini"), ("pytest", "pytest.ini")],
)
def test_ini_names(self, testdir, name, section):
2018-05-23 22:48:46 +08:00
testdir.tmpdir.join(name).write(
textwrap.dedent(
"""
[{section}]
minversion = 1.0
2018-05-23 22:48:46 +08:00
""".format(
section=section
)
)
)
config = testdir.parseconfig()
assert config.getini("minversion") == "1.0"
def test_toxini_before_lower_pytestini(self, testdir):
sub = testdir.tmpdir.mkdir("sub")
2018-05-23 22:48:46 +08:00
sub.join("tox.ini").write(
textwrap.dedent(
"""
[pytest]
minversion = 2.0
2018-05-23 22:48:46 +08:00
"""
)
)
testdir.tmpdir.join("pytest.ini").write(
textwrap.dedent(
"""
[pytest]
minversion = 1.5
2018-05-23 22:48:46 +08:00
"""
)
)
config = testdir.parseconfigure(sub)
assert config.getini("minversion") == "2.0"
@pytest.mark.xfail(reason="probably not needed")
2010-11-07 08:10:15 +08:00
def test_confcutdir(self, testdir):
sub = testdir.mkdir("sub")
sub.chdir()
2018-05-23 22:48:46 +08:00
testdir.makeini(
"""
2010-11-07 08:10:15 +08:00
[pytest]
addopts = --qwe
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.inline_run("--confcutdir=.")
2010-11-07 08:10:15 +08:00
assert result.ret == 0
class TestConfigCmdlineParsing(object):
def test_parsing_again_fails(self, testdir):
config = testdir.parseconfig()
pytest.raises(AssertionError, lambda: config.parse([]))
def test_explicitly_specified_config_file_is_loaded(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("custom", "")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeini(
"""
[pytest]
custom = 0
2018-05-23 22:48:46 +08:00
"""
)
testdir.makefile(
".ini",
2018-05-23 22:48:46 +08:00
custom="""
[pytest]
custom = 1
2018-05-23 22:48:46 +08:00
""",
)
config = testdir.parseconfig("-c", "custom.ini")
assert config.getini("custom") == "1"
2018-05-23 22:48:46 +08:00
testdir.makefile(
".cfg",
custom_tool_pytest_section="""
2018-02-28 00:16:45 +08:00
[tool:pytest]
custom = 1
2018-05-23 22:48:46 +08:00
""",
)
2018-02-28 00:16:45 +08:00
config = testdir.parseconfig("-c", "custom_tool_pytest_section.cfg")
assert config.getini("custom") == "1"
2016-04-20 01:27:37 +08:00
def test_absolute_win32_path(self, testdir):
temp_ini_file = testdir.makefile(
".ini",
2018-05-23 22:48:46 +08:00
custom="""
2016-04-20 01:27:37 +08:00
[pytest]
addopts = --version
2018-05-23 22:48:46 +08:00
""",
)
2016-04-20 01:27:37 +08:00
from os.path import normpath
2018-05-23 22:48:46 +08:00
temp_ini_file = normpath(str(temp_ini_file))
ret = pytest.main(["-c", temp_ini_file])
2016-04-20 01:27:37 +08:00
assert ret == _pytest.main.EXIT_OK
class TestConfigAPI(object):
def test_config_trace(self, testdir):
2013-09-29 04:22:53 +08:00
config = testdir.parseconfig()
values = []
config.trace.root.setwriter(values.append)
config.trace("hello")
assert len(values) == 1
assert values[0] == "hello [config]\n"
def test_config_getoption(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addoption("--hello", "-X", dest="hello")
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig("--hello=this")
for x in ("hello", "--hello", "-X"):
assert config.getoption(x) == "this"
pytest.raises(ValueError, "config.getoption('qweqwe')")
2018-05-23 22:48:46 +08:00
@pytest.mark.skipif("sys.version_info[0] < 3")
def test_config_getoption_unicode(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
from __future__ import unicode_literals
def pytest_addoption(parser):
parser.addoption('--hello', type=str)
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig("--hello=this")
assert config.getoption("hello") == "this"
def test_config_getvalueorskip(self, testdir):
config = testdir.parseconfig()
2018-05-23 22:48:46 +08:00
pytest.raises(pytest.skip.Exception, "config.getvalueorskip('hello')")
verbose = config.getvalueorskip("verbose")
assert verbose == config.option.verbose
def test_config_getvalueorskip_None(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addoption("--hello")
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
2014-06-16 17:27:32 +08:00
with pytest.raises(pytest.skip.Exception):
2018-05-23 22:48:46 +08:00
config.getvalueorskip("hello")
def test_getoption(self, testdir):
config = testdir.parseconfig()
with pytest.raises(ValueError):
2018-05-23 22:48:46 +08:00
config.getvalue("x")
assert config.getoption("x", 1) == 1
def test_getconftest_pathlist(self, testdir, tmpdir):
somepath = tmpdir.join("x", "y", "z")
p = tmpdir.join("conftest.py")
p.write("pathlist = ['.', %r]" % str(somepath))
config = testdir.parseconfigure(p)
2018-05-23 22:48:46 +08:00
assert config._getconftest_pathlist("notexist", path=tmpdir) is None
pl = config._getconftest_pathlist("pathlist", path=tmpdir)
print(pl)
assert len(pl) == 2
assert pl[0] == tmpdir
assert pl[1] == somepath
def test_addini(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("myname", "my new ini value")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeini(
"""
[pytest]
myname=hello
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
val = config.getini("myname")
assert val == "hello"
2018-05-23 22:48:46 +08:00
pytest.raises(ValueError, config.getini, "other")
def test_addini_pathlist(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("paths", "my new ini value", type="pathlist")
parser.addini("abc", "abc value")
2018-05-23 22:48:46 +08:00
"""
)
p = testdir.makeini(
"""
[pytest]
paths=hello world/sub.py
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
values = config.getini("paths")
assert len(values) == 2
2018-05-23 22:48:46 +08:00
assert values[0] == p.dirpath("hello")
assert values[1] == p.dirpath("world/sub.py")
pytest.raises(ValueError, config.getini, "other")
def test_addini_args(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("args", "new args", type="args")
parser.addini("a2", "", "args", default="1 2 3".split())
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeini(
"""
[pytest]
args=123 "123 hello" "this"
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
values = config.getini("args")
assert len(values) == 3
assert values == ["123", "123 hello", "this"]
values = config.getini("a2")
assert values == list("123")
2010-11-06 06:37:31 +08:00
def test_addini_linelist(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
2010-11-06 06:37:31 +08:00
def pytest_addoption(parser):
parser.addini("xy", "", type="linelist")
parser.addini("a2", "", "linelist")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeini(
"""
2010-11-06 06:37:31 +08:00
[pytest]
xy= 123 345
second line
2018-05-23 22:48:46 +08:00
"""
)
2010-11-06 06:37:31 +08:00
config = testdir.parseconfig()
values = config.getini("xy")
assert len(values) == 2
assert values == ["123 345", "second line"]
values = config.getini("a2")
assert values == []
2010-11-06 06:37:31 +08:00
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize(
"str_val, bool_val", [("True", True), ("no", False), ("no-ini", True)]
)
2016-02-15 05:27:48 +08:00
def test_addini_bool(self, testdir, str_val, bool_val):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
2016-02-15 05:27:48 +08:00
def pytest_addoption(parser):
parser.addini("strip", "", type="bool", default=True)
2018-05-23 22:48:46 +08:00
"""
)
if str_val != "no-ini":
testdir.makeini(
"""
2016-02-15 05:27:48 +08:00
[pytest]
strip=%s
2018-05-23 22:48:46 +08:00
"""
% str_val
)
2016-02-15 05:27:48 +08:00
config = testdir.parseconfig()
assert config.getini("strip") is bool_val
def test_addinivalue_line_existing(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("xy", "", type="linelist")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeini(
"""
[pytest]
xy= 123
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
values = config.getini("xy")
assert len(values) == 1
assert values == ["123"]
config.addinivalue_line("xy", "456")
values = config.getini("xy")
assert len(values) == 2
assert values == ["123", "456"]
def test_addinivalue_line_new(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("xy", "", type="linelist")
2018-05-23 22:48:46 +08:00
"""
)
config = testdir.parseconfig()
assert not config.getini("xy")
config.addinivalue_line("xy", "456")
values = config.getini("xy")
assert len(values) == 1
assert values == ["456"]
config.addinivalue_line("xy", "123")
values = config.getini("xy")
assert len(values) == 2
assert values == ["456", "123"]
def test_confcutdir_check_isdir(self, testdir):
"""Give an error if --confcutdir is not a valid directory (#2078)"""
with pytest.raises(pytest.UsageError):
2018-05-23 22:48:46 +08:00
testdir.parseconfig(
"--confcutdir", testdir.tmpdir.join("file").ensure(file=1)
)
with pytest.raises(pytest.UsageError):
2018-05-23 22:48:46 +08:00
testdir.parseconfig("--confcutdir", testdir.tmpdir.join("inexistant"))
config = testdir.parseconfig(
"--confcutdir", testdir.tmpdir.join("dir").ensure(dir=1)
)
assert config.getoption("confcutdir") == str(testdir.tmpdir.join("dir"))
@pytest.mark.parametrize(
"names, expected",
[
(["bar.py"], ["bar"]),
(["foo", "bar.py"], []),
(["foo", "bar.pyc"], []),
(["foo", "__init__.py"], ["foo"]),
(["foo", "bar", "__init__.py"], []),
],
)
def test_iter_rewritable_modules(self, names, expected):
2018-05-23 22:48:46 +08:00
assert list(_iter_rewritable_modules(["/".join(names)])) == expected
class TestConfigFromdictargs(object):
def test_basic_behavior(self):
from _pytest.config import Config
2018-05-23 22:48:46 +08:00
option_dict = {"verbose": 444, "foo": "bar", "capture": "no"}
args = ["a", "b"]
config = Config.fromdictargs(option_dict, args)
with pytest.raises(AssertionError):
2018-05-23 22:48:46 +08:00
config.parse(["should refuse to parse again"])
2015-09-25 08:52:53 +08:00
assert config.option.verbose == 444
2018-05-23 22:48:46 +08:00
assert config.option.foo == "bar"
assert config.option.capture == "no"
assert config.args == args
def test_origargs(self):
"""Show that fromdictargs can handle args in their "orig" format"""
from _pytest.config import Config
2018-05-23 22:48:46 +08:00
option_dict = {}
2018-05-23 22:48:46 +08:00
args = ["-vvvv", "-s", "a", "b"]
config = Config.fromdictargs(option_dict, args)
2018-05-23 22:48:46 +08:00
assert config.args == ["a", "b"]
2015-09-25 08:52:53 +08:00
assert config._origargs == args
assert config.option.verbose == 4
2018-05-23 22:48:46 +08:00
assert config.option.capture == "no"
2015-09-25 08:52:53 +08:00
def test_inifilename(self, tmpdir):
2018-05-23 22:48:46 +08:00
tmpdir.join("foo/bar.ini").ensure().write(
textwrap.dedent(
"""\
[pytest]
name = value
2018-05-23 22:48:46 +08:00
"""
)
)
from _pytest.config import Config
2018-05-23 22:48:46 +08:00
inifile = "../../foo/bar.ini"
option_dict = {"inifilename": inifile, "capture": "no"}
cwd = tmpdir.join("a/b")
cwd.join("pytest.ini").ensure().write(
textwrap.dedent(
"""\
[pytest]
name = wrong-value
should_not_be_set = true
2018-05-23 22:48:46 +08:00
"""
)
)
2015-09-25 08:52:53 +08:00
with cwd.ensure(dir=True).as_cwd():
config = Config.fromdictargs(option_dict, ())
assert config.args == [str(cwd)]
assert config.option.inifilename == inifile
2018-05-23 22:48:46 +08:00
assert config.option.capture == "no"
# this indicates this is the file used for getting configuration values
assert config.inifile == inifile
2018-05-23 22:48:46 +08:00
assert config.inicfg.get("name") == "value"
assert config.inicfg.get("should_not_be_set") is None
def test_options_on_small_file_do_not_blow_up(testdir):
def runfiletest(opts):
reprec = testdir.inline_run(*opts)
passed, skipped, failed = reprec.countoutcomes()
assert failed == 2
assert skipped == passed == 0
2018-05-23 22:48:46 +08:00
path = testdir.makepyfile(
"""
def test_f1(): assert 0
def test_f2(): assert 0
2018-05-23 22:48:46 +08:00
"""
)
2018-05-23 22:48:46 +08:00
for opts in (
[],
["-l"],
["-s"],
["--tb=no"],
["--tb=short"],
["--tb=long"],
["--fulltrace"],
["--traceconfig"],
["-v"],
["-v", "-v"],
):
runfiletest(opts + [path])
def test_preparse_ordering_with_setuptools(testdir, monkeypatch):
pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name):
assert name == "pytest11"
class Dist(object):
2018-05-23 22:48:46 +08:00
project_name = "spam"
version = "1.0"
def _get_metadata(self, name):
2018-05-23 22:48:46 +08:00
return ["foo.txt,sha256=abc,123"]
class EntryPoint(object):
name = "mytestplugin"
dist = Dist()
def load(self):
class PseudoPlugin(object):
x = 42
2018-05-23 22:48:46 +08:00
return PseudoPlugin()
return iter([EntryPoint()])
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
testdir.makeconftest(
"""
pytest_plugins = "mytestplugin",
2018-05-23 22:48:46 +08:00
"""
)
monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin")
config = testdir.parseconfig()
plugin = config.pluginmanager.getplugin("mytestplugin")
assert plugin.x == 42
def test_setuptools_importerror_issue1479(testdir, monkeypatch):
pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name):
assert name == "pytest11"
class Dist(object):
2018-05-23 22:48:46 +08:00
project_name = "spam"
version = "1.0"
def _get_metadata(self, name):
2018-05-23 22:48:46 +08:00
return ["foo.txt,sha256=abc,123"]
class EntryPoint(object):
name = "mytestplugin"
dist = Dist()
def load(self):
raise ImportError("Don't hide me!")
return iter([EntryPoint()])
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
with pytest.raises(ImportError):
testdir.parseconfig()
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize("block_it", [True, False])
def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch, block_it):
pkg_resources = pytest.importorskip("pkg_resources")
plugin_module_placeholder = object()
def my_iter(name):
assert name == "pytest11"
class Dist(object):
2018-05-23 22:48:46 +08:00
project_name = "spam"
version = "1.0"
def _get_metadata(self, name):
2018-05-23 22:48:46 +08:00
return ["foo.txt,sha256=abc,123"]
class EntryPoint(object):
name = "mytestplugin"
dist = Dist()
def load(self):
return plugin_module_placeholder
return iter([EntryPoint()])
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
args = ("-p", "no:mytestplugin") if block_it else ()
config = testdir.parseconfig(*args)
2017-11-29 00:29:52 +08:00
config.pluginmanager.import_plugin("mytestplugin")
if block_it:
assert "mytestplugin" not in sys.modules
2018-05-23 22:48:46 +08:00
assert config.pluginmanager.get_plugin("mytestplugin") is None
else:
assert (
config.pluginmanager.get_plugin("mytestplugin") is plugin_module_placeholder
)
@pytest.mark.parametrize(
"parse_args,should_load", [(("-p", "mytestplugin"), True), ((), False)]
)
def test_disable_plugin_autoload(testdir, monkeypatch, parse_args, should_load):
pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name):
raise AssertionError("Should not be called")
class PseudoPlugin(object):
x = 42
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
monkeypatch.setitem(sys.modules, "mytestplugin", PseudoPlugin())
config = testdir.parseconfig(*parse_args)
has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None
assert has_loaded == should_load
def test_cmdline_processargs_simple(testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_cmdline_preparse(args):
args.append("-h")
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*pytest*", "*-h*"])
def test_invalid_options_show_extra_information(testdir):
"""display extra information when pytest exits due to unrecognized
options in the command-line"""
2018-05-23 22:48:46 +08:00
testdir.makeini(
"""
[pytest]
addopts = --invalid-option
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(
[
"*error: unrecognized arguments: --invalid-option*",
"* inifile: %s*" % testdir.tmpdir.join("tox.ini"),
"* rootdir: %s*" % testdir.tmpdir,
]
)
@pytest.mark.parametrize(
"args",
[
["dir1", "dir2", "-v"],
["dir1", "-v", "dir2"],
["dir2", "-v", "dir1"],
["-v", "dir2", "dir1"],
],
)
def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args):
"""
Consider all arguments in the command-line for rootdir and inifile
discovery, even if they happen to occur after an option. #949
"""
# replace "dir1" and "dir2" from "args" into their real directory
2018-05-23 22:48:46 +08:00
root = testdir.tmpdir.mkdir("myroot")
d1 = root.mkdir("dir1")
d2 = root.mkdir("dir2")
for i, arg in enumerate(args):
2018-05-23 22:48:46 +08:00
if arg == "dir1":
args[i] = d1
2018-05-23 22:48:46 +08:00
elif arg == "dir2":
args[i] = d2
with root.as_cwd():
result = testdir.runpytest(*args)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*rootdir: *myroot, inifile:"])
@pytest.mark.skipif("sys.platform == 'win32'")
def test_toolongargs_issue224(testdir):
result = testdir.runpytest("-m", "hello" * 500)
assert result.ret == EXIT_NOTESTSCOLLECTED
def test_config_in_subdirectory_colon_command_line_issue2148(testdir):
2018-05-23 22:48:46 +08:00
conftest_source = """
def pytest_addoption(parser):
parser.addini('foo', 'foo')
2018-05-23 22:48:46 +08:00
"""
2018-05-23 22:48:46 +08:00
testdir.makefile(
".ini",
**{"pytest": "[pytest]\nfoo = root", "subdir/pytest": "[pytest]\nfoo = subdir"}
)
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
**{
"conftest": conftest_source,
"subdir/conftest": conftest_source,
"subdir/test_foo": """
def test_foo(pytestconfig):
assert pytestconfig.getini('foo') == 'subdir'
2018-05-23 22:48:46 +08:00
""",
}
)
2018-05-23 22:48:46 +08:00
result = testdir.runpytest("subdir/test_foo.py::test_foo")
assert result.ret == 0
def test_notify_exception(testdir, capfd):
config = testdir.parseconfig()
excinfo = pytest.raises(ValueError, "raise ValueError(1)")
config.notify_exception(excinfo)
out, err = capfd.readouterr()
assert "ValueError" in err
class A(object):
def pytest_internalerror(self, excrepr):
return True
config.pluginmanager.register(A())
config.notify_exception(excinfo)
out, err = capfd.readouterr()
assert not err
def test_load_initial_conftest_last_ordering(testdir):
from _pytest.config import get_config
2018-05-23 22:48:46 +08:00
pm = get_config().pluginmanager
class My(object):
def pytest_load_initial_conftests(self):
pass
m = My()
pm.register(m)
hc = pm.hook.pytest_load_initial_conftests
values = hc._nonwrappers + hc._wrappers
2018-05-23 22:48:46 +08:00
expected = ["_pytest.config", "test_config", "_pytest.capture"]
assert [x.function.__module__ for x in values] == expected
def test_get_plugin_specs_as_list():
from _pytest.config import _get_plugin_specs_as_list
2018-05-23 22:48:46 +08:00
with pytest.raises(pytest.UsageError):
2018-05-23 22:48:46 +08:00
_get_plugin_specs_as_list({"foo"})
with pytest.raises(pytest.UsageError):
_get_plugin_specs_as_list(dict())
assert _get_plugin_specs_as_list(None) == []
2018-05-23 22:48:46 +08:00
assert _get_plugin_specs_as_list("") == []
assert _get_plugin_specs_as_list("foo") == ["foo"]
assert _get_plugin_specs_as_list("foo,bar") == ["foo", "bar"]
assert _get_plugin_specs_as_list(["foo", "bar"]) == ["foo", "bar"]
assert _get_plugin_specs_as_list(("foo", "bar")) == ["foo", "bar"]
def test_collect_pytest_prefix_bug_integration(testdir):
"""Integration test for issue #3775"""
p = testdir.copy_example("config/collect_pytest_prefix")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines("* 1 passed *")
def test_collect_pytest_prefix_bug(pytestconfig):
"""Ensure we collect only actual functions from conftest files (#3775)"""
class Dummy(object):
class pytest_something(object):
pass
pm = pytestconfig.pluginmanager
assert pm.parse_hookimpl_opts(Dummy(), "pytest_something") is None
class TestLegacyWarning(object):
@pytest.mark.filterwarnings("default")
def test_warn_config(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
values = []
def pytest_runtest_setup(item):
item.config.warn("C1", "hello")
def pytest_logwarning(code, message):
if message == "hello" and code == "C1":
values.append(1)
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(
"""
def test_proper(pytestconfig):
import conftest
assert conftest.values == [1]
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
["*hello", "*config.warn has been deprecated*", "*1 passed*"]
)
@pytest.mark.filterwarnings("default")
@pytest.mark.parametrize("use_kw", [True, False])
def test_warn_on_test_item_from_request(self, testdir, use_kw):
code_kw = "code=" if use_kw else ""
message_kw = "message=" if use_kw else ""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
import pytest
@pytest.fixture
def fix(request):
request.node.warn({code_kw}"T1", {message_kw}"hello")
def test_hello(fix):
pass
""".format(
code_kw=code_kw, message_kw=message_kw
)
2018-05-23 22:48:46 +08:00
)
result = testdir.runpytest("--disable-pytest-warnings")
assert "hello" not in result.stdout.str()
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
"""
2017-03-08 07:47:43 +08:00
===*warnings summary*===
*test_warn_on_test_item_from_request.py::test_hello*
2017-03-08 07:47:43 +08:00
*hello*
*test_warn_on_test_item_from_request.py:7:*Node.warn(code, message) form has been deprecated*
2018-05-23 22:48:46 +08:00
"""
)
class TestRootdir(object):
def test_simple_noini(self, tmpdir):
assert get_common_ancestor([tmpdir]) == tmpdir
a = tmpdir.mkdir("a")
assert get_common_ancestor([a, tmpdir]) == tmpdir
assert get_common_ancestor([tmpdir, a]) == tmpdir
with tmpdir.as_cwd():
assert get_common_ancestor([]) == tmpdir
2018-05-23 22:48:46 +08:00
no_path = tmpdir.join("does-not-exist")
assert get_common_ancestor([no_path]) == tmpdir
2018-05-23 22:48:46 +08:00
assert get_common_ancestor([no_path.join("a")]) == tmpdir
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
def test_with_ini(self, tmpdir, name):
inifile = tmpdir.join(name)
inifile.write("[pytest]\n" if name != "setup.cfg" else "[tool:pytest]\n")
a = tmpdir.mkdir("a")
b = a.mkdir("b")
for args in ([tmpdir], [a], [b]):
rootdir, inifile, inicfg = determine_setup(None, args)
assert rootdir == tmpdir
assert inifile == inifile
rootdir, inifile, inicfg = determine_setup(None, [b, a])
assert rootdir == tmpdir
assert inifile == inifile
@pytest.mark.parametrize("name", "setup.cfg tox.ini".split())
2018-11-09 08:44:53 +08:00
def test_pytestini_overrides_empty_other(self, tmpdir, name):
inifile = tmpdir.ensure("pytest.ini")
a = tmpdir.mkdir("a")
a.ensure(name)
rootdir, inifile, inicfg = determine_setup(None, [a])
assert rootdir == tmpdir
assert inifile == inifile
def test_setuppy_fallback(self, tmpdir):
a = tmpdir.mkdir("a")
a.ensure("setup.cfg")
tmpdir.ensure("setup.py")
rootdir, inifile, inicfg = determine_setup(None, [a])
assert rootdir == tmpdir
assert inifile is None
assert inicfg == {}
def test_nothing(self, tmpdir, monkeypatch):
monkeypatch.chdir(str(tmpdir))
rootdir, inifile, inicfg = determine_setup(None, [tmpdir])
assert rootdir == tmpdir
assert inifile is None
assert inicfg == {}
def test_with_specific_inifile(self, tmpdir):
inifile = tmpdir.ensure("pytest.ini")
rootdir, inifile, inicfg = determine_setup(inifile, [tmpdir])
assert rootdir == tmpdir
class TestOverrideIniArgs(object):
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
def test_override_ini_names(self, testdir, name):
section = "[pytest]" if name != "setup.cfg" else "[tool:pytest]"
2018-05-23 22:48:46 +08:00
testdir.tmpdir.join(name).write(
textwrap.dedent(
"""
{section}
custom = 1.0""".format(
section=section
)
2018-05-23 22:48:46 +08:00
)
)
testdir.makeconftest(
"""
def pytest_addoption(parser):
2018-05-23 22:48:46 +08:00
parser.addini("custom", "")"""
)
testdir.makepyfile(
"""
def test_pass(pytestconfig):
ini_val = pytestconfig.getini("custom")
2018-05-23 22:48:46 +08:00
print('\\ncustom_option:%s\\n' % ini_val)"""
)
result = testdir.runpytest("--override-ini", "custom=2.0", "-s")
assert result.ret == 0
2016-07-24 00:09:42 +08:00
result.stdout.fnmatch_lines(["custom_option:2.0"])
2018-05-23 22:48:46 +08:00
result = testdir.runpytest(
"--override-ini", "custom=2.0", "--override-ini=custom=3.0", "-s"
)
assert result.ret == 0
2016-07-24 00:09:42 +08:00
result.stdout.fnmatch_lines(["custom_option:3.0"])
def test_override_ini_pathlist(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
2016-07-24 00:09:42 +08:00
def pytest_addoption(parser):
2018-05-23 22:48:46 +08:00
parser.addini("paths", "my new ini value", type="pathlist")"""
)
testdir.makeini(
"""
2016-07-24 00:09:42 +08:00
[pytest]
2018-05-23 22:48:46 +08:00
paths=blah.py"""
)
testdir.makepyfile(
"""
2016-07-24 00:09:42 +08:00
import py.path
def test_pathlist(pytestconfig):
config_paths = pytestconfig.getini("paths")
print(config_paths)
for cpf in config_paths:
2018-05-23 22:48:46 +08:00
print('\\nuser_path:%s' % cpf.basename)"""
)
result = testdir.runpytest(
"--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s"
)
result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])
def test_override_multiple_and_default(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
2016-07-24 00:09:42 +08:00
def pytest_addoption(parser):
addini = parser.addini
addini("custom_option_1", "", default="o1")
addini("custom_option_2", "", default="o2")
addini("custom_option_3", "", default=False, type="bool")
2018-05-23 22:48:46 +08:00
addini("custom_option_4", "", default=True, type="bool")"""
)
testdir.makeini(
"""
2016-07-24 00:09:42 +08:00
[pytest]
custom_option_1=custom_option_1
custom_option_2=custom_option_2
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(
"""
2016-07-24 00:09:42 +08:00
def test_multiple_options(pytestconfig):
prefix = "custom_option"
for x in range(1, 5):
ini_value=pytestconfig.getini("%s_%d" % (prefix, x))
print('\\nini%d:%s' % (x, ini_value))
2018-05-23 22:48:46 +08:00
"""
)
2016-07-24 00:09:42 +08:00
result = testdir.runpytest(
2018-05-23 22:48:46 +08:00
"--override-ini",
"custom_option_1=fulldir=/tmp/user1",
"-o",
"custom_option_2=url=/tmp/user2?a=b&d=e",
"-o",
"custom_option_3=True",
"-o",
"custom_option_4=no",
"-s",
)
result.stdout.fnmatch_lines(
[
"ini1:fulldir=/tmp/user1",
"ini2:url=/tmp/user2?a=b&d=e",
"ini3:True",
"ini4:False",
]
)
def test_override_ini_usage_error_bad_style(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeini(
"""
[pytest]
xdist_strict=False
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("--override-ini", "xdist_strict True", "-s")
result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"])
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize("with_ini", [True, False])
def test_override_ini_handled_asap(self, testdir, with_ini):
"""-o should be handled as soon as possible and always override what's in ini files (#2238)"""
if with_ini:
2018-05-23 22:48:46 +08:00
testdir.makeini(
"""
[pytest]
python_files=test_*.py
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(
unittest_ini_handle="""
def test():
pass
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("--override-ini", "python_files=unittest_*.py")
result.stdout.fnmatch_lines(["*1 passed in*"])
def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch):
monkeypatch.chdir(str(tmpdir))
a = tmpdir.mkdir("a")
b = tmpdir.mkdir("b")
rootdir, inifile, inicfg = determine_setup(None, [a, b])
assert rootdir == tmpdir
assert inifile is None
def test_with_arg_outside_cwd_with_inifile(self, tmpdir):
a = tmpdir.mkdir("a")
b = tmpdir.mkdir("b")
inifile = a.ensure("pytest.ini")
rootdir, parsed_inifile, inicfg = determine_setup(None, [a, b])
assert rootdir == a
assert inifile == parsed_inifile
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize("dirs", ([], ["does-not-exist"], ["a/does-not-exist"]))
def test_with_non_dir_arg(self, dirs, tmpdir):
with tmpdir.ensure(dir=True).as_cwd():
rootdir, inifile, inicfg = determine_setup(None, dirs)
assert rootdir == tmpdir
assert inifile is None
def test_with_existing_file_in_subdir(self, tmpdir):
a = tmpdir.mkdir("a")
a.ensure("exist")
with tmpdir.as_cwd():
2018-05-23 22:48:46 +08:00
rootdir, inifile, inicfg = determine_setup(None, ["a/exist"])
assert rootdir == tmpdir
assert inifile is None
2017-10-11 01:03:15 +08:00
def test_addopts_before_initini(self, monkeypatch):
2018-05-23 22:48:46 +08:00
cache_dir = ".custom_cache"
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
2017-10-11 01:03:15 +08:00
from _pytest.config import get_config
2018-05-23 22:48:46 +08:00
2017-10-11 01:03:15 +08:00
config = get_config()
config._preparse([], addopts=True)
2018-05-23 22:48:46 +08:00
assert config._override_ini == ["cache_dir=%s" % cache_dir]
2018-01-21 02:23:08 +08:00
def test_override_ini_does_not_contain_paths(self):
"""Check that -o no longer swallows all options after it (#3103)"""
from _pytest.config import get_config
2018-05-23 22:48:46 +08:00
config = get_config()
2018-05-23 22:48:46 +08:00
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
assert config._override_ini == ["cache_dir=/cache"]
def test_multiple_override_ini_options(self, testdir, request):
"""Ensure a file path following a '-o' option does not generate an error (#3103)"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
**{
"conftest.py": """
def pytest_addoption(parser):
parser.addini('foo', default=None, help='some option')
parser.addini('bar', default=None, help='some option')
""",
2018-05-23 22:48:46 +08:00
"test_foo.py": """
def test(pytestconfig):
assert pytestconfig.getini('foo') == '1'
assert pytestconfig.getini('bar') == '0'
""",
2018-05-23 22:48:46 +08:00
"test_bar.py": """
def test():
assert False
""",
2018-05-23 22:48:46 +08:00
}
)
result = testdir.runpytest("-o", "foo=1", "-o", "bar=0", "test_foo.py")
assert "ERROR:" not in result.stderr.str()
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="])