2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2015-02-27 04:56:44 +08:00
|
|
|
from _pytest.config import getcfg, get_common_ancestor, determine_setup
|
2015-07-05 01:42:22 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
2010-10-28 01:35:27 +08:00
|
|
|
|
|
|
|
class TestParseIni:
|
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):
|
2010-11-07 23:10:22 +08:00
|
|
|
testdir.tmpdir.join(name).write(py.std.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")
|
|
|
|
sub.join("tox.ini").write(py.std.textwrap.dedent("""
|
|
|
|
[pytest]
|
|
|
|
minversion = 2.0
|
|
|
|
"""))
|
|
|
|
testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent("""
|
|
|
|
[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-04-20 01:27:37 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestConfigCmdlineParsing:
|
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
|
|
|
|
""")
|
|
|
|
testdir.makefile(".cfg", custom = """
|
|
|
|
[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
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestConfigAPI:
|
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()
|
2010-11-06 06:37:31 +08:00
|
|
|
l = []
|
|
|
|
config.trace.root.setwriter(l.append)
|
|
|
|
config.trace("hello")
|
|
|
|
assert len(l) == 1
|
2011-07-15 01:11:50 +08:00
|
|
|
assert l[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')")
|
|
|
|
|
2013-10-01 20:20:20 +08:00
|
|
|
@pytest.mark.skipif('sys.version_info[:2] not in [(2, 6), (2, 7)]')
|
|
|
|
def test_config_getoption_unicode(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--hello', type='string')
|
|
|
|
""")
|
|
|
|
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,
|
2010-04-28 03:13:09 +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()
|
|
|
|
l = config.getini("paths")
|
|
|
|
assert len(l) == 2
|
|
|
|
assert l[0] == p.dirpath('hello')
|
|
|
|
assert l[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()
|
|
|
|
l = config.getini("args")
|
|
|
|
assert len(l) == 3
|
|
|
|
assert l == ["123", "123 hello", "this"]
|
|
|
|
l = config.getini("a2")
|
|
|
|
assert l == list("123")
|
|
|
|
|
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()
|
|
|
|
l = config.getini("xy")
|
|
|
|
assert len(l) == 2
|
|
|
|
assert l == ["123 345", "second line"]
|
|
|
|
l = config.getini("a2")
|
|
|
|
assert l == []
|
|
|
|
|
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()
|
|
|
|
l = config.getini("xy")
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l == ["123"]
|
|
|
|
config.addinivalue_line("xy", "456")
|
|
|
|
l = config.getini("xy")
|
|
|
|
assert len(l) == 2
|
|
|
|
assert l == ["123", "456"]
|
|
|
|
|
|
|
|
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")
|
|
|
|
l = config.getini("xy")
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l == ["456"]
|
|
|
|
config.addinivalue_line("xy", "123")
|
|
|
|
l = config.getini("xy")
|
|
|
|
assert len(l) == 2
|
|
|
|
assert l == ["456", "123"]
|
|
|
|
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
class TestConfigFromdictargs:
|
|
|
|
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
|
|
|
|
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")
|
2010-01-03 00:17:13 +08:00
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-06-22 18:42:11 +08:00
|
|
|
class Dist:
|
|
|
|
project_name = 'spam'
|
|
|
|
version = '1.0'
|
|
|
|
def _get_metadata(self, name):
|
|
|
|
return ['foo.txt,sha256=abc,123']
|
2010-01-03 00:17:13 +08:00
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
2016-06-22 18:42:11 +08:00
|
|
|
dist = Dist()
|
2010-01-03 00:17:13 +08:00
|
|
|
def load(self):
|
|
|
|
class PseudoPlugin:
|
|
|
|
x = 42
|
|
|
|
return PseudoPlugin()
|
|
|
|
return iter([EntryPoint()])
|
|
|
|
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")
|
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-07-14 19:42:29 +08:00
|
|
|
class Dist:
|
|
|
|
project_name = 'spam'
|
|
|
|
version = '1.0'
|
|
|
|
def _get_metadata(self, name):
|
|
|
|
return ['foo.txt,sha256=abc,123']
|
2016-05-23 14:13:41 +08:00
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
2016-07-14 19:42:29 +08:00
|
|
|
dist = Dist()
|
2016-05-23 14:13:41 +08:00
|
|
|
def load(self):
|
|
|
|
raise ImportError("Don't hide me!")
|
|
|
|
return iter([EntryPoint()])
|
|
|
|
|
|
|
|
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
testdir.parseconfig()
|
|
|
|
|
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch):
|
2014-01-18 19:31:33 +08:00
|
|
|
pkg_resources = pytest.importorskip("pkg_resources")
|
2010-12-06 23:54:42 +08:00
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
2016-06-22 18:42:11 +08:00
|
|
|
class Dist:
|
|
|
|
project_name = 'spam'
|
|
|
|
version = '1.0'
|
|
|
|
def _get_metadata(self, name):
|
|
|
|
return ['foo.txt,sha256=abc,123']
|
2010-12-06 23:54:42 +08:00
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
2016-06-22 18:42:11 +08:00
|
|
|
dist = Dist()
|
2010-12-06 23:54:42 +08:00
|
|
|
def load(self):
|
|
|
|
assert 0, "should not arrive here"
|
|
|
|
return iter([EntryPoint()])
|
|
|
|
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
|
|
|
|
config = testdir.parseconfig("-p", "no:mytestplugin")
|
|
|
|
plugin = config.pluginmanager.getplugin("mytestplugin")
|
2015-04-26 02:17:32 +08:00
|
|
|
assert plugin is None
|
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
|
|
|
|
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)
|
2015-08-26 09:08:05 +08:00
|
|
|
result.stdout.fnmatch_lines(['*rootdir: *myroot, inifile: '])
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def test_notify_exception(testdir, capfd):
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
excinfo = pytest.raises(ValueError, "raise ValueError(1)")
|
|
|
|
config.notify_exception(excinfo)
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert "ValueError" in err
|
|
|
|
class A:
|
|
|
|
def pytest_internalerror(self, excrepr):
|
|
|
|
return True
|
|
|
|
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):
|
2015-04-26 00:14:41 +08:00
|
|
|
from _pytest.config import get_config
|
|
|
|
pm = get_config().pluginmanager
|
2013-09-30 19:14:16 +08:00
|
|
|
class My:
|
|
|
|
def pytest_load_initial_conftests(self):
|
|
|
|
pass
|
|
|
|
m = My()
|
|
|
|
pm.register(m)
|
2015-04-25 17:29:11 +08:00
|
|
|
hc = pm.hook.pytest_load_initial_conftests
|
2015-04-25 19:38:29 +08:00
|
|
|
l = hc._nonwrappers + hc._wrappers
|
2016-06-20 20:45:13 +08:00
|
|
|
expected = [
|
|
|
|
"_pytest.config",
|
|
|
|
'test_config',
|
|
|
|
'_pytest.capture',
|
|
|
|
]
|
|
|
|
assert [x.function.__module__ for x in l] == expected
|
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
class TestWarning:
|
|
|
|
def test_warn_config(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
l = []
|
|
|
|
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":
|
|
|
|
l.append(1)
|
2014-03-12 05:10:17 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_proper(pytestconfig):
|
|
|
|
import conftest
|
|
|
|
assert conftest.l == [1]
|
|
|
|
""")
|
|
|
|
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")
|
2015-08-28 06:59:52 +08:00
|
|
|
assert result.parseoutcomes()["pytest-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("""
|
2015-08-28 06:59:52 +08:00
|
|
|
===*pytest-warning summary*===
|
2016-08-12 06:29:03 +08:00
|
|
|
*WT1*test_warn_on_test_item*:7 hello*
|
2014-03-12 05:10:17 +08:00
|
|
|
""")
|
2015-02-27 04:56:44 +08:00
|
|
|
|
|
|
|
class TestRootdir:
|
|
|
|
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
|
|
|
|
rootdir, inifile, inicfg = determine_setup(None, [b,a])
|
|
|
|
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
|
|
|
|
2016-06-25 22:12:42 +08:00
|
|
|
class TestOverrideIniArgs:
|
|
|
|
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
|
|
|
|
def test_override_ini_names(self, testdir, name):
|
|
|
|
testdir.tmpdir.join(name).write(py.std.textwrap.dedent("""
|
|
|
|
[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
|
|
|
|
|
|
|
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
|
2016-08-07 04:58:17 +08:00
|
|
|
|