2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2010-10-28 01:35:27 +08:00
|
|
|
from pytest.plugin.config import getcfg, Config
|
|
|
|
|
|
|
|
class TestParseIni:
|
|
|
|
def test_getcfg_and_config(self, tmpdir):
|
|
|
|
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"
|
|
|
|
config = Config()
|
|
|
|
config._preparse([sub])
|
|
|
|
assert config.inicfg['name'] == 'value'
|
|
|
|
|
|
|
|
def test_getvalue(self, tmpdir):
|
|
|
|
tmpdir.join("setup.cfg").write(py.code.Source("""
|
|
|
|
[pytest]
|
|
|
|
verbose = True
|
|
|
|
"""))
|
|
|
|
config = Config()
|
|
|
|
config._preparse([tmpdir])
|
|
|
|
assert config.option.verbose
|
|
|
|
|
|
|
|
def test_append_parse_args(self, tmpdir):
|
|
|
|
tmpdir.join("setup.cfg").write(py.code.Source("""
|
|
|
|
[pytest]
|
|
|
|
appendargs = --verbose
|
|
|
|
"""))
|
|
|
|
config = Config()
|
|
|
|
config.parse([tmpdir])
|
|
|
|
assert config.option.verbose
|
|
|
|
|
|
|
|
def test_tox_ini_wrong_version(self, testdir):
|
|
|
|
p = testdir.makefile('.ini', tox="""
|
|
|
|
[pytest]
|
|
|
|
minversion=9.0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"*tox.ini:2*requires*9.0*actual*"
|
|
|
|
])
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestConfigCmdlineParsing:
|
2009-03-02 19:14:59 +08:00
|
|
|
def test_parser_addoption_default_env(self, testdir, monkeypatch):
|
|
|
|
import os
|
|
|
|
config = testdir.Config()
|
2010-10-28 01:35:27 +08:00
|
|
|
config._preparse([testdir.tmpdir])
|
2009-10-27 17:03:11 +08:00
|
|
|
group = config._parser.getgroup("hello")
|
2009-03-02 19:14:59 +08:00
|
|
|
|
|
|
|
monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION1', 'True')
|
|
|
|
group.addoption("--option1", action="store_true")
|
|
|
|
assert group.options[0].default == True
|
|
|
|
|
|
|
|
monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION2', 'abc')
|
|
|
|
group.addoption("--option2", action="store", default="x")
|
|
|
|
assert group.options[1].default == "abc"
|
|
|
|
|
|
|
|
monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION3', '32')
|
|
|
|
group.addoption("--option3", action="store", type="int")
|
|
|
|
assert group.options[2].default == 32
|
|
|
|
|
|
|
|
group.addoption("--option4", action="store", type="int")
|
|
|
|
assert group.options[3].default == ("NO", "DEFAULT")
|
|
|
|
|
2009-03-17 14:10:40 +08:00
|
|
|
def test_parser_addoption_default_conftest(self, testdir, monkeypatch):
|
|
|
|
import os
|
2009-08-19 21:45:01 +08:00
|
|
|
testdir.makeconftest("option_verbose=True")
|
2009-03-17 14:10:40 +08:00
|
|
|
config = testdir.parseconfig()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert config.option.verbose
|
2009-03-17 14:10:40 +08:00
|
|
|
|
2009-12-30 01:02:54 +08:00
|
|
|
def test_parsing_again_fails(self, testdir):
|
|
|
|
config = testdir.reparseconfig([testdir.tmpdir])
|
2009-02-27 18:18:27 +08:00
|
|
|
py.test.raises(AssertionError, "config.parse([])")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
|
2009-03-17 20:42:40 +08:00
|
|
|
class TestConfigTmpdir:
|
|
|
|
def test_getbasetemp(self, testdir):
|
|
|
|
config = testdir.Config()
|
|
|
|
config.basetemp = "hello"
|
|
|
|
config.getbasetemp() == "hello"
|
|
|
|
|
|
|
|
def test_mktemp(self, testdir):
|
|
|
|
config = testdir.Config()
|
|
|
|
config.basetemp = testdir.mkdir("hello")
|
|
|
|
tmp = config.mktemp("world")
|
|
|
|
assert tmp.relto(config.basetemp) == "world"
|
|
|
|
tmp = config.mktemp("this", numbered=True)
|
|
|
|
assert tmp.relto(config.basetemp).startswith("this")
|
|
|
|
tmp2 = config.mktemp("this", numbered=True)
|
|
|
|
assert tmp2.relto(config.basetemp).startswith("this")
|
|
|
|
assert tmp2 != tmp
|
|
|
|
|
|
|
|
def test_reparse(self, testdir):
|
2009-12-30 01:02:54 +08:00
|
|
|
config2 = testdir.reparseconfig([])
|
|
|
|
config3 = testdir.reparseconfig([])
|
|
|
|
assert config2.getbasetemp() != config3.getbasetemp()
|
|
|
|
assert not config2.getbasetemp().relto(config3.getbasetemp())
|
|
|
|
assert not config3.getbasetemp().relto(config2.getbasetemp())
|
2009-03-17 20:42:40 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestConfigAPI:
|
2009-06-18 23:19:12 +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
|
|
|
|
py.test.raises(KeyError, "config.getvalue('y')")
|
2009-12-30 01:02:54 +08:00
|
|
|
config = testdir.reparseconfig([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
|
|
|
|
py.test.raises(KeyError, 'config.getvalue("y", o)')
|
|
|
|
|
2009-03-02 19:14:59 +08:00
|
|
|
def test_config_getvalueorskip(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
2010-07-27 03:15:15 +08:00
|
|
|
py.test.raises(py.test.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-07-27 03:15:15 +08:00
|
|
|
py.test.raises(py.test.skip.Exception,
|
2010-04-28 03:13:09 +08:00
|
|
|
"config.getvalueorskip('hello')")
|
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")
|
2009-12-30 01:02:54 +08:00
|
|
|
config = testdir.reparseconfig([str(o)])
|
2009-02-27 18:18:27 +08:00
|
|
|
assert config.getvalue('x') == 1
|
|
|
|
config.option.x = 2
|
|
|
|
assert config.getvalue('x') == 2
|
2009-12-30 01:02:54 +08:00
|
|
|
config = testdir.reparseconfig([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))
|
2009-12-30 01:02:54 +08:00
|
|
|
config = testdir.reparseconfig([p])
|
2009-03-17 05:17:14 +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
|
|
|
|
|
|
|
|
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-01-03 00:17:13 +08:00
|
|
|
def test_preparse_ordering(testdir, monkeypatch):
|
|
|
|
pkg_resources = py.test.importorskip("pkg_resources")
|
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
|
|
|
class EntryPoint:
|
|
|
|
name = "mytestplugin"
|
|
|
|
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
|