2015-09-30 04:57:49 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-08-07 13:31:04 +08:00
|
|
|
import re
|
2016-04-02 10:45:44 +08:00
|
|
|
import sys
|
2013-02-13 06:30:34 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
|
|
|
import py
|
|
|
|
import pytest
|
2016-07-10 02:36:00 +08:00
|
|
|
from _pytest import python, fixtures
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2016-04-02 10:45:44 +08:00
|
|
|
import hypothesis
|
|
|
|
from hypothesis import strategies
|
|
|
|
|
|
|
|
PY3 = sys.version_info >= (3, 0)
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMetafunc(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def Metafunc(self, func):
|
|
|
|
# the unit tests of this class check if things work correctly
|
|
|
|
# on the funcarg level, so we don't need a full blown
|
|
|
|
# initiliazation
|
2017-02-17 02:41:51 +08:00
|
|
|
class FixtureInfo(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
name2fixturedefs = None
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def __init__(self, names):
|
|
|
|
self.names_closure = names
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-07-10 02:36:00 +08:00
|
|
|
names = fixtures.getfuncargnames(func)
|
2012-11-02 23:04:57 +08:00
|
|
|
fixtureinfo = FixtureInfo(names)
|
2016-07-10 02:36:00 +08:00
|
|
|
return python.Metafunc(func, fixtureinfo, None)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_no_funcargs(self, testdir):
|
|
|
|
def function(): pass
|
|
|
|
metafunc = self.Metafunc(function)
|
|
|
|
assert not metafunc.fixturenames
|
|
|
|
repr(metafunc._calls)
|
|
|
|
|
|
|
|
def test_function_basic(self):
|
|
|
|
def func(arg1, arg2="qwe"): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
assert len(metafunc.fixturenames) == 1
|
|
|
|
assert 'arg1' in metafunc.fixturenames
|
|
|
|
assert metafunc.function is func
|
|
|
|
assert metafunc.cls is None
|
|
|
|
|
|
|
|
def test_addcall_no_args(self):
|
|
|
|
def func(arg1): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.addcall()
|
|
|
|
assert len(metafunc._calls) == 1
|
|
|
|
call = metafunc._calls[0]
|
|
|
|
assert call.id == "0"
|
|
|
|
assert not hasattr(call, 'param')
|
|
|
|
|
|
|
|
def test_addcall_id(self):
|
|
|
|
def func(arg1): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
pytest.raises(ValueError, "metafunc.addcall(id=None)")
|
|
|
|
|
|
|
|
metafunc.addcall(id=1)
|
|
|
|
pytest.raises(ValueError, "metafunc.addcall(id=1)")
|
|
|
|
pytest.raises(ValueError, "metafunc.addcall(id='1')")
|
|
|
|
metafunc.addcall(id=2)
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].id == "1"
|
|
|
|
assert metafunc._calls[1].id == "2"
|
|
|
|
|
|
|
|
def test_addcall_param(self):
|
|
|
|
def func(arg1): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class obj(object): pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc.addcall(param=obj)
|
|
|
|
metafunc.addcall(param=obj)
|
|
|
|
metafunc.addcall(param=1)
|
|
|
|
assert len(metafunc._calls) == 3
|
|
|
|
assert metafunc._calls[0].getparam("arg1") == obj
|
|
|
|
assert metafunc._calls[1].getparam("arg1") == obj
|
|
|
|
assert metafunc._calls[2].getparam("arg1") == 1
|
|
|
|
|
|
|
|
def test_addcall_funcargs(self):
|
|
|
|
def func(x): pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class obj(object): pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc.addcall(funcargs={"x": 2})
|
|
|
|
metafunc.addcall(funcargs={"x": 3})
|
|
|
|
pytest.raises(pytest.fail.Exception, "metafunc.addcall({'xyz': 0})")
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == {'x': 2}
|
|
|
|
assert metafunc._calls[1].funcargs == {'x': 3}
|
|
|
|
assert not hasattr(metafunc._calls[1], 'param')
|
|
|
|
|
|
|
|
def test_parametrize_error(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize("x", [1,2])
|
|
|
|
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5,6]))
|
|
|
|
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5,6]))
|
|
|
|
metafunc.parametrize("y", [1,2])
|
|
|
|
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5,6]))
|
|
|
|
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5,6]))
|
|
|
|
|
2016-09-07 02:16:25 +08:00
|
|
|
def test_parametrize_bad_scope(self, testdir):
|
|
|
|
def func(x): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
try:
|
|
|
|
metafunc.parametrize("x", [1], scope='doggy')
|
|
|
|
except ValueError as ve:
|
|
|
|
assert "has an unsupported scope value 'doggy'" in str(ve)
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_parametrize_and_id(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
|
|
|
|
metafunc.parametrize("x", [1,2], ids=['basic', 'advanced'])
|
|
|
|
metafunc.parametrize("y", ["abc", "def"])
|
|
|
|
ids = [x.id for x in metafunc._calls]
|
|
|
|
assert ids == ["basic-abc", "basic-def", "advanced-abc", "advanced-def"]
|
|
|
|
|
2016-09-03 05:25:26 +08:00
|
|
|
def test_parametrize_and_id_unicode(self):
|
|
|
|
"""Allow unicode strings for "ids" parameter in Python 2 (##1905)"""
|
|
|
|
def func(x): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize("x", [1, 2], ids=[u'basic', u'advanced'])
|
|
|
|
ids = [x.id for x in metafunc._calls]
|
|
|
|
assert ids == [u"basic", u"advanced"]
|
|
|
|
|
2013-05-29 10:59:47 +08:00
|
|
|
def test_parametrize_with_wrong_number_of_ids(self, testdir):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
|
2013-07-16 21:43:20 +08:00
|
|
|
pytest.raises(ValueError, lambda:
|
2017-07-17 07:25:07 +08:00
|
|
|
metafunc.parametrize("x", [1,2], ids=['basic']))
|
2013-05-29 10:59:47 +08:00
|
|
|
|
2013-07-16 21:43:20 +08:00
|
|
|
pytest.raises(ValueError, lambda:
|
2017-07-17 07:25:07 +08:00
|
|
|
metafunc.parametrize(("x","y"), [("abc", "def"),
|
|
|
|
("ghi", "jkl")], ids=["one"]))
|
2013-05-29 10:59:47 +08:00
|
|
|
|
2016-04-22 02:57:53 +08:00
|
|
|
@pytest.mark.issue510
|
|
|
|
def test_parametrize_empty_list(self):
|
|
|
|
def func( y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize("y", [])
|
|
|
|
assert 'skip' in metafunc._calls[0].keywords
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_parametrize_with_userobjects(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc.parametrize("x", [A(), A()])
|
|
|
|
metafunc.parametrize("y", list("ab"))
|
|
|
|
assert metafunc._calls[0].id == "x0-a"
|
|
|
|
assert metafunc._calls[1].id == "x0-b"
|
|
|
|
assert metafunc._calls[2].id == "x1-a"
|
|
|
|
assert metafunc._calls[3].id == "x1-b"
|
|
|
|
|
2016-04-02 10:45:44 +08:00
|
|
|
@hypothesis.given(strategies.text() | strategies.binary())
|
|
|
|
def test_idval_hypothesis(self, value):
|
|
|
|
from _pytest.python import _idval
|
|
|
|
escaped = _idval(value, 'a', 6, None)
|
|
|
|
assert isinstance(escaped, str)
|
|
|
|
if PY3:
|
|
|
|
escaped.encode('ascii')
|
|
|
|
else:
|
|
|
|
escaped.decode('ascii')
|
|
|
|
|
2016-04-02 00:27:17 +08:00
|
|
|
def test_unicode_idval(self):
|
|
|
|
"""This tests that Unicode strings outside the ASCII character set get
|
|
|
|
escaped, using byte escapes if they're in that range or unicode
|
|
|
|
escapes if they're not.
|
|
|
|
|
2015-09-30 04:57:49 +08:00
|
|
|
"""
|
|
|
|
from _pytest.python import _idval
|
|
|
|
values = [
|
|
|
|
(u'', ''),
|
|
|
|
(u'ascii', 'ascii'),
|
2016-04-02 00:27:17 +08:00
|
|
|
(u'ação', 'a\\xe7\\xe3o'),
|
|
|
|
(u'josé@blah.com', 'jos\\xe9@blah.com'),
|
|
|
|
(u'δοκ.ιμή@παράδειγμα.δοκιμή', '\\u03b4\\u03bf\\u03ba.\\u03b9\\u03bc\\u03ae@\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3\\u03bc\\u03b1.\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae'),
|
2015-09-30 04:57:49 +08:00
|
|
|
]
|
|
|
|
for val, expected in values:
|
2015-09-30 05:20:30 +08:00
|
|
|
assert _idval(val, 'a', 6, None) == expected
|
|
|
|
|
|
|
|
def test_bytes_idval(self):
|
|
|
|
"""unittest for the expected behavior to obtain ids for parametrized
|
|
|
|
bytes values:
|
|
|
|
- python2: non-ascii strings are considered bytes and formatted using
|
|
|
|
"binary escape", where any byte < 127 is escaped into its hex form.
|
|
|
|
- python3: bytes objects are always escaped using "binary escape".
|
|
|
|
"""
|
|
|
|
from _pytest.python import _idval
|
|
|
|
values = [
|
|
|
|
(b'', ''),
|
|
|
|
(b'\xc3\xb4\xff\xe4', '\\xc3\\xb4\\xff\\xe4'),
|
|
|
|
(b'ascii', 'ascii'),
|
|
|
|
(u'αρά'.encode('utf-8'), '\\xce\\xb1\\xcf\\x81\\xce\\xac'),
|
|
|
|
]
|
|
|
|
for val, expected in values:
|
2015-09-30 04:57:49 +08:00
|
|
|
assert _idval(val, 'a', 6, None) == expected
|
|
|
|
|
2013-02-13 06:30:34 +08:00
|
|
|
@pytest.mark.issue250
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_idmaker_autoname(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [pytest.param("string", 1.0),
|
|
|
|
pytest.param("st-ring", 2.0)])
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result == ["string-1.0", "st-ring-2.0"]
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [pytest.param(object(), 1.0),
|
|
|
|
pytest.param(object(), object())])
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result == ["a0-1.0", "a1-b1"]
|
2013-02-13 06:30:34 +08:00
|
|
|
# unicode mixing, issue250
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(
|
|
|
|
(py.builtin._totext("a"), "b"),
|
|
|
|
[pytest.param({}, b'\xc3\xb4')])
|
2015-09-23 09:48:22 +08:00
|
|
|
assert result == ['a0-\\xc3\\xb4']
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2016-03-06 01:23:13 +08:00
|
|
|
def test_idmaker_with_bytes_regex(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a"), [pytest.param(re.compile(b'foo'), 1.0)])
|
2016-03-06 01:23:13 +08:00
|
|
|
assert result == ["foo"]
|
|
|
|
|
2013-05-29 10:59:47 +08:00
|
|
|
def test_idmaker_native_strings(self):
|
|
|
|
from _pytest.python import idmaker
|
2015-09-23 09:48:22 +08:00
|
|
|
totext = py.builtin._totext
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [
|
|
|
|
pytest.param(1.0, -1.1),
|
|
|
|
pytest.param(2, -202),
|
|
|
|
pytest.param("three", "three hundred"),
|
|
|
|
pytest.param(True, False),
|
|
|
|
pytest.param(None, None),
|
|
|
|
pytest.param(re.compile('foo'), re.compile('bar')),
|
|
|
|
pytest.param(str, int),
|
|
|
|
pytest.param(list("six"), [66, 66]),
|
|
|
|
pytest.param(set([7]), set("seven")),
|
|
|
|
pytest.param(tuple("eight"), (8, -8, 8)),
|
|
|
|
pytest.param(b'\xc3\xb4', b"name"),
|
|
|
|
pytest.param(b'\xc3\xb4', totext("other")),
|
2013-05-29 10:59:47 +08:00
|
|
|
])
|
|
|
|
assert result == ["1.0--1.1",
|
|
|
|
"2--202",
|
|
|
|
"three-three hundred",
|
|
|
|
"True-False",
|
|
|
|
"None-None",
|
2015-08-07 13:31:04 +08:00
|
|
|
"foo-bar",
|
|
|
|
"str-int",
|
|
|
|
"a7-b7",
|
|
|
|
"a8-b8",
|
2015-09-23 09:48:22 +08:00
|
|
|
"a9-b9",
|
|
|
|
"\\xc3\\xb4-name",
|
|
|
|
"\\xc3\\xb4-other",
|
|
|
|
]
|
2015-08-07 13:31:04 +08:00
|
|
|
|
|
|
|
def test_idmaker_enum(self):
|
|
|
|
from _pytest.python import idmaker
|
|
|
|
enum = pytest.importorskip("enum")
|
|
|
|
e = enum.Enum("Foo", "one, two")
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [pytest.param(e.one, e.two)])
|
2015-08-07 13:31:04 +08:00
|
|
|
assert result == ["Foo.one-Foo.two"]
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2014-04-18 03:08:49 +08:00
|
|
|
@pytest.mark.issue351
|
|
|
|
def test_idmaker_idfn(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-04-18 03:08:49 +08:00
|
|
|
def ids(val):
|
|
|
|
if isinstance(val, Exception):
|
|
|
|
return repr(val)
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [
|
|
|
|
pytest.param(10.0, IndexError()),
|
|
|
|
pytest.param(20, KeyError()),
|
|
|
|
pytest.param("three", [1, 2, 3]),
|
2014-04-18 03:08:49 +08:00
|
|
|
], idfn=ids)
|
|
|
|
assert result == ["10.0-IndexError()",
|
|
|
|
"20-KeyError()",
|
|
|
|
"three-b2",
|
2017-07-17 07:25:07 +08:00
|
|
|
]
|
2014-04-18 03:08:49 +08:00
|
|
|
|
|
|
|
@pytest.mark.issue351
|
|
|
|
def test_idmaker_idfn_unique_names(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-04-18 03:08:49 +08:00
|
|
|
def ids(val):
|
|
|
|
return 'a'
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [pytest.param(10.0, IndexError()),
|
|
|
|
pytest.param(20, KeyError()),
|
|
|
|
pytest.param("three", [1, 2, 3]),
|
2017-07-17 07:25:07 +08:00
|
|
|
], idfn=ids)
|
2016-03-23 05:58:28 +08:00
|
|
|
assert result == ["a-a0",
|
|
|
|
"a-a1",
|
|
|
|
"a-a2",
|
2017-07-17 07:25:07 +08:00
|
|
|
]
|
2014-04-18 03:08:49 +08:00
|
|
|
|
|
|
|
@pytest.mark.issue351
|
|
|
|
def test_idmaker_idfn_exception(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-12-31 01:37:09 +08:00
|
|
|
from _pytest.recwarn import WarningsRecorder
|
|
|
|
|
|
|
|
class BadIdsException(Exception):
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-04-18 03:08:49 +08:00
|
|
|
def ids(val):
|
2016-12-31 01:37:09 +08:00
|
|
|
raise BadIdsException("ids raised")
|
|
|
|
|
|
|
|
rec = WarningsRecorder()
|
|
|
|
with rec:
|
2016-09-07 17:00:27 +08:00
|
|
|
idmaker(("a", "b"), [
|
|
|
|
pytest.param(10.0, IndexError()),
|
|
|
|
pytest.param(20, KeyError()),
|
|
|
|
pytest.param("three", [1, 2, 3]),
|
2016-12-31 01:37:09 +08:00
|
|
|
], idfn=ids)
|
|
|
|
|
|
|
|
assert [str(i.message) for i in rec.list] == [
|
|
|
|
"Raised while trying to determine id of parameter a at position 0."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
"Raised while trying to determine id of parameter b at position 0."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
"Raised while trying to determine id of parameter a at position 1."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
"Raised while trying to determine id of parameter b at position 1."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
"Raised while trying to determine id of parameter a at position 2."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
"Raised while trying to determine id of parameter b at position 2."
|
|
|
|
"\nUpdate your code as this will raise an error in pytest-4.0.",
|
|
|
|
]
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2016-12-31 01:37:09 +08:00
|
|
|
|
|
|
|
def test_parametrize_ids_exception(self, testdir):
|
|
|
|
"""
|
|
|
|
:param testdir: the instance of Testdir class, a temporary
|
|
|
|
test directory.
|
|
|
|
"""
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def ids(arg):
|
|
|
|
raise Exception("bad ids")
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
|
|
|
|
def test_foo(arg):
|
|
|
|
pass
|
|
|
|
""")
|
2017-03-21 09:01:22 +08:00
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
result = testdir.runpytest("--collect-only")
|
2016-12-31 01:37:09 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"<Module 'test_parametrize_ids_exception.py'>",
|
|
|
|
" <Function 'test_foo[a]'>",
|
|
|
|
" <Function 'test_foo[b]'>",
|
|
|
|
])
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2016-03-20 03:23:49 +08:00
|
|
|
def test_idmaker_with_ids(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a", "b"), [pytest.param(1, 2),
|
|
|
|
pytest.param(3, 4)],
|
2016-03-20 03:23:49 +08:00
|
|
|
ids=["a", None])
|
|
|
|
assert result == ["a", "3-4"]
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
def test_idmaker_with_paramset_id(self):
|
|
|
|
from _pytest.python import idmaker
|
|
|
|
result = idmaker(("a", "b"), [pytest.param(1, 2, id="me"),
|
|
|
|
pytest.param(3, 4, id="you")],
|
|
|
|
ids=["a", None])
|
|
|
|
assert result == ["me", "you"]
|
|
|
|
|
2016-03-20 03:42:47 +08:00
|
|
|
def test_idmaker_with_ids_unique_names(self):
|
|
|
|
from _pytest.python import idmaker
|
2016-09-07 17:00:27 +08:00
|
|
|
result = idmaker(("a"), map(pytest.param, [1,2,3,4,5]),
|
2016-03-23 05:58:28 +08:00
|
|
|
ids=["a", "a", "b", "c", "b"])
|
|
|
|
assert result == ["a0", "a1", "b0", "c", "b1"]
|
2016-03-20 03:42:47 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_addcall_and_parametrize(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.addcall({'x': 1})
|
|
|
|
metafunc.parametrize('y', [2,3])
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == {'x': 1, 'y': 2}
|
|
|
|
assert metafunc._calls[1].funcargs == {'x': 1, 'y': 3}
|
|
|
|
assert metafunc._calls[0].id == "0-2"
|
|
|
|
assert metafunc._calls[1].id == "0-3"
|
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_parametrize_indirect(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize('x', [1], indirect=True)
|
|
|
|
metafunc.parametrize('y', [2,3], indirect=True)
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == {}
|
|
|
|
assert metafunc._calls[1].funcargs == {}
|
2015-08-23 18:42:40 +08:00
|
|
|
assert metafunc._calls[0].params == dict(x=1,y=2)
|
|
|
|
assert metafunc._calls[1].params == dict(x=1,y=3)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2015-08-02 21:40:40 +08:00
|
|
|
def test_parametrize_indirect_list(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize('x, y', [('a', 'b')], indirect=['x'])
|
|
|
|
assert metafunc._calls[0].funcargs == dict(y='b')
|
|
|
|
assert metafunc._calls[0].params == dict(x='a')
|
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2015-08-02 21:40:40 +08:00
|
|
|
def test_parametrize_indirect_list_all(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'y'])
|
|
|
|
assert metafunc._calls[0].funcargs == {}
|
|
|
|
assert metafunc._calls[0].params == dict(x='a', y='b')
|
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2015-08-02 21:40:40 +08:00
|
|
|
def test_parametrize_indirect_list_empty(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize('x, y', [('a', 'b')], indirect=[])
|
|
|
|
assert metafunc._calls[0].funcargs == dict(x='a', y='b')
|
|
|
|
assert metafunc._calls[0].params == {}
|
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2015-08-02 21:40:40 +08:00
|
|
|
def test_parametrize_indirect_list_functional(self, testdir):
|
2015-08-04 05:02:03 +08:00
|
|
|
"""
|
|
|
|
Test parametrization with 'indirect' parameter applied on
|
2015-08-09 01:20:09 +08:00
|
|
|
particular arguments. As y is is direct, its value should
|
|
|
|
be used directly rather than being passed to the fixture
|
|
|
|
y.
|
2015-08-04 05:02:03 +08:00
|
|
|
|
|
|
|
:param testdir: the instance of Testdir class, a temporary
|
|
|
|
test directory.
|
|
|
|
"""
|
2015-08-02 21:40:40 +08:00
|
|
|
testdir.makepyfile("""
|
2015-08-03 01:30:23 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def x(request):
|
2015-08-02 21:40:40 +08:00
|
|
|
return request.param * 3
|
2015-08-03 01:30:23 +08:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def y(request):
|
2015-08-02 21:40:40 +08:00
|
|
|
return request.param * 2
|
2015-08-03 01:30:23 +08:00
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
|
2015-08-02 21:40:40 +08:00
|
|
|
def test_simple(x,y):
|
|
|
|
assert len(x) == 3
|
|
|
|
assert len(y) == 1
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_simple*a-b*",
|
|
|
|
"*1 passed*",
|
|
|
|
])
|
|
|
|
|
2015-08-04 05:02:03 +08:00
|
|
|
@pytest.mark.issue714
|
2015-08-03 04:53:44 +08:00
|
|
|
def test_parametrize_indirect_list_error(self, testdir):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
2015-08-04 05:02:03 +08:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'z'])
|
2015-08-03 04:06:24 +08:00
|
|
|
|
2015-08-09 01:20:09 +08:00
|
|
|
@pytest.mark.issue714
|
|
|
|
def test_parametrize_uses_no_fixture_error_indirect_false(self, testdir):
|
|
|
|
"""The 'uses no fixture' error tells the user at collection time
|
|
|
|
that the parametrize data they've set up doesn't correspond to the
|
|
|
|
fixtures in their test function, rather than silently ignoring this
|
|
|
|
and letting the test potentially pass.
|
|
|
|
"""
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=False)
|
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines([
|
2016-07-23 22:49:20 +08:00
|
|
|
"*uses no argument 'y'*",
|
2015-08-09 01:20:09 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
@pytest.mark.issue714
|
|
|
|
def test_parametrize_uses_no_fixture_error_indirect_true(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def x(request):
|
|
|
|
return request.param * 3
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def y(request):
|
|
|
|
return request.param * 2
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=True)
|
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*uses no fixture 'y'*",
|
|
|
|
])
|
|
|
|
|
2016-07-24 16:47:06 +08:00
|
|
|
@pytest.mark.issue714
|
|
|
|
def test_parametrize_indirect_uses_no_fixture_error_indirect_string(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def x(request):
|
|
|
|
return request.param * 3
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect='y')
|
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*uses no fixture 'y'*",
|
|
|
|
])
|
|
|
|
|
2015-08-09 01:20:09 +08:00
|
|
|
@pytest.mark.issue714
|
|
|
|
def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def x(request):
|
|
|
|
return request.param * 3
|
|
|
|
|
2016-07-23 22:49:20 +08:00
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['y'])
|
2015-08-09 01:20:09 +08:00
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*uses no fixture 'y'*",
|
|
|
|
])
|
|
|
|
|
2016-07-23 22:49:20 +08:00
|
|
|
@pytest.mark.issue714
|
|
|
|
def test_parametrize_argument_not_in_indirect_list(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def x(request):
|
|
|
|
return request.param * 3
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
|
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--collect-only")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*uses no argument 'y'*",
|
|
|
|
])
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_addcalls_and_parametrize_indirect(self):
|
|
|
|
def func(x, y): pass
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.addcall(param="123")
|
|
|
|
metafunc.parametrize('x', [1], indirect=True)
|
|
|
|
metafunc.parametrize('y', [2,3], indirect=True)
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == {}
|
|
|
|
assert metafunc._calls[1].funcargs == {}
|
|
|
|
assert metafunc._calls[0].params == dict(x=1,y=2)
|
|
|
|
assert metafunc._calls[1].params == dict(x=1,y=3)
|
|
|
|
|
|
|
|
def test_parametrize_functional(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize('x', [1,2], indirect=True)
|
|
|
|
metafunc.parametrize('y', [2])
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def x(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param * 10
|
|
|
|
|
|
|
|
def test_simple(x,y):
|
|
|
|
assert x in (10,20)
|
|
|
|
assert y == 2
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_simple*1-2*",
|
|
|
|
"*test_simple*2-2*",
|
|
|
|
"*2 passed*",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_parametrize_onearg(self):
|
|
|
|
metafunc = self.Metafunc(lambda x: None)
|
|
|
|
metafunc.parametrize("x", [1,2])
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == dict(x=1)
|
|
|
|
assert metafunc._calls[0].id == "1"
|
|
|
|
assert metafunc._calls[1].funcargs == dict(x=2)
|
|
|
|
assert metafunc._calls[1].id == "2"
|
|
|
|
|
|
|
|
def test_parametrize_onearg_indirect(self):
|
|
|
|
metafunc = self.Metafunc(lambda x: None)
|
|
|
|
metafunc.parametrize("x", [1,2], indirect=True)
|
|
|
|
assert metafunc._calls[0].params == dict(x=1)
|
|
|
|
assert metafunc._calls[0].id == "1"
|
|
|
|
assert metafunc._calls[1].params == dict(x=2)
|
|
|
|
assert metafunc._calls[1].id == "2"
|
|
|
|
|
|
|
|
def test_parametrize_twoargs(self):
|
|
|
|
metafunc = self.Metafunc(lambda x,y: None)
|
|
|
|
metafunc.parametrize(("x", "y"), [(1,2), (3,4)])
|
|
|
|
assert len(metafunc._calls) == 2
|
|
|
|
assert metafunc._calls[0].funcargs == dict(x=1, y=2)
|
|
|
|
assert metafunc._calls[0].id == "1-2"
|
|
|
|
assert metafunc._calls[1].funcargs == dict(x=3, y=4)
|
|
|
|
assert metafunc._calls[1].id == "3-4"
|
|
|
|
|
|
|
|
def test_parametrize_multiple_times(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.parametrize("x", [1,2])
|
|
|
|
def test_func(x):
|
|
|
|
assert 0, x
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
pytestmark = pytest.mark.parametrize("y", [3,4])
|
|
|
|
def test_meth(self, x, y):
|
|
|
|
assert 0, x
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 1
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(failed=6)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
def test_parametrize_CSV(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("x, y,", [(1,2), (2,3)])
|
|
|
|
def test_func(x, y):
|
|
|
|
assert x+1 == y
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_parametrize_class_scenarios(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
# same as doc/en/example/parametrize scenario example
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
idlist = []
|
|
|
|
argvalues = []
|
|
|
|
for scenario in metafunc.cls.scenarios:
|
|
|
|
idlist.append(scenario[0])
|
|
|
|
items = scenario[1].items()
|
|
|
|
argnames = [x[0] for x in items]
|
|
|
|
argvalues.append(([x[1] for x in items]))
|
|
|
|
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
|
|
|
|
|
|
|
|
class Test(object):
|
|
|
|
scenarios = [['1', {'arg': {1: 2}, "arg2": "value2"}],
|
|
|
|
['2', {'arg':'value2', "arg2": "value2"}]]
|
|
|
|
|
|
|
|
def test_1(self, arg, arg2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_2(self, arg2, arg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_3(self, arg, arg2):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*test_1*1*
|
|
|
|
*test_2*1*
|
|
|
|
*test_3*1*
|
|
|
|
*test_1*2*
|
|
|
|
*test_2*2*
|
|
|
|
*test_3*2*
|
|
|
|
*6 passed*
|
|
|
|
""")
|
|
|
|
|
2015-09-17 04:52:37 +08:00
|
|
|
def test_format_args(self):
|
|
|
|
def function1(): pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures._format_args(function1) == '()'
|
2015-09-17 04:52:37 +08:00
|
|
|
|
|
|
|
def function2(arg1): pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures._format_args(function2) == "(arg1)"
|
2015-09-17 04:52:37 +08:00
|
|
|
|
|
|
|
def function3(arg1, arg2="qwe"): pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures._format_args(function3) == "(arg1, arg2='qwe')"
|
2015-09-17 04:52:37 +08:00
|
|
|
|
|
|
|
def function4(arg1, *args, **kwargs): pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures._format_args(function4) == "(arg1, *args, **kwargs)"
|
2015-09-17 04:52:37 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMetafuncFunctional(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_attributes(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
# assumes that generate/provide runs in the same process
|
|
|
|
import py, pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(param=metafunc)
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def metafunc(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
assert request._pyfuncitem._genid == "0"
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_function(metafunc, pytestconfig):
|
|
|
|
assert metafunc.config == pytestconfig
|
|
|
|
assert metafunc.module.__name__ == __name__
|
|
|
|
assert metafunc.function == test_function
|
|
|
|
assert metafunc.cls is None
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_method(self, metafunc, pytestconfig):
|
|
|
|
assert metafunc.config == pytestconfig
|
|
|
|
assert metafunc.module.__name__ == __name__
|
|
|
|
if py.std.sys.version_info > (3, 0):
|
|
|
|
unbound = TestClass.test_method
|
|
|
|
else:
|
|
|
|
unbound = TestClass.test_method.im_func
|
|
|
|
# XXX actually have an unbound test function here?
|
|
|
|
assert metafunc.function == unbound
|
|
|
|
assert metafunc.cls == TestClass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p, "-v")
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_addcall_with_two_funcargs_generators(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert "arg1" in metafunc.fixturenames
|
|
|
|
metafunc.addcall(funcargs=dict(arg1=1, arg2=2))
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(funcargs=dict(arg1=1, arg2=1))
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_myfunc(self, arg1, arg2):
|
|
|
|
assert arg1 == arg2
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_myfunc*0*PASS*",
|
|
|
|
"*test_myfunc*1*FAIL*",
|
|
|
|
"*1 failed, 1 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_two_functions(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(param=10)
|
|
|
|
metafunc.addcall(param=20)
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_func1(arg1):
|
|
|
|
assert arg1 == 10
|
|
|
|
def test_func2(arg1):
|
|
|
|
assert arg1 in (10, 20)
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func1*0*PASS*",
|
|
|
|
"*test_func1*1*FAIL*",
|
|
|
|
"*test_func2*PASS*",
|
|
|
|
"*1 failed, 3 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_noself_in_method(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert 'xyz' not in metafunc.fixturenames
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestHello(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello(xyz):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p)
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_generate_plugin_and_module(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert "arg1" in metafunc.fixturenames
|
|
|
|
metafunc.addcall(id="world", param=(2,100))
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(param=(1,1), id="hello")
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param[0]
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param[1]
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_myfunc(self, arg1, arg2):
|
|
|
|
assert arg1 == arg2
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_myfunc*hello*PASS*",
|
|
|
|
"*test_myfunc*world*FAIL*",
|
|
|
|
"*1 failed, 1 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_generate_tests_in_class(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(self, metafunc):
|
|
|
|
metafunc.addcall(funcargs={'hello': 'world'}, id="hello")
|
|
|
|
|
|
|
|
def test_myfunc(self, hello):
|
|
|
|
assert hello == "world"
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_myfunc*hello*PASS*",
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_two_functions_not_same_instance(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall({'arg1': 10})
|
|
|
|
metafunc.addcall({'arg1': 20})
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_func(self, arg1):
|
|
|
|
assert not hasattr(self, 'x')
|
|
|
|
self.x = 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func*0*PASS*",
|
|
|
|
"*test_func*1*PASS*",
|
|
|
|
"*2 pass*",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_issue28_setup_method_in_generate_tests(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall({'arg1': 1})
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_method(self, arg1):
|
|
|
|
assert arg1 == self.val
|
|
|
|
def setup_method(self, func):
|
|
|
|
self.val = 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p)
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_parametrize_functional2(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize("arg1", [1,2])
|
|
|
|
metafunc.parametrize("arg2", [4,5])
|
|
|
|
def test_hello(arg1, arg2):
|
|
|
|
assert 0, (arg1, arg2)
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*(1, 4)*",
|
|
|
|
"*(1, 5)*",
|
|
|
|
"*(2, 4)*",
|
|
|
|
"*(2, 5)*",
|
|
|
|
"*4 failed*",
|
|
|
|
])
|
|
|
|
|
2016-06-21 18:09:55 +08:00
|
|
|
def test_parametrize_and_inner_getfixturevalue(self, testdir):
|
2012-11-02 23:04:57 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize("arg1", [1], indirect=True)
|
|
|
|
metafunc.parametrize("arg2", [10], indirect=True)
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
x = request.getfixturevalue("arg2")
|
2012-11-02 23:04:57 +08:00
|
|
|
return x + request.param
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_func1(arg1, arg2):
|
|
|
|
assert arg1 == 11
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func1*1*PASS*",
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_parametrize_on_setup_arg(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert "arg1" in metafunc.fixturenames
|
|
|
|
metafunc.parametrize("arg1", [1], indirect=True)
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request, arg1):
|
2012-11-02 23:04:57 +08:00
|
|
|
return 10 * arg1
|
|
|
|
|
|
|
|
def test_func(arg2):
|
|
|
|
assert arg2 == 10
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func*1*PASS*",
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_parametrize_with_ids(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize(("a", "b"), [(1,1), (1,2)],
|
|
|
|
ids=["basic", "advanced"])
|
|
|
|
|
|
|
|
def test_function(a, b):
|
|
|
|
assert a == b
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines_random([
|
|
|
|
"*test_function*basic*PASSED",
|
|
|
|
"*test_function*advanced*FAILED",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_parametrize_without_ids(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize(("a", "b"),
|
|
|
|
[(1,object()), (1.3,object())])
|
|
|
|
|
|
|
|
def test_function(a, b):
|
|
|
|
assert 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*test_function*1-b0*
|
|
|
|
*test_function*1.3-b1*
|
|
|
|
""")
|
|
|
|
|
2016-03-20 03:38:24 +08:00
|
|
|
def test_parametrize_with_None_in_ids(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
2016-03-20 03:42:47 +08:00
|
|
|
metafunc.parametrize(("a", "b"), [(1,1), (1,1), (1,2)],
|
2016-03-20 03:38:24 +08:00
|
|
|
ids=["basic", None, "advanced"])
|
|
|
|
|
|
|
|
def test_function(a, b):
|
|
|
|
assert a == b
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines_random([
|
|
|
|
"*test_function*basic*PASSED",
|
|
|
|
"*test_function*1-1*PASSED",
|
|
|
|
"*test_function*advanced*FAILED",
|
|
|
|
])
|
|
|
|
|
2016-08-24 05:14:15 +08:00
|
|
|
def test_fixture_parametrized_empty_ids(self, testdir):
|
|
|
|
"""Fixtures parametrized with empty ids cause an internal error (#1849)."""
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", ids=[], params=[])
|
|
|
|
def temp(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_temp(temp):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 1 skipped *'])
|
|
|
|
|
|
|
|
def test_parametrized_empty_ids(self, testdir):
|
|
|
|
"""Tests parametrized with empty ids cause an internal error (#1849)."""
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('temp', [], ids=list())
|
|
|
|
def test_temp(temp):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 1 skipped *'])
|
|
|
|
|
2016-08-24 08:07:41 +08:00
|
|
|
def test_parametrized_ids_invalid_type(self, testdir):
|
|
|
|
"""Tests parametrized with ids as non-strings (#1857)."""
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x, expected", [(10, 20), (40, 80)], ids=(None, 2))
|
|
|
|
def test_ids_numbers(x,expected):
|
|
|
|
assert x * 2 == expected
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['*ids must be list of strings, found: 2 (type: int)*'])
|
|
|
|
|
2016-03-20 03:42:47 +08:00
|
|
|
def test_parametrize_with_identical_ids_get_unique_names(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize(("a", "b"), [(1,1), (1,2)],
|
|
|
|
ids=["a", "a"])
|
|
|
|
|
|
|
|
def test_function(a, b):
|
|
|
|
assert a == b
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines_random([
|
2016-03-23 05:58:28 +08:00
|
|
|
"*test_function*a0*PASSED",
|
|
|
|
"*test_function*a1*FAILED"
|
2016-03-20 03:42:47 +08:00
|
|
|
])
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.mark.parametrize(("scope", "length"),
|
|
|
|
[("module", 2), ("function", 4)])
|
|
|
|
def test_parametrize_scope_overrides(self, testdir, scope, length):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
l = []
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
if "arg" in metafunc.funcargnames:
|
|
|
|
metafunc.parametrize("arg", [1,2], indirect=True,
|
|
|
|
scope=%r)
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
l.append(request.param)
|
|
|
|
return request.param
|
|
|
|
def test_hello(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
def test_world(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
def test_checklength():
|
|
|
|
assert len(l) == %d
|
|
|
|
""" % (scope, length))
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2013-06-28 18:57:10 +08:00
|
|
|
def test_parametrize_issue323(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module', params=range(966))
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_it(foo):
|
|
|
|
pass
|
2013-12-08 03:55:17 +08:00
|
|
|
def test_it2(foo):
|
|
|
|
pass
|
2013-06-28 18:57:10 +08:00
|
|
|
""")
|
2013-08-01 23:32:19 +08:00
|
|
|
reprec = testdir.inline_run("--collect-only")
|
2013-06-28 18:57:10 +08:00
|
|
|
assert not reprec.getcalls("pytest_internalerror")
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_usefixtures_seen_in_generate_tests(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert "abc" in metafunc.fixturenames
|
|
|
|
metafunc.parametrize("abc", [1])
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("abc")
|
|
|
|
def test_function():
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
reprec = testdir.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
reprec.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_generate_tests_only_done_in_subdir(self, testdir):
|
|
|
|
sub1 = testdir.mkpydir("sub1")
|
|
|
|
sub2 = testdir.mkpydir("sub2")
|
2015-11-27 22:43:01 +08:00
|
|
|
sub1.join("conftest.py").write(_pytest._code.Source("""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert metafunc.function.__name__ == "test_1"
|
|
|
|
"""))
|
2015-11-27 22:43:01 +08:00
|
|
|
sub2.join("conftest.py").write(_pytest._code.Source("""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert metafunc.function.__name__ == "test_2"
|
|
|
|
"""))
|
|
|
|
sub1.join("test_in_sub1.py").write("def test_1(): pass")
|
|
|
|
sub2.join("test_in_sub2.py").write("def test_2(): pass")
|
2016-07-25 18:40:57 +08:00
|
|
|
result = testdir.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1)
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=3)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2013-12-16 14:47:59 +08:00
|
|
|
def test_generate_same_function_names_issue403(self, testdir):
|
2013-12-16 19:38:15 +08:00
|
|
|
testdir.makepyfile("""
|
2013-12-16 14:47:59 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def make_tests():
|
|
|
|
@pytest.mark.parametrize("x", range(2))
|
|
|
|
def test_foo(x):
|
|
|
|
pass
|
|
|
|
return test_foo
|
|
|
|
|
|
|
|
test_x = make_tests()
|
|
|
|
test_y = make_tests()
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
reprec = testdir.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
reprec.assert_outcomes(passed=4)
|
2013-12-16 14:47:59 +08:00
|
|
|
|
2015-03-22 06:06:25 +08:00
|
|
|
@pytest.mark.issue463
|
2015-08-07 13:51:59 +08:00
|
|
|
@pytest.mark.parametrize('attr', ['parametrise', 'parameterize',
|
2017-07-17 07:25:07 +08:00
|
|
|
'parameterise'])
|
2015-08-07 13:51:59 +08:00
|
|
|
def test_parametrize_misspelling(self, testdir, attr):
|
2015-03-22 06:06:25 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2015-08-07 13:51:59 +08:00
|
|
|
@pytest.mark.{0}("x", range(2))
|
2015-03-22 06:06:25 +08:00
|
|
|
def test_foo(x):
|
|
|
|
pass
|
2015-08-07 13:51:59 +08:00
|
|
|
""".format(attr))
|
2015-03-22 06:06:25 +08:00
|
|
|
reprec = testdir.inline_run('--collectonly')
|
|
|
|
failures = reprec.getfailures()
|
|
|
|
assert len(failures) == 1
|
2015-08-07 13:51:59 +08:00
|
|
|
expectederror = "MarkerError: test_foo has '{0}', spelling should be 'parametrize'".format(attr)
|
2015-03-22 06:06:25 +08:00
|
|
|
assert expectederror in failures[0].longrepr.reprcrash.message
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMetafuncFunctionalAuto(object):
|
2016-08-23 07:21:31 +08:00
|
|
|
"""
|
|
|
|
Tests related to automatically find out the correct scope for parametrized tests (#1832).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_parametrize_auto_scope(self, testdir):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def fixture():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal', ["dog", "cat"])
|
|
|
|
def test_1(animal):
|
|
|
|
assert animal in ('dog', 'cat')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal', ['fish'])
|
|
|
|
def test_2(animal):
|
|
|
|
assert animal == 'fish'
|
|
|
|
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 3 passed *'])
|
|
|
|
|
|
|
|
def test_parametrize_auto_scope_indirect(self, testdir):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def echo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal, echo', [("dog", 1), ("cat", 2)], indirect=['echo'])
|
|
|
|
def test_1(animal, echo):
|
|
|
|
assert animal in ('dog', 'cat')
|
|
|
|
assert echo in (1, 2, 3)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal, echo', [('fish', 3)], indirect=['echo'])
|
|
|
|
def test_2(animal, echo):
|
|
|
|
assert animal == 'fish'
|
|
|
|
assert echo in (1, 2, 3)
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 3 passed *'])
|
|
|
|
|
|
|
|
def test_parametrize_auto_scope_override_fixture(self, testdir):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def animal():
|
|
|
|
return 'fox'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal', ["dog", "cat"])
|
|
|
|
def test_1(animal):
|
|
|
|
assert animal in ('dog', 'cat')
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 2 passed *'])
|
|
|
|
|
|
|
|
def test_parametrize_all_indirects(self, testdir):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def animal(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def echo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal, echo', [("dog", 1), ("cat", 2)], indirect=True)
|
|
|
|
def test_1(animal, echo):
|
|
|
|
assert animal in ('dog', 'cat')
|
|
|
|
assert echo in (1, 2, 3)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('animal, echo', [("fish", 3)], indirect=True)
|
|
|
|
def test_2(animal, echo):
|
|
|
|
assert animal == 'fish'
|
|
|
|
assert echo in (1, 2, 3)
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 3 passed *'])
|
|
|
|
|
|
|
|
def test_parametrize_issue634(self, testdir):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def foo(request):
|
|
|
|
print('preparing foo-%d' % request.param)
|
|
|
|
return 'foo-%d' % request.param
|
|
|
|
|
|
|
|
def test_one(foo):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_two(foo):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test_two.test_with = (2, 3)
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
params = (1, 2, 3, 4)
|
|
|
|
if not 'foo' in metafunc.fixturenames:
|
|
|
|
return
|
|
|
|
|
|
|
|
test_with = getattr(metafunc.function, 'test_with', None)
|
|
|
|
if test_with:
|
|
|
|
params = test_with
|
|
|
|
metafunc.parametrize('foo', params, indirect=True)
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest("-s")
|
|
|
|
output = result.stdout.str()
|
|
|
|
assert output.count('preparing foo-2') == 1
|
|
|
|
assert output.count('preparing foo-3') == 1
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMarkersWithParametrization(object):
|
2013-05-22 21:24:58 +08:00
|
|
|
pytestmark = pytest.mark.issue308
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_simple_mark(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.foo
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
|
|
|
pytest.mark.bar((1, 3)),
|
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
items = testdir.getitems(s)
|
|
|
|
assert len(items) == 3
|
|
|
|
for item in items:
|
|
|
|
assert 'foo' in item.keywords
|
|
|
|
assert 'bar' not in items[0].keywords
|
|
|
|
assert 'bar' in items[1].keywords
|
|
|
|
assert 'bar' not in items[2].keywords
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_select_based_on_mark(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
|
|
|
pytest.mark.foo((2, 3)),
|
|
|
|
(3, 4),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
rec = testdir.inline_run("-m", 'foo')
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
assert len(passed) == 1
|
|
|
|
assert len(skipped) == 0
|
|
|
|
assert len(fail) == 0
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.xfail(reason="is this important to support??")
|
|
|
|
def test_nested_marks(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
2013-05-20 10:52:20 +08:00
|
|
|
mastermark = pytest.mark.foo(pytest.mark.bar)
|
2013-05-17 16:46:36 +08:00
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2013-05-20 10:52:20 +08:00
|
|
|
mastermark((1, 3)),
|
2013-05-17 16:46:36 +08:00
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
items = testdir.getitems(s)
|
|
|
|
assert len(items) == 3
|
|
|
|
for mark in ['foo', 'bar']:
|
|
|
|
assert mark not in items[0].keywords
|
|
|
|
assert mark in items[1].keywords
|
|
|
|
assert mark not in items[2].keywords
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_simple_xfail(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2013-05-20 10:52:20 +08:00
|
|
|
pytest.mark.xfail((1, 3)),
|
2013-05-17 16:46:36 +08:00
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
2013-05-20 10:52:20 +08:00
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
# xfail is skip??
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
2013-05-17 16:46:36 +08:00
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_simple_xfail_single_argname(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize("n", [
|
|
|
|
2,
|
|
|
|
pytest.mark.xfail(3),
|
|
|
|
4,
|
2013-05-17 16:46:36 +08:00
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_isEven(n):
|
|
|
|
assert n % 2 == 0
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_xfail_with_arg(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2013-05-22 21:24:58 +08:00
|
|
|
pytest.mark.xfail("True")((1, 3)),
|
2013-05-17 16:46:36 +08:00
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_xfail_with_kwarg(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
|
|
|
pytest.mark.xfail(reason="some bug")((1, 3)),
|
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_xfail_with_arg_and_kwarg(self, testdir):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2013-05-22 21:24:58 +08:00
|
|
|
pytest.mark.xfail("True", reason="some bug")((1, 3)),
|
2013-05-17 16:46:36 +08:00
|
|
|
(2, 3),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2016-08-18 06:15:14 +08:00
|
|
|
@pytest.mark.parametrize('strict', [True, False])
|
|
|
|
def test_xfail_passing_is_xpass(self, testdir, strict):
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2016-08-18 06:32:56 +08:00
|
|
|
pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict})((2, 3)),
|
2013-05-17 16:46:36 +08:00
|
|
|
(3, 4),
|
|
|
|
])
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
2016-08-18 06:15:14 +08:00
|
|
|
""".format(strict=strict)
|
2013-05-17 16:46:36 +08:00
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
2016-08-18 06:15:14 +08:00
|
|
|
passed, failed = (2, 1) if strict else (3, 0)
|
|
|
|
reprec.assertoutcome(passed=passed, failed=failed)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
def test_parametrize_called_in_generate_tests(self, testdir):
|
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
passingTestData = [(1, 2),
|
|
|
|
(2, 3)]
|
|
|
|
failingTestData = [(1, 3),
|
|
|
|
(2, 2)]
|
|
|
|
|
|
|
|
testData = passingTestData + [pytest.mark.xfail(d)
|
|
|
|
for d in failingTestData]
|
|
|
|
metafunc.parametrize(("n", "expected"), testData)
|
|
|
|
|
|
|
|
|
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
|
|
|
"""
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2, skipped=2)
|
2013-08-16 17:41:31 +08:00
|
|
|
|
|
|
|
|
2013-12-08 02:31:27 +08:00
|
|
|
@pytest.mark.issue290
|
2013-08-16 17:41:31 +08:00
|
|
|
def test_parametrize_ID_generation_string_int_works(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def myfixture():
|
|
|
|
return 'example'
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'limit', (0, '0'))
|
|
|
|
def test_limit(limit, myfixture):
|
|
|
|
return
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
2016-04-25 22:48:28 +08:00
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('strict', [True, False])
|
|
|
|
def test_parametrize_marked_value(self, testdir, strict):
|
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
|
|
|
pytest.param(
|
|
|
|
2,3,
|
|
|
|
marks=pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict}),
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
2,3,
|
|
|
|
marks=[pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict})],
|
|
|
|
),
|
|
|
|
])
|
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
|
|
|
""".format(strict=strict)
|
|
|
|
testdir.makepyfile(s)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
passed, failed = (0, 2) if strict else (2, 0)
|
|
|
|
reprec.assertoutcome(passed=passed, failed=failed)
|
|
|
|
|
|
|
|
|
2016-04-25 22:48:28 +08:00
|
|
|
def test_pytest_make_parametrize_id(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2016-04-26 15:23:57 +08:00
|
|
|
def pytest_make_parametrize_id(config, val):
|
2016-04-25 22:48:28 +08:00
|
|
|
return str(val * 2)
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", range(2))
|
|
|
|
def test_func(x):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func*0*PASS*",
|
|
|
|
"*test_func*2*PASS*",
|
|
|
|
])
|
2017-01-16 11:09:46 +08:00
|
|
|
|
|
|
|
def test_pytest_make_parametrize_id_with_argname(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_make_parametrize_id(config, val, argname):
|
|
|
|
return str(val * 2 if argname == 'x' else val * 10)
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", range(2))
|
2017-01-21 11:40:42 +08:00
|
|
|
def test_func_a(x):
|
2017-01-16 11:09:46 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("y", [1])
|
2017-01-21 11:40:42 +08:00
|
|
|
def test_func_b(y):
|
2017-01-16 11:09:46 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.stdout.fnmatch_lines([
|
2017-01-21 11:40:42 +08:00
|
|
|
"*test_func_a*0*PASS*",
|
|
|
|
"*test_func_a*2*PASS*",
|
|
|
|
"*test_func_b*10*PASS*",
|
2017-01-16 11:09:46 +08:00
|
|
|
])
|