2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
from _pytest.config import getcfg
|
2010-10-28 01:35:27 +08:00
|
|
|
|
|
|
|
class TestParseIni:
|
2011-11-08 02:08:41 +08:00
|
|
|
def test_getcfg_and_config(self, testdir, tmpdir):
|
2010-10-28 01:35:27 +08:00
|
|
|
sub = tmpdir.mkdir("sub")
|
|
|
|
sub.chdir()
|
|
|
|
tmpdir.join("setup.cfg").write(py.code.Source("""
|
|
|
|
[pytest]
|
|
|
|
name = value
|
|
|
|
"""))
|
|
|
|
cfg = getcfg([sub], ["setup.cfg"])
|
|
|
|
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'
|
|
|
|
|
2011-09-26 05:26:49 +08:00
|
|
|
def test_getcfg_empty_path(self, tmpdir):
|
2014-01-18 19:39:16 +08:00
|
|
|
getcfg([''], ['setup.cfg']) #happens on py.test ""
|
2011-09-26 05:26:49 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def test_append_parse_args(self, testdir, tmpdir):
|
2010-10-28 01:35:27 +08:00
|
|
|
tmpdir.join("setup.cfg").write(py.code.Source("""
|
|
|
|
[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)
|
2010-10-28 01:35:27 +08:00
|
|
|
assert config.option.verbose
|
2011-11-08 02:08:41 +08:00
|
|
|
#config = testdir.Config()
|
|
|
|
#args = [tmpdir,]
|
|
|
|
#config._preparse(args, addopts=False)
|
|
|
|
#assert len(args) == 1
|
2010-10-28 01:35:27 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"*tox.ini:2*requires*9.0*actual*"
|
|
|
|
])
|
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
|
2010-11-07 23:10:22 +08:00
|
|
|
def test_ini_names(self, testdir, name):
|
|
|
|
testdir.tmpdir.join(name).write(py.std.textwrap.dedent("""
|
|
|
|
[pytest]
|
|
|
|
minversion = 1.0
|
|
|
|
"""))
|
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
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--confcutdir=.")
|
|
|
|
assert result.ret == 0
|
2011-11-08 02:08:41 +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
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_config_getvalue_honours_conftest(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="x=1")
|
|
|
|
testdir.mkdir("sub").join("conftest.py").write("x=2 ; y = 3")
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
o = testdir.tmpdir
|
|
|
|
assert config.getvalue("x") == 1
|
|
|
|
assert config.getvalue("x", o.join('sub')) == 2
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(KeyError, "config.getvalue('y')")
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(str(o.join('sub')))
|
2009-02-27 18:18:27 +08:00
|
|
|
assert config.getvalue("x") == 2
|
|
|
|
assert config.getvalue("y") == 3
|
|
|
|
assert config.getvalue("x", o) == 1
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(KeyError, 'config.getvalue("y", o)')
|
2009-02-27 18:18:27 +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
|
|
|
|
config.option.hello = None
|
2010-12-07 19:18:24 +08:00
|
|
|
try:
|
|
|
|
config.getvalueorskip('hello')
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
frame = excinfo.traceback[-2].frame
|
|
|
|
assert frame.code.name == "getvalueorskip"
|
|
|
|
assert frame.eval("__tracebackhide__")
|
2009-03-02 19:14:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_config_overwrite(self, testdir):
|
|
|
|
o = testdir.tmpdir
|
|
|
|
o.ensure("conftest.py").write("x=1")
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig(str(o))
|
2009-02-27 18:18:27 +08:00
|
|
|
assert config.getvalue('x') == 1
|
|
|
|
config.option.x = 2
|
|
|
|
assert config.getvalue('x') == 2
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig([str(o)])
|
2009-02-27 18:18:27 +08:00
|
|
|
assert config.getvalue('x') == 1
|
|
|
|
|
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)
|
2010-11-01 00:41:58 +08:00
|
|
|
assert config._getconftest_pathlist('notexist') is None
|
|
|
|
pl = config._getconftest_pathlist('pathlist')
|
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 == []
|
|
|
|
|
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"]
|
|
|
|
|
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'],
|
|
|
|
['--tb=long'], ['--fulltrace'], ['--nomagic'],
|
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"
|
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
2011-01-13 02:39:36 +08:00
|
|
|
class dist:
|
|
|
|
pass
|
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
|
|
|
|
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"
|
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
|
|
|
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")
|
|
|
|
assert plugin == -1
|
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")
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*pytest*",
|
|
|
|
"*-h*",
|
|
|
|
])
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2012-11-12 17:15:43 +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)
|
|
|
|
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
|
|
|
|
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):
|
|
|
|
from _pytest.config import get_plugin_manager
|
|
|
|
pm = get_plugin_manager()
|
|
|
|
class My:
|
|
|
|
def pytest_load_initial_conftests(self):
|
|
|
|
pass
|
|
|
|
m = My()
|
|
|
|
pm.register(m)
|
|
|
|
l = pm.listattr("pytest_load_initial_conftests")
|
|
|
|
assert l[-1].__module__ == "_pytest.capture"
|
|
|
|
assert l[-2] == m.pytest_load_initial_conftests
|
|
|
|
assert l[-3].__module__ == "_pytest.config"
|
|
|
|
|
|
|
|
|