2018-10-25 15:01:29 +08:00
|
|
|
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
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2019-02-08 06:59:10 +08:00
|
|
|
import attr
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2018-06-05 16:20:36 +08:00
|
|
|
from _pytest.config import _iter_rewritable_modules
|
2019-01-18 05:06:45 +08:00
|
|
|
from _pytest.config.exceptions import UsageError
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.config.findpaths import determine_setup
|
|
|
|
from _pytest.config.findpaths import get_common_ancestor
|
|
|
|
from _pytest.config.findpaths import getcfg
|
2015-07-05 01:42:22 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
2019-03-21 11:50:51 +08:00
|
|
|
from _pytest.main import EXIT_OK
|
2019-03-20 09:25:35 +08:00
|
|
|
from _pytest.main import EXIT_TESTSFAILED
|
2019-01-18 05:06:45 +08:00
|
|
|
from _pytest.main import EXIT_USAGEERROR
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestParseIni(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")]
|
|
|
|
)
|
2016-08-17 08:30:07 +08:00
|
|
|
def test_getcfg_and_config(self, testdir, tmpdir, section, filename):
|
2010-10-28 01:35:27 +08:00
|
|
|
sub = tmpdir.mkdir("sub")
|
|
|
|
sub.chdir()
|
2018-05-23 22:48:46 +08:00
|
|
|
tmpdir.join(filename).write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[{section}]
|
|
|
|
name = value
|
|
|
|
""".format(
|
2018-05-23 22:48:46 +08:00
|
|
|
section=section
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2016-08-17 08:30:07 +08:00
|
|
|
rootdir, inifile, cfg = getcfg([sub])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert cfg["name"] == "value"
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(sub)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.inicfg["name"] == "value"
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2016-08-17 08:30:07 +08:00
|
|
|
def test_getcfg_empty_path(self):
|
|
|
|
"""correctly handle zero length arguments (a la pytest '')"""
|
2018-05-23 22:48:46 +08:00
|
|
|
getcfg([""])
|
2011-09-26 05:26:49 +08:00
|
|
|
|
2019-03-28 07:27:23 +08:00
|
|
|
def test_setupcfg_uses_toolpytest_with_pytest(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test(): pass")
|
|
|
|
testdir.makefile(
|
|
|
|
".cfg",
|
|
|
|
setup="""
|
|
|
|
[tool:pytest]
|
|
|
|
testpaths=%s
|
|
|
|
[pytest]
|
|
|
|
testpaths=ignored
|
|
|
|
"""
|
|
|
|
% p1.basename,
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*, inifile: setup.cfg, *", "* 1 passed in *"])
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2015-01-29 18:52:01 +08:00
|
|
|
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(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2011-11-08 02:08:41 +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"
|
2010-10-28 01:35:27 +08:00
|
|
|
assert config.option.verbose
|
|
|
|
|
|
|
|
def test_tox_ini_wrong_version(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makefile(
|
|
|
|
".ini",
|
|
|
|
tox="""
|
2010-10-28 01:35:27 +08:00
|
|
|
[pytest]
|
|
|
|
minversion=9.0
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2010-10-28 01:35:27 +08:00
|
|
|
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")],
|
2016-08-17 08:30:07 +08:00
|
|
|
)
|
|
|
|
def test_ini_names(self, testdir, name, section):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join(name).write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2016-08-17 08:30:07 +08:00
|
|
|
[{section}]
|
2010-11-07 23:10:22 +08:00
|
|
|
minversion = 1.0
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
section=section
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig()
|
2010-11-07 23:10:22 +08:00
|
|
|
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(
|
|
|
|
"""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 2.0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.tmpdir.join("pytest.ini").write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 1.5
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(sub)
|
2010-11-07 23:10:22 +08:00
|
|
|
assert config.getini("minversion") == "2.0"
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@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
|
|
|
"""
|
|
|
|
)
|
2015-04-28 17:54:45 +08:00
|
|
|
result = testdir.inline_run("--confcutdir=.")
|
2010-11-07 08:10:15 +08:00
|
|
|
assert result.ret == 0
|
2016-12-01 20:20:42 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestConfigCmdlineParsing(object):
|
2009-12-30 01:02:54 +08:00
|
|
|
def test_parsing_again_fails(self, testdir):
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(AssertionError, lambda: config.parse([]))
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2014-06-27 23:42:37 +08:00
|
|
|
def test_explicitly_specified_config_file_is_loaded(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2014-06-27 23:42:37 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("custom", "")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2014-06-27 23:42:37 +08:00
|
|
|
[pytest]
|
|
|
|
custom = 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makefile(
|
2018-09-02 08:58:48 +08:00
|
|
|
".ini",
|
2018-05-23 22:48:46 +08:00
|
|
|
custom="""
|
2014-06-27 23:42:37 +08:00
|
|
|
[pytest]
|
|
|
|
custom = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2018-09-02 08:58:48 +08:00
|
|
|
config = testdir.parseconfig("-c", "custom.ini")
|
2014-06-27 23:42:37 +08:00
|
|
|
assert config.getini("custom") == "1"
|
2008-08-16 23:26:59 +08:00
|
|
|
|
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):
|
2018-09-02 08:58:48 +08:00
|
|
|
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
|
|
|
|
2018-09-02 08:58:48 +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
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestConfigAPI(object):
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_config_trace(self, testdir):
|
2013-09-29 04:22:53 +08:00
|
|
|
config = testdir.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
|
|
|
config.trace.root.setwriter(values.append)
|
2010-11-06 06:37:31 +08:00
|
|
|
config.trace("hello")
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
|
|
|
assert values[0] == "hello [config]\n"
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2012-11-06 21:09:12 +08:00
|
|
|
def test_config_getoption(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-06 21:09:12 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello", "-X", dest="hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-06 21:09:12 +08:00
|
|
|
config = testdir.parseconfig("--hello=this")
|
|
|
|
for x in ("hello", "--hello", "-X"):
|
|
|
|
assert config.getoption(x) == "this"
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ValueError, config.getoption, "qweqwe")
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info[0] < 3")
|
2013-10-01 20:20:20 +08:00
|
|
|
def test_config_getoption_unicode(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2013-10-01 20:20:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2017-03-21 09:01:22 +08:00
|
|
|
parser.addoption('--hello', type=str)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
config = testdir.parseconfig("--hello=this")
|
|
|
|
assert config.getoption("hello") == "this"
|
2013-10-01 20:20:20 +08:00
|
|
|
|
2009-03-02 19:14:59 +08:00
|
|
|
def test_config_getvalueorskip(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(pytest.skip.Exception, config.getvalueorskip, "hello")
|
2009-03-02 19:14:59 +08:00
|
|
|
verbose = config.getvalueorskip("verbose")
|
|
|
|
assert verbose == config.option.verbose
|
2014-04-03 04:30:45 +08:00
|
|
|
|
2014-05-14 13:36:31 +08:00
|
|
|
def test_config_getvalueorskip_None(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2014-05-14 13:36:31 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-05-14 13:36:31 +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")
|
2014-05-14 13:36:31 +08:00
|
|
|
|
2014-04-03 04:30:45 +08:00
|
|
|
def test_getoption(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
with pytest.raises(ValueError):
|
2018-05-23 22:48:46 +08:00
|
|
|
config.getvalue("x")
|
2014-04-03 04:30:45 +08:00
|
|
|
assert config.getoption("x", 1) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-12-30 01:02:54 +08:00
|
|
|
def test_getconftest_pathlist(self, testdir, tmpdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
somepath = tmpdir.join("x", "y", "z")
|
|
|
|
p = tmpdir.join("conftest.py")
|
|
|
|
p.write("pathlist = ['.', %r]" % str(somepath))
|
2011-11-08 02:08:41 +08:00
|
|
|
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)
|
2009-08-30 02:04:48 +08:00
|
|
|
print(pl)
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(pl) == 2
|
|
|
|
assert pl[0] == tmpdir
|
|
|
|
assert pl[1] == somepath
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
def test_addini(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("myname", "my new ini value")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
[pytest]
|
|
|
|
myname=hello
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-11-01 00:41:58 +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")
|
2010-11-01 00:41:58 +08:00
|
|
|
|
|
|
|
def test_addini_pathlist(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
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(
|
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
[pytest]
|
|
|
|
paths=hello world/sub.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-11-01 00:41:58 +08:00
|
|
|
config = testdir.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
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")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-11-05 06:21:26 +08:00
|
|
|
def test_addini_args(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-11-05 06:21:26 +08:00
|
|
|
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(
|
|
|
|
"""
|
2010-11-05 06:21:26 +08:00
|
|
|
[pytest]
|
|
|
|
args=123 "123 hello" "this"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-11-05 06:21:26 +08:00
|
|
|
config = testdir.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("args")
|
|
|
|
assert len(values) == 3
|
|
|
|
assert values == ["123", "123 hello", "this"]
|
|
|
|
values = config.getini("a2")
|
|
|
|
assert values == list("123")
|
2010-11-05 06:21:26 +08:00
|
|
|
|
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()
|
2017-11-04 23:17:20 +08:00
|
|
|
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
|
|
|
|
|
2011-11-12 06:56:08 +08:00
|
|
|
def test_addinivalue_line_existing(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
[pytest]
|
|
|
|
xy= 123
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-11-12 06:56:08 +08:00
|
|
|
config = testdir.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 1
|
|
|
|
assert values == ["123"]
|
2011-11-12 06:56:08 +08:00
|
|
|
config.addinivalue_line("xy", "456")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values == ["123", "456"]
|
2011-11-12 06:56:08 +08:00
|
|
|
|
|
|
|
def test_addinivalue_line_new(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-11-12 06:56:08 +08:00
|
|
|
config = testdir.parseconfig()
|
|
|
|
assert not config.getini("xy")
|
|
|
|
config.addinivalue_line("xy", "456")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 1
|
|
|
|
assert values == ["456"]
|
2011-11-12 06:56:08 +08:00
|
|
|
config.addinivalue_line("xy", "123")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values == ["456", "123"]
|
2011-11-12 06:56:08 +08:00
|
|
|
|
2016-11-23 19:47:36 +08:00
|
|
|
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)
|
|
|
|
)
|
2016-11-23 19:47:36 +08:00
|
|
|
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"], []),
|
|
|
|
],
|
|
|
|
)
|
2017-07-29 03:03:07 +08:00
|
|
|
def test_iter_rewritable_modules(self, names, expected):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert list(_iter_rewritable_modules(["/".join(names)])) == expected
|
2017-07-29 03:03:07 +08:00
|
|
|
|
2015-09-25 05:13:36 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestConfigFromdictargs(object):
|
2015-09-25 05:13:36 +08:00
|
|
|
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"]
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
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"
|
2015-09-25 05:13:36 +08:00
|
|
|
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
|
|
|
|
2015-09-25 05:13:36 +08:00
|
|
|
option_dict = {}
|
2018-05-23 22:48:46 +08:00
|
|
|
args = ["-vvvv", "-s", "a", "b"]
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
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
|
2015-09-25 05:13:36 +08:00
|
|
|
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(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
name = value
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2015-09-25 05:13:36 +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(
|
2018-08-24 00:06:17 +08:00
|
|
|
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)]
|
2015-09-25 05:13:36 +08:00
|
|
|
assert config.option.inifilename == inifile
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.option.capture == "no"
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
# 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
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
|
2009-02-28 05:32:49 +08:00
|
|
|
def test_options_on_small_file_do_not_blow_up(testdir):
|
|
|
|
def runfiletest(opts):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run(*opts)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert failed == 2
|
2009-02-28 05:32:49 +08:00
|
|
|
assert skipped == passed == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
path = testdir.makepyfile(
|
|
|
|
"""
|
2009-02-28 05:32:49 +08:00
|
|
|
def test_f1(): assert 0
|
|
|
|
def test_f2(): assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-02-28 05:32:49 +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"],
|
|
|
|
):
|
2009-02-28 05:32:49 +08:00
|
|
|
runfiletest(opts + [path])
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def test_preparse_ordering_with_setuptools(testdir, monkeypatch):
|
2014-01-18 19:31:33 +08:00
|
|
|
pkg_resources = pytest.importorskip("pkg_resources")
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-02-23 05:49:56 +08:00
|
|
|
def my_iter(group, name=None):
|
|
|
|
assert group == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
project_name = "spam"
|
|
|
|
version = "1.0"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-06-22 18:42:11 +08:00
|
|
|
def _get_metadata(self, name):
|
2018-05-23 22:48:46 +08:00
|
|
|
return ["foo.txt,sha256=abc,123"]
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class EntryPoint(object):
|
2010-01-03 00:17:13 +08:00
|
|
|
name = "mytestplugin"
|
2016-06-22 18:42:11 +08:00
|
|
|
dist = Dist()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-01-03 00:17:13 +08:00
|
|
|
def load(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class PseudoPlugin(object):
|
2010-01-03 00:17:13 +08:00
|
|
|
x = 42
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-01-03 00:17:13 +08:00
|
|
|
return PseudoPlugin()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-01-03 00:17:13 +08:00
|
|
|
return iter([EntryPoint()])
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-01-03 00:17:13 +08:00
|
|
|
pytest_plugins = "mytestplugin",
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-01-03 00:17:13 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin")
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
plugin = config.pluginmanager.getplugin("mytestplugin")
|
|
|
|
assert plugin.x == 42
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2016-05-23 14:13:41 +08:00
|
|
|
|
|
|
|
def test_setuptools_importerror_issue1479(testdir, monkeypatch):
|
|
|
|
pkg_resources = pytest.importorskip("pkg_resources")
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-02-23 05:49:56 +08:00
|
|
|
def my_iter(group, name=None):
|
|
|
|
assert group == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
project_name = "spam"
|
|
|
|
version = "1.0"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-07-14 19:42:29 +08:00
|
|
|
def _get_metadata(self, name):
|
2018-05-23 22:48:46 +08:00
|
|
|
return ["foo.txt,sha256=abc,123"]
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class EntryPoint(object):
|
2016-05-23 14:13:41 +08:00
|
|
|
name = "mytestplugin"
|
2016-07-14 19:42:29 +08:00
|
|
|
dist = Dist()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-05-23 14:13:41 +08:00
|
|
|
def load(self):
|
|
|
|
raise ImportError("Don't hide me!")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-05-23 14:13:41 +08:00
|
|
|
return iter([EntryPoint()])
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
|
2016-05-23 14:13:41 +08:00
|
|
|
with pytest.raises(ImportError):
|
|
|
|
testdir.parseconfig()
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("block_it", [True, False])
|
2017-11-29 07:29:58 +08:00
|
|
|
def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch, block_it):
|
2014-01-18 19:31:33 +08:00
|
|
|
pkg_resources = pytest.importorskip("pkg_resources")
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-11-29 07:29:58 +08:00
|
|
|
plugin_module_placeholder = object()
|
|
|
|
|
2019-02-23 05:49:56 +08:00
|
|
|
def my_iter(group, name=None):
|
|
|
|
assert group == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
project_name = "spam"
|
|
|
|
version = "1.0"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-06-22 18:42:11 +08:00
|
|
|
def _get_metadata(self, name):
|
2018-05-23 22:48:46 +08:00
|
|
|
return ["foo.txt,sha256=abc,123"]
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class EntryPoint(object):
|
2010-12-06 23:54:42 +08:00
|
|
|
name = "mytestplugin"
|
2016-06-22 18:42:11 +08:00
|
|
|
dist = Dist()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def load(self):
|
2017-11-29 07:29:58 +08:00
|
|
|
return plugin_module_placeholder
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
return iter([EntryPoint()])
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
|
2017-11-29 07:29:58 +08:00
|
|
|
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")
|
2017-11-29 07:29:58 +08:00
|
|
|
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
|
2017-11-29 07:29:58 +08:00
|
|
|
else:
|
2018-06-26 21:35:27 +08:00
|
|
|
assert (
|
|
|
|
config.pluginmanager.get_plugin("mytestplugin") is plugin_module_placeholder
|
|
|
|
)
|
2010-12-07 19:14:12 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-08-07 04:17:24 +08:00
|
|
|
@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")
|
|
|
|
|
2019-02-23 05:49:56 +08:00
|
|
|
def my_iter(group, name=None):
|
2019-02-08 06:59:10 +08:00
|
|
|
assert group == "pytest11"
|
|
|
|
assert name == "mytestplugin"
|
|
|
|
return iter([DummyEntryPoint()])
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class DummyEntryPoint(object):
|
|
|
|
name = "mytestplugin"
|
|
|
|
version = "1.0"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def project_name(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
return sys.modules[self.name]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dist(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _get_metadata(self, *args):
|
|
|
|
return []
|
2018-08-07 04:17:24 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2010-12-07 19:14:12 +08:00
|
|
|
def test_cmdline_processargs_simple(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-12-07 19:34:18 +08:00
|
|
|
def pytest_cmdline_preparse(args):
|
2010-12-07 19:14:12 +08:00
|
|
|
args.append("-h")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*pytest*", "*-h*"])
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-07-09 08:22:08 +08:00
|
|
|
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(
|
|
|
|
"""
|
2015-07-09 08:22:08 +08:00
|
|
|
[pytest]
|
|
|
|
addopts = --invalid-option
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-09 08:22:08 +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"],
|
|
|
|
],
|
|
|
|
)
|
2019-03-02 22:31:09 +08:00
|
|
|
def test_consider_args_after_options_for_rootdir(testdir, args):
|
2015-08-26 09:08:05 +08:00
|
|
|
"""
|
2019-03-02 22:31:09 +08:00
|
|
|
Consider all arguments in the command-line for rootdir
|
2015-08-26 09:08:05 +08:00
|
|
|
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")
|
2015-08-26 09:08:05 +08:00
|
|
|
for i, arg in enumerate(args):
|
2018-05-23 22:48:46 +08:00
|
|
|
if arg == "dir1":
|
2015-08-26 09:08:05 +08:00
|
|
|
args[i] = d1
|
2018-05-23 22:48:46 +08:00
|
|
|
elif arg == "dir2":
|
2015-08-26 09:08:05 +08:00
|
|
|
args[i] = d2
|
2016-06-21 00:47:12 +08:00
|
|
|
with root.as_cwd():
|
|
|
|
result = testdir.runpytest(*args)
|
2019-03-02 22:31:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["*rootdir: *myroot"])
|
2015-08-26 09:08:05 +08:00
|
|
|
|
|
|
|
|
2012-11-20 18:52:06 +08:00
|
|
|
@pytest.mark.skipif("sys.platform == 'win32'")
|
2012-11-12 17:15:43 +08:00
|
|
|
def test_toolongargs_issue224(testdir):
|
|
|
|
result = testdir.runpytest("-m", "hello" * 500)
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2013-09-30 19:14:14 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-12-20 18:22:18 +08:00
|
|
|
def test_config_in_subdirectory_colon_command_line_issue2148(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
conftest_source = """
|
2016-12-20 18:22:18 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini('foo', 'foo')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makefile(
|
|
|
|
".ini",
|
|
|
|
**{"pytest": "[pytest]\nfoo = root", "subdir/pytest": "[pytest]\nfoo = subdir"}
|
|
|
|
)
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"conftest": conftest_source,
|
|
|
|
"subdir/conftest": conftest_source,
|
|
|
|
"subdir/test_foo": """
|
2016-12-20 18:22:18 +08:00
|
|
|
def test_foo(pytestconfig):
|
|
|
|
assert pytestconfig.getini('foo') == 'subdir'
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("subdir/test_foo.py::test_foo")
|
2016-12-20 18:22:18 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2013-09-30 19:14:14 +08:00
|
|
|
def test_notify_exception(testdir, capfd):
|
|
|
|
config = testdir.parseconfig()
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
raise ValueError(1)
|
2013-09-30 19:14:14 +08:00
|
|
|
config.notify_exception(excinfo)
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert "ValueError" in err
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2013-09-30 19:14:14 +08:00
|
|
|
def pytest_internalerror(self, excrepr):
|
|
|
|
return True
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-09-30 19:14:14 +08:00
|
|
|
config.pluginmanager.register(A())
|
|
|
|
config.notify_exception(excinfo)
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert not err
|
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2019-03-28 05:34:37 +08:00
|
|
|
def test_load_initial_conftest_last_ordering(testdir, _config_for_test):
|
|
|
|
pm = _config_for_test.pluginmanager
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class My(object):
|
2013-09-30 19:14:16 +08:00
|
|
|
def pytest_load_initial_conftests(self):
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
m = My()
|
|
|
|
pm.register(m)
|
2015-04-25 17:29:11 +08:00
|
|
|
hc = pm.hook.pytest_load_initial_conftests
|
2017-11-04 23:17:20 +08:00
|
|
|
values = hc._nonwrappers + hc._wrappers
|
2018-05-23 22:48:46 +08:00
|
|
|
expected = ["_pytest.config", "test_config", "_pytest.capture"]
|
2017-11-04 23:17:20 +08:00
|
|
|
assert [x.function.__module__ for x in values] == expected
|
2016-06-20 20:45:13 +08:00
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2017-01-13 00:23:09 +08:00
|
|
|
def test_get_plugin_specs_as_list():
|
|
|
|
from _pytest.config import _get_plugin_specs_as_list
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-01-13 00:23:09 +08:00
|
|
|
with pytest.raises(pytest.UsageError):
|
2018-05-23 22:48:46 +08:00
|
|
|
_get_plugin_specs_as_list({"foo"})
|
2017-01-13 00:23:09 +08:00
|
|
|
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"]
|
2017-01-13 00:23:09 +08:00
|
|
|
|
|
|
|
|
2018-08-05 03:35:24 +08:00
|
|
|
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)
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed *"])
|
2018-08-05 03:35:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRootdir(object):
|
2015-02-27 04:56:44 +08:00
|
|
|
def test_simple_noini(self, tmpdir):
|
|
|
|
assert get_common_ancestor([tmpdir]) == tmpdir
|
2016-06-21 00:47:12 +08:00
|
|
|
a = tmpdir.mkdir("a")
|
|
|
|
assert get_common_ancestor([a, tmpdir]) == tmpdir
|
|
|
|
assert get_common_ancestor([tmpdir, a]) == tmpdir
|
2015-02-27 04:56:44 +08:00
|
|
|
with tmpdir.as_cwd():
|
|
|
|
assert get_common_ancestor([]) == tmpdir
|
2018-05-23 22:48:46 +08:00
|
|
|
no_path = tmpdir.join("does-not-exist")
|
2016-06-21 00:47:12 +08:00
|
|
|
assert get_common_ancestor([no_path]) == tmpdir
|
2018-05-23 22:48:46 +08:00
|
|
|
assert get_common_ancestor([no_path.join("a")]) == tmpdir
|
2015-02-27 04:56:44 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
|
|
|
|
def test_with_ini(self, tmpdir, name):
|
|
|
|
inifile = tmpdir.join(name)
|
2018-09-02 08:58:48 +08:00
|
|
|
inifile.write("[pytest]\n" if name != "setup.cfg" else "[tool:pytest]\n")
|
2015-02-27 04:56:44 +08:00
|
|
|
|
|
|
|
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
|
2017-07-17 07:25:08 +08:00
|
|
|
rootdir, inifile, inicfg = determine_setup(None, [b, a])
|
2015-02-27 04:56:44 +08:00
|
|
|
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):
|
2015-02-27 04:56:44 +08:00
|
|
|
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 == {}
|
|
|
|
|
2016-08-07 04:58:17 +08:00
|
|
|
def test_nothing(self, tmpdir, monkeypatch):
|
|
|
|
monkeypatch.chdir(str(tmpdir))
|
2015-02-27 04:56:44 +08:00
|
|
|
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
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2016-08-18 09:39:23 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestOverrideIniArgs(object):
|
2016-06-25 22:12:42 +08:00
|
|
|
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
|
|
|
|
def test_override_ini_names(self, testdir, name):
|
2018-09-02 08:58:48 +08:00
|
|
|
section = "[pytest]" if name != "setup.cfg" else "[tool:pytest]"
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join(name).write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2018-09-02 08:58:48 +08:00
|
|
|
{section}
|
|
|
|
custom = 1.0""".format(
|
|
|
|
section=section
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2016-06-25 22:12:42 +08:00
|
|
|
def pytest_addoption(parser):
|
2018-05-23 22:48:46 +08:00
|
|
|
parser.addini("custom", "")"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-25 22:12:42 +08:00
|
|
|
def test_pass(pytestconfig):
|
|
|
|
ini_val = pytestconfig.getini("custom")
|
2018-05-23 22:48:46 +08:00
|
|
|
print('\\ncustom_option:%s\\n' % ini_val)"""
|
|
|
|
)
|
2016-06-25 22:12:42 +08:00
|
|
|
|
|
|
|
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"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest(
|
|
|
|
"--override-ini", "custom=2.0", "--override-ini=custom=3.0", "-s"
|
|
|
|
)
|
2016-06-25 22:12:42 +08:00
|
|
|
assert result.ret == 0
|
2016-07-24 00:09:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["custom_option:3.0"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
|
|
|
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"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
|
|
|
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
|
2018-01-24 07:13:20 +08:00
|
|
|
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))
|
2018-01-24 07:13:20 +08:00
|
|
|
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",
|
|
|
|
]
|
|
|
|
)
|
2016-08-07 04:58:17 +08:00
|
|
|
|
2016-12-01 20:20:42 +08:00
|
|
|
def test_override_ini_usage_error_bad_style(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2016-12-01 20:20:42 +08:00
|
|
|
[pytest]
|
|
|
|
xdist_strict=False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--override-ini", "xdist_strict True", "-s")
|
2016-12-01 20:20:42 +08:00
|
|
|
result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"])
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("with_ini", [True, False])
|
2017-02-09 08:39:34 +08:00
|
|
|
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(
|
|
|
|
"""
|
2017-02-09 08:39:34 +08:00
|
|
|
[pytest]
|
|
|
|
python_files=test_*.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
unittest_ini_handle="""
|
2017-02-09 08:39:34 +08:00
|
|
|
def test():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--override-ini", "python_files=unittest_*.py")
|
2017-02-09 08:39:34 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed in*"])
|
|
|
|
|
2016-08-07 04:58:17 +08:00
|
|
|
def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch):
|
|
|
|
monkeypatch.chdir(str(tmpdir))
|
2016-06-21 00:47:12 +08:00
|
|
|
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"]))
|
2016-06-21 00:47:12 +08:00
|
|
|
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"])
|
2016-06-21 00:47:12 +08:00
|
|
|
assert rootdir == tmpdir
|
|
|
|
assert inifile is None
|
2017-10-11 01:03:15 +08:00
|
|
|
|
2019-03-28 05:34:37 +08:00
|
|
|
def test_addopts_before_initini(self, monkeypatch, _config_for_test):
|
2018-05-23 22:48:46 +08:00
|
|
|
cache_dir = ".custom_cache"
|
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2017-10-11 01:03:15 +08:00
|
|
|
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
|
|
|
|
2019-03-28 05:34:37 +08:00
|
|
|
def test_addopts_from_env_not_concatenated(self, monkeypatch, _config_for_test):
|
2018-12-09 21:44:45 +08:00
|
|
|
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
|
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2019-01-18 05:06:45 +08:00
|
|
|
with pytest.raises(UsageError) as excinfo:
|
2018-12-09 21:44:45 +08:00
|
|
|
config._preparse(["cache_dir=ignored"], addopts=True)
|
2019-01-18 05:06:45 +08:00
|
|
|
assert (
|
|
|
|
"error: argument -o/--override-ini: expected one argument (via PYTEST_ADDOPTS)"
|
|
|
|
in excinfo.value.args[0]
|
|
|
|
)
|
2018-12-09 21:44:45 +08:00
|
|
|
|
|
|
|
def test_addopts_from_ini_not_concatenated(self, testdir):
|
|
|
|
"""addopts from ini should not take values from normal args (#4265)."""
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts=-o
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("cache_dir=ignored")
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
2019-01-18 05:06:45 +08:00
|
|
|
"%s: error: argument -o/--override-ini: expected one argument (via addopts config)"
|
2018-12-09 21:44:45 +08:00
|
|
|
% (testdir.request.config._parser.optparser.prog,)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert result.ret == _pytest.main.EXIT_USAGEERROR
|
|
|
|
|
2019-03-28 05:34:37 +08:00
|
|
|
def test_override_ini_does_not_contain_paths(self, _config_for_test):
|
2018-01-24 07:13:20 +08:00
|
|
|
"""Check that -o no longer swallows all options after it (#3103)"""
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2018-05-23 22:48:46 +08:00
|
|
|
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
|
|
|
|
assert config._override_ini == ["cache_dir=/cache"]
|
2018-01-24 07:13:20 +08:00
|
|
|
|
|
|
|
def test_multiple_override_ini_options(self, testdir, request):
|
2018-01-23 03:58:04 +08:00
|
|
|
"""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": """
|
2018-01-24 07:13:20 +08:00
|
|
|
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": """
|
2018-01-24 07:13:20 +08:00
|
|
|
def test(pytestconfig):
|
|
|
|
assert pytestconfig.getini('foo') == '1'
|
|
|
|
assert pytestconfig.getini('bar') == '0'
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_bar.py": """
|
2018-01-24 07:13:20 +08:00
|
|
|
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 *="])
|
2019-01-18 05:06:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_help_via_addopts(testdir):
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts = --unknown-option-should-allow-for-help --help
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"usage: *",
|
|
|
|
"positional arguments:",
|
|
|
|
# Displays full/default help.
|
|
|
|
"to see available markers type: pytest --markers",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_help_and_version_after_argument_error(testdir):
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
def validate(arg):
|
|
|
|
raise argparse.ArgumentTypeError("argerror")
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup('cov')
|
|
|
|
group.addoption(
|
|
|
|
"--invalid-option-should-allow-for-help",
|
|
|
|
type=validate,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts = --invalid-option-should-allow-for-help
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--help")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"usage: *",
|
|
|
|
"positional arguments:",
|
|
|
|
"NOTE: displaying only minimal help due to UsageError.",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ERROR: usage: *",
|
|
|
|
"%s: error: argument --invalid-option-should-allow-for-help: expected one argument"
|
|
|
|
% (testdir.request.config._parser.optparser.prog,),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
# Does not display full/default help.
|
|
|
|
assert "to see available markers type: pytest --markers" not in result.stdout.lines
|
|
|
|
assert result.ret == EXIT_USAGEERROR
|
|
|
|
|
|
|
|
result = testdir.runpytest("--version")
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*pytest*{}*imported from*".format(pytest.__version__)]
|
|
|
|
)
|
|
|
|
assert result.ret == EXIT_USAGEERROR
|
2019-03-20 09:25:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_does_not_load_blocked_plugin_from_args(testdir):
|
|
|
|
"""This tests that pytest's config setup handles "-p no:X"."""
|
|
|
|
p = testdir.makepyfile("def test(capfd): pass")
|
2019-03-20 09:27:47 +08:00
|
|
|
result = testdir.runpytest(str(p), "-pno:capture")
|
2019-03-20 09:25:35 +08:00
|
|
|
result.stdout.fnmatch_lines(["E fixture 'capfd' not found"])
|
|
|
|
assert result.ret == EXIT_TESTSFAILED
|
|
|
|
|
|
|
|
result = testdir.runpytest(str(p), "-pno:capture", "-s")
|
|
|
|
result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"])
|
|
|
|
assert result.ret == EXIT_USAGEERROR
|
2019-03-21 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"plugin",
|
|
|
|
[
|
|
|
|
x
|
|
|
|
for x in _pytest.config.default_plugins
|
2019-03-28 02:10:33 +08:00
|
|
|
if x not in _pytest.config.essential_plugins
|
2019-03-21 11:50:51 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_config_blocked_default_plugins(testdir, plugin):
|
|
|
|
if plugin == "debugging":
|
2019-03-28 02:10:33 +08:00
|
|
|
# Fixed in xdist master (after 1.27.0).
|
2019-03-21 11:50:51 +08:00
|
|
|
# https://github.com/pytest-dev/pytest-xdist/pull/422
|
|
|
|
try:
|
|
|
|
import xdist # noqa: F401
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
pytest.skip("does not work with xdist currently")
|
|
|
|
|
|
|
|
p = testdir.makepyfile("def test(): pass")
|
|
|
|
result = testdir.runpytest(str(p), "-pno:%s" % plugin)
|
|
|
|
assert result.ret == EXIT_OK
|
2019-03-28 02:10:33 +08:00
|
|
|
if plugin != "terminal":
|
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
|
|
|
|
|
|
|
if plugin != "terminal": # fails to report due to its options being used elsewhere.
|
|
|
|
p = testdir.makepyfile("def test(): assert 0")
|
|
|
|
result = testdir.runpytest(str(p), "-pno:%s" % plugin)
|
|
|
|
assert result.ret == EXIT_TESTSFAILED
|
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in *"])
|