2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2017-11-29 00:29:52 +08:00
|
|
|
import sys
|
2017-12-27 11:47:26 +08:00
|
|
|
import textwrap
|
2017-07-17 07:25:09 +08:00
|
|
|
import pytest
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2017-07-29 03:03:07 +08:00
|
|
|
from _pytest.config import getcfg, get_common_ancestor, determine_setup, _iter_rewritable_modules
|
2015-07-05 01:42:22 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
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):
|
2016-08-17 08:30:07 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('section, filename',
|
|
|
|
[('pytest', 'pytest.ini'), ('tool:pytest', 'setup.cfg')])
|
|
|
|
def test_getcfg_and_config(self, testdir, tmpdir, section, filename):
|
2010-10-28 01:35:27 +08:00
|
|
|
sub = tmpdir.mkdir("sub")
|
|
|
|
sub.chdir()
|
2016-08-17 08:30:07 +08:00
|
|
|
tmpdir.join(filename).write(_pytest._code.Source("""
|
|
|
|
[{section}]
|
2010-10-28 01:35:27 +08:00
|
|
|
name = value
|
2016-08-17 08:30:07 +08:00
|
|
|
""".format(section=section)))
|
|
|
|
rootdir, inifile, cfg = getcfg([sub])
|
2010-10-28 01:35:27 +08:00
|
|
|
assert cfg['name'] == "value"
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(sub)
|
2010-10-28 01:35:27 +08:00
|
|
|
assert config.inicfg['name'] == 'value'
|
|
|
|
|
2016-08-17 08:30:07 +08:00
|
|
|
def test_getcfg_empty_path(self):
|
|
|
|
"""correctly handle zero length arguments (a la pytest '')"""
|
|
|
|
getcfg([''])
|
2011-09-26 05:26:49 +08:00
|
|
|
|
2015-01-29 18:52:01 +08:00
|
|
|
def test_append_parse_args(self, testdir, tmpdir, monkeypatch):
|
|
|
|
monkeypatch.setenv('PYTEST_ADDOPTS', '--color no -rs --tb="short"')
|
2016-08-17 08:30:07 +08:00
|
|
|
tmpdir.join("pytest.ini").write(_pytest._code.Source("""
|
2010-10-28 01:35:27 +08:00
|
|
|
[pytest]
|
2010-11-01 15:55:14 +08:00
|
|
|
addopts = --verbose
|
2010-10-28 01:35:27 +08:00
|
|
|
"""))
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig(tmpdir)
|
2015-01-24 04:09:42 +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):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makefile('.ini', tox="""
|
2010-10-28 01:35:27 +08:00
|
|
|
[pytest]
|
|
|
|
minversion=9.0
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2010-10-28 01:35:27 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"*tox.ini:2*requires*9.0*actual*"
|
|
|
|
])
|
|
|
|
|
2016-08-17 08:30:07 +08:00
|
|
|
@pytest.mark.parametrize("section, name", [
|
|
|
|
('tool:pytest', 'setup.cfg'),
|
|
|
|
('pytest', 'tox.ini'),
|
|
|
|
('pytest', 'pytest.ini')],
|
|
|
|
)
|
|
|
|
def test_ini_names(self, testdir, name, section):
|
2017-12-27 11:47:26 +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
|
2016-08-17 08:30:07 +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")
|
2017-12-27 11:47:26 +08:00
|
|
|
sub.join("tox.ini").write(textwrap.dedent("""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 2.0
|
|
|
|
"""))
|
2017-12-27 11:47:26 +08:00
|
|
|
testdir.tmpdir.join("pytest.ini").write(textwrap.dedent("""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 1.5
|
|
|
|
"""))
|
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()
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
addopts = --qwe
|
|
|
|
""")
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("custom", "")
|
|
|
|
""")
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
custom = 0
|
|
|
|
""")
|
2017-07-17 07:25:08 +08:00
|
|
|
testdir.makefile(".cfg", custom="""
|
2014-06-27 23:42:37 +08:00
|
|
|
[pytest]
|
|
|
|
custom = 1
|
|
|
|
""")
|
|
|
|
config = testdir.parseconfig("-c", "custom.cfg")
|
|
|
|
assert config.getini("custom") == "1"
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2016-04-20 01:27:37 +08:00
|
|
|
def test_absolute_win32_path(self, testdir):
|
|
|
|
temp_cfg_file = testdir.makefile(".cfg", custom="""
|
|
|
|
[pytest]
|
|
|
|
addopts = --version
|
|
|
|
""")
|
|
|
|
from os.path import normpath
|
|
|
|
temp_cfg_file = normpath(str(temp_cfg_file))
|
|
|
|
ret = pytest.main("-c " + temp_cfg_file)
|
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello", "-X", dest="hello")
|
|
|
|
""")
|
|
|
|
config = testdir.parseconfig("--hello=this")
|
|
|
|
for x in ("hello", "--hello", "-X"):
|
|
|
|
assert config.getoption(x) == "this"
|
|
|
|
pytest.raises(ValueError, "config.getoption('qweqwe')")
|
|
|
|
|
2017-10-10 13:54:56 +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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2017-03-21 09:01:22 +08:00
|
|
|
parser.addoption('--hello', type=str)
|
2013-10-01 20:20:20 +08:00
|
|
|
""")
|
|
|
|
config = testdir.parseconfig('--hello=this')
|
|
|
|
assert config.getoption('hello') == 'this'
|
|
|
|
|
2009-03-02 19:14:59 +08:00
|
|
|
def test_config_getvalueorskip(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(pytest.skip.Exception,
|
2017-07-17 07:25:07 +08:00
|
|
|
"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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello")
|
|
|
|
""")
|
|
|
|
config = testdir.parseconfig()
|
2014-06-16 17:27:32 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception):
|
|
|
|
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):
|
|
|
|
config.getvalue('x')
|
|
|
|
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)
|
2014-04-03 04:30:45 +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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("myname", "my new ini value")
|
|
|
|
""")
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
myname=hello
|
|
|
|
""")
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
val = config.getini("myname")
|
|
|
|
assert val == "hello"
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ValueError, config.getini, 'other')
|
2010-11-01 00:41:58 +08:00
|
|
|
|
|
|
|
def test_addini_pathlist(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("paths", "my new ini value", type="pathlist")
|
|
|
|
parser.addini("abc", "abc value")
|
|
|
|
""")
|
|
|
|
p = testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
paths=hello world/sub.py
|
|
|
|
""")
|
|
|
|
config = testdir.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("paths")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values[0] == p.dirpath('hello')
|
|
|
|
assert values[1] == p.dirpath('world/sub.py')
|
2010-11-18 05:12:16 +08:00
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("args", "new args", type="args")
|
|
|
|
parser.addini("a2", "", "args", default="1 2 3".split())
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeini("""
|
2010-11-05 06:21:26 +08:00
|
|
|
[pytest]
|
|
|
|
args=123 "123 hello" "this"
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
|
|
|
parser.addini("a2", "", "linelist")
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeini("""
|
2010-11-06 06:37:31 +08:00
|
|
|
[pytest]
|
|
|
|
xy= 123 345
|
|
|
|
second line
|
|
|
|
""")
|
|
|
|
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
|
|
|
|
2016-02-15 05:27:48 +08:00
|
|
|
@pytest.mark.parametrize('str_val, bool_val',
|
|
|
|
[('True', True), ('no', False), ('no-ini', True)])
|
|
|
|
def test_addini_bool(self, testdir, str_val, bool_val):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("strip", "", type="bool", default=True)
|
|
|
|
""")
|
|
|
|
if str_val != 'no-ini':
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
strip=%s
|
|
|
|
""" % str_val)
|
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeini("""
|
2011-11-12 06:56:08 +08:00
|
|
|
[pytest]
|
|
|
|
xy= 123
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.parseconfig('--confcutdir', testdir.tmpdir.join('file').ensure(file=1))
|
|
|
|
with pytest.raises(pytest.UsageError):
|
|
|
|
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'))
|
|
|
|
|
2017-07-29 03:03:07 +08:00
|
|
|
@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):
|
|
|
|
assert list(_iter_rewritable_modules(['/'.join(names)])) == expected
|
|
|
|
|
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
|
|
|
|
option_dict = {
|
2015-09-25 08:52:53 +08:00
|
|
|
'verbose': 444,
|
2015-09-25 05:13:36 +08:00
|
|
|
'foo': 'bar',
|
2015-09-25 08:52:53 +08:00
|
|
|
'capture': 'no',
|
2015-09-25 05:13:36 +08:00
|
|
|
}
|
|
|
|
args = ['a', 'b']
|
|
|
|
|
|
|
|
config = Config.fromdictargs(option_dict, args)
|
|
|
|
with pytest.raises(AssertionError):
|
2015-09-25 08:52:53 +08:00
|
|
|
config.parse(['should refuse to parse again'])
|
|
|
|
assert config.option.verbose == 444
|
2015-09-25 05:13:36 +08:00
|
|
|
assert config.option.foo == 'bar'
|
2015-09-25 08:52:53 +08:00
|
|
|
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
|
|
|
|
option_dict = {}
|
2015-09-25 08:52:53 +08:00
|
|
|
args = ['-vvvv', '-s', 'a', 'b']
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
config = Config.fromdictargs(option_dict, args)
|
|
|
|
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
|
2015-09-25 08:52:53 +08:00
|
|
|
assert config.option.capture == 'no'
|
|
|
|
|
|
|
|
def test_inifilename(self, tmpdir):
|
2015-11-27 22:43:01 +08:00
|
|
|
tmpdir.join("foo/bar.ini").ensure().write(_pytest._code.Source("""
|
2015-09-25 08:52:53 +08:00
|
|
|
[pytest]
|
|
|
|
name = value
|
|
|
|
"""))
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
from _pytest.config import Config
|
2015-09-25 08:52:53 +08:00
|
|
|
inifile = '../../foo/bar.ini'
|
2015-09-25 05:13:36 +08:00
|
|
|
option_dict = {
|
|
|
|
'inifilename': inifile,
|
2015-09-25 08:52:53 +08:00
|
|
|
'capture': 'no',
|
2015-09-25 05:13:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-25 08:52:53 +08:00
|
|
|
cwd = tmpdir.join('a/b')
|
2015-11-27 22:43:01 +08:00
|
|
|
cwd.join('pytest.ini').ensure().write(_pytest._code.Source("""
|
2015-10-10 00:57:40 +08:00
|
|
|
[pytest]
|
|
|
|
name = wrong-value
|
|
|
|
should_not_be_set = true
|
|
|
|
"""))
|
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
|
2015-09-25 08:52:53 +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
|
2015-09-25 08:52:53 +08:00
|
|
|
assert config.inicfg.get('name') == 'value'
|
2015-10-10 00:57:40 +08:00
|
|
|
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
|
|
|
|
path = testdir.makepyfile("""
|
|
|
|
def test_f1(): assert 0
|
|
|
|
def test_f2(): assert 0
|
|
|
|
""")
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'],
|
2016-06-25 17:27:10 +08:00
|
|
|
['--tb=long'], ['--fulltrace'],
|
2009-02-28 05:32:49 +08:00
|
|
|
['--traceconfig'], ['-v'], ['-v', '-v']):
|
|
|
|
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")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-01-03 00:17:13 +08:00
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2016-06-22 18:42:11 +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):
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2010-01-03 00:17:13 +08:00
|
|
|
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
|
|
|
|
testdir.makeconftest("""
|
|
|
|
pytest_plugins = "mytestplugin",
|
|
|
|
""")
|
|
|
|
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")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-05-23 14:13:41 +08:00
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2016-07-14 19:42:29 +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):
|
|
|
|
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()])
|
|
|
|
|
|
|
|
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
testdir.parseconfig()
|
|
|
|
|
|
|
|
|
2017-11-29 07:29:58 +08:00
|
|
|
@pytest.mark.parametrize('block_it', [True, False])
|
|
|
|
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")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-11-29 07:29:58 +08:00
|
|
|
plugin_module_placeholder = object()
|
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Dist(object):
|
2016-06-22 18:42:11 +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):
|
|
|
|
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
|
|
|
|
2010-12-06 23:54:42 +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
|
|
|
|
assert config.pluginmanager.get_plugin('mytestplugin') is None
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
2010-12-07 19:14:12 +08:00
|
|
|
def test_cmdline_processargs_simple(testdir):
|
|
|
|
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")
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2010-12-07 19:14:12 +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"""
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
addopts = --invalid-option
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"*error: unrecognized arguments: --invalid-option*",
|
|
|
|
"* inifile: %s*" % testdir.tmpdir.join('tox.ini'),
|
|
|
|
"* rootdir: %s*" % testdir.tmpdir,
|
|
|
|
])
|
2012-11-12 17:15:43 +08:00
|
|
|
|
2015-08-26 09:08:05 +08:00
|
|
|
|
|
|
|
@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
|
|
|
|
root = testdir.tmpdir.mkdir('myroot')
|
|
|
|
d1 = root.mkdir('dir1')
|
|
|
|
d2 = root.mkdir('dir2')
|
|
|
|
for i, arg in enumerate(args):
|
|
|
|
if arg == 'dir1':
|
|
|
|
args[i] = d1
|
|
|
|
elif arg == 'dir2':
|
|
|
|
args[i] = d2
|
2016-06-21 00:47:12 +08:00
|
|
|
with root.as_cwd():
|
|
|
|
result = testdir.runpytest(*args)
|
2017-03-06 03:44:13 +08:00
|
|
|
result.stdout.fnmatch_lines(['*rootdir: *myroot, inifile:'])
|
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):
|
|
|
|
conftest_source = '''
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini('foo', 'foo')
|
|
|
|
'''
|
|
|
|
|
|
|
|
testdir.makefile('.ini', **{
|
|
|
|
'pytest': '[pytest]\nfoo = root',
|
|
|
|
'subdir/pytest': '[pytest]\nfoo = subdir',
|
|
|
|
})
|
|
|
|
|
|
|
|
testdir.makepyfile(**{
|
|
|
|
'conftest': conftest_source,
|
|
|
|
'subdir/conftest': conftest_source,
|
|
|
|
'subdir/test_foo': '''
|
|
|
|
def test_foo(pytestconfig):
|
|
|
|
assert pytestconfig.getini('foo') == 'subdir'
|
|
|
|
'''})
|
|
|
|
|
|
|
|
result = testdir.runpytest('subdir/test_foo.py::test_foo')
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2013-09-30 19:14:14 +08:00
|
|
|
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
|
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
|
|
|
|
|
|
|
def test_load_initial_conftest_last_ordering(testdir):
|
2017-07-17 07:25:09 +08:00
|
|
|
from _pytest.config import get_config
|
2015-04-26 00:14:41 +08:00
|
|
|
pm = get_config().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
|
2016-06-20 20:45:13 +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
|
|
|
|
with pytest.raises(pytest.UsageError):
|
|
|
|
_get_plugin_specs_as_list(set(['foo']))
|
|
|
|
with pytest.raises(pytest.UsageError):
|
|
|
|
_get_plugin_specs_as_list(dict())
|
|
|
|
|
|
|
|
assert _get_plugin_specs_as_list(None) == []
|
|
|
|
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-02-17 02:41:51 +08:00
|
|
|
class TestWarning(object):
|
2014-03-12 05:10:17 +08:00
|
|
|
def test_warn_config(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-03-12 05:10:17 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.warn("C1", "hello")
|
|
|
|
def pytest_logwarning(code, message):
|
2015-04-27 21:06:47 +08:00
|
|
|
if message == "hello" and code == "C1":
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2014-03-12 05:10:17 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_proper(pytestconfig):
|
|
|
|
import conftest
|
2017-11-04 23:17:20 +08:00
|
|
|
assert conftest.values == [1]
|
2014-03-12 05:10:17 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2016-08-12 06:29:03 +08:00
|
|
|
def test_warn_on_test_item_from_request(self, testdir, request):
|
2014-03-12 05:10:17 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(request):
|
|
|
|
request.node.warn("T1", "hello")
|
2016-08-12 06:29:03 +08:00
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
def test_hello(fix):
|
|
|
|
pass
|
|
|
|
""")
|
2016-07-05 21:22:27 +08:00
|
|
|
result = testdir.runpytest("--disable-pytest-warnings")
|
2017-03-08 07:47:43 +08:00
|
|
|
assert result.parseoutcomes()["warnings"] > 0
|
2016-07-05 21:22:27 +08:00
|
|
|
assert "hello" not in result.stdout.str()
|
2015-04-27 21:06:47 +08:00
|
|
|
|
2016-07-05 21:22:27 +08:00
|
|
|
result = testdir.runpytest()
|
2014-03-12 05:10:17 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
2017-03-08 07:47:43 +08:00
|
|
|
===*warnings summary*===
|
2017-03-17 08:55:03 +08:00
|
|
|
*test_warn_on_test_item_from_request.py::test_hello*
|
2017-03-08 07:47:43 +08:00
|
|
|
*hello*
|
2014-03-12 05:10:17 +08:00
|
|
|
""")
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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
|
2016-06-21 00:47:12 +08:00
|
|
|
no_path = tmpdir.join('does-not-exist')
|
|
|
|
assert get_common_ancestor([no_path]) == tmpdir
|
|
|
|
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)
|
|
|
|
inifile.write("[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
|
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())
|
|
|
|
def test_pytestini_overides_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 == {}
|
|
|
|
|
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):
|
2017-12-27 11:47:26 +08:00
|
|
|
testdir.tmpdir.join(name).write(textwrap.dedent("""
|
2016-06-25 22:12:42 +08:00
|
|
|
[pytest]
|
2016-07-24 00:09:42 +08:00
|
|
|
custom = 1.0"""))
|
2016-06-25 22:12:42 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
2016-07-24 00:09:42 +08:00
|
|
|
parser.addini("custom", "")""")
|
2016-06-25 22:12:42 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_pass(pytestconfig):
|
|
|
|
ini_val = pytestconfig.getini("custom")
|
2016-07-24 00:09:42 +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
|
|
|
|
|
|
|
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"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
|
|
|
def test_override_ini_pathlist(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2016-07-24 00:09:42 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("paths", "my new ini value", type="pathlist")""")
|
2016-06-25 22:12:42 +08:00
|
|
|
testdir.makeini("""
|
2016-07-24 00:09:42 +08:00
|
|
|
[pytest]
|
|
|
|
paths=blah.py""")
|
2016-06-25 22:12:42 +08:00
|
|
|
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:
|
|
|
|
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):
|
|
|
|
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")
|
|
|
|
addini("custom_option_4", "", default=True, type="bool")""")
|
2016-06-25 22:12:42 +08:00
|
|
|
testdir.makeini("""
|
2016-07-24 00:09:42 +08:00
|
|
|
[pytest]
|
|
|
|
custom_option_1=custom_option_1
|
|
|
|
custom_option_2=custom_option_2""")
|
2016-06-25 22:12:42 +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))""")
|
|
|
|
result = testdir.runpytest(
|
|
|
|
"--override-ini", 'custom_option_1=fulldir=/tmp/user1',
|
|
|
|
'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):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
xdist_strict=False
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--override-ini", 'xdist_strict True', "-s")
|
|
|
|
result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"])
|
|
|
|
|
2017-02-09 08:39:34 +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:
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
python_files=test_*.py
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(unittest_ini_handle="""
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--override-ini", 'python_files=unittest_*.py')
|
|
|
|
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
|
|
|
|
|
|
|
|
@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():
|
|
|
|
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, testdir, tmpdir, monkeypatch):
|
2017-10-12 06:37:17 +08:00
|
|
|
cache_dir = '.custom_cache'
|
2017-10-11 01:03:15 +08:00
|
|
|
monkeypatch.setenv('PYTEST_ADDOPTS', '-o cache_dir=%s' % cache_dir)
|
|
|
|
from _pytest.config import get_config
|
|
|
|
config = get_config()
|
|
|
|
config._preparse([], addopts=True)
|
|
|
|
assert config._override_ini == [['cache_dir=%s' % cache_dir]]
|