Some refactorings after code review

This commit is contained in:
elizabeth 2015-08-04 00:02:03 +03:00
parent db9809d6dc
commit 657ca97dbd
2 changed files with 27 additions and 13 deletions

View File

@ -892,22 +892,23 @@ class Metafunc(FuncargnamesCompatAttr):
if scope is None: if scope is None:
scope = "function" scope = "function"
scopenum = scopes.index(scope) scopenum = scopes.index(scope)
valtypes = dict.fromkeys(argnames, "funcargs") valtypes = {}
if not indirect: if indirect is True:
valtypes = dict.fromkeys(argnames, "params")
elif indirect is False:
valtypes = dict.fromkeys(argnames, "funcargs")
#XXX should we also check for the opposite case? #XXX should we also check for the opposite case?
for arg in argnames: for arg in argnames:
if arg not in self.fixturenames: if arg not in self.fixturenames:
raise ValueError("%r uses no fixture %r" %( raise ValueError("%r uses no fixture %r" %(
self.function, arg)) self.function, arg))
else: elif isinstance(indirect, (tuple, list)):
if not isinstance(indirect, (tuple, list)): valtypes = dict.fromkeys(argnames, "funcargs")
valtypes = dict.fromkeys(argnames, "params") for arg in indirect:
else: if arg not in argnames:
for arg in indirect: raise ValueError("indirect given to %r: fixture %r doesn't exist" %(
if arg not in argnames: self.function, arg))
raise ValueError("indirect: fixture %r doesn't exist" %( valtypes[arg] = "params"
arg))
valtypes[arg] = "params"
idfn = None idfn = None
if callable(ids): if callable(ids):
idfn = ids idfn = ids

View File

@ -208,6 +208,7 @@ class TestMetafunc:
assert metafunc._calls[0].id == "0-2" assert metafunc._calls[0].id == "0-2"
assert metafunc._calls[1].id == "0-3" assert metafunc._calls[1].id == "0-3"
@pytest.mark.issue714
def test_parametrize_indirect(self): def test_parametrize_indirect(self):
def func(x, y): pass def func(x, y): pass
metafunc = self.Metafunc(func) metafunc = self.Metafunc(func)
@ -220,6 +221,7 @@ class TestMetafunc:
assert metafunc._calls[0].params == dict(x=1,y=2, unnamed=1) assert metafunc._calls[0].params == dict(x=1,y=2, unnamed=1)
assert metafunc._calls[1].params == dict(x=1,y=3, unnamed=1) assert metafunc._calls[1].params == dict(x=1,y=3, unnamed=1)
@pytest.mark.issue714
def test_parametrize_indirect_list(self): def test_parametrize_indirect_list(self):
def func(x, y): pass def func(x, y): pass
metafunc = self.Metafunc(func) metafunc = self.Metafunc(func)
@ -227,6 +229,7 @@ class TestMetafunc:
assert metafunc._calls[0].funcargs == dict(y='b') assert metafunc._calls[0].funcargs == dict(y='b')
assert metafunc._calls[0].params == dict(x='a') assert metafunc._calls[0].params == dict(x='a')
@pytest.mark.issue714
def test_parametrize_indirect_list_all(self): def test_parametrize_indirect_list_all(self):
def func(x, y): pass def func(x, y): pass
metafunc = self.Metafunc(func) metafunc = self.Metafunc(func)
@ -234,6 +237,7 @@ class TestMetafunc:
assert metafunc._calls[0].funcargs == {} assert metafunc._calls[0].funcargs == {}
assert metafunc._calls[0].params == dict(x='a', y='b') assert metafunc._calls[0].params == dict(x='a', y='b')
@pytest.mark.issue714
def test_parametrize_indirect_list_empty(self): def test_parametrize_indirect_list_empty(self):
def func(x, y): pass def func(x, y): pass
metafunc = self.Metafunc(func) metafunc = self.Metafunc(func)
@ -241,7 +245,15 @@ class TestMetafunc:
assert metafunc._calls[0].funcargs == dict(x='a', y='b') assert metafunc._calls[0].funcargs == dict(x='a', y='b')
assert metafunc._calls[0].params == {} assert metafunc._calls[0].params == {}
@pytest.mark.issue714
def test_parametrize_indirect_list_functional(self, testdir): def test_parametrize_indirect_list_functional(self, testdir):
"""
Test parametrization with 'indirect' parameter applied on
particular arguments.
:param testdir: the instance of Testdir class, a temporary
test directory.
"""
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -261,11 +273,12 @@ class TestMetafunc:
"*1 passed*", "*1 passed*",
]) ])
@pytest.mark.issue714
def test_parametrize_indirect_list_error(self, testdir): def test_parametrize_indirect_list_error(self, testdir):
def func(x, y): pass def func(x, y): pass
metafunc = self.Metafunc(func) metafunc = self.Metafunc(func)
pytest.raises(ValueError, lambda: with pytest.raises(ValueError):
metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'z'])) metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'z'])
def test_addcalls_and_parametrize_indirect(self): def test_addcalls_and_parametrize_indirect(self):
def func(x, y): pass def func(x, y): pass