2023-01-20 17:13:36 +08:00
|
|
|
import dataclasses
|
2020-02-14 16:50:45 +08:00
|
|
|
import itertools
|
2015-08-07 13:31:04 +08:00
|
|
|
import re
|
2016-04-02 10:45:44 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2020-02-14 16:50:45 +08:00
|
|
|
from typing import Any
|
2020-06-29 00:31:35 +08:00
|
|
|
from typing import cast
|
|
|
|
from typing import Dict
|
2020-02-14 16:50:45 +08:00
|
|
|
from typing import Iterator
|
|
|
|
from typing import List
|
|
|
|
from typing import Optional
|
2020-06-29 00:31:35 +08:00
|
|
|
from typing import Sequence
|
2020-02-14 16:50:45 +08:00
|
|
|
from typing import Tuple
|
|
|
|
from typing import Union
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2016-04-02 10:45:44 +08:00
|
|
|
import hypothesis
|
|
|
|
from hypothesis import strategies
|
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
|
|
|
from _pytest import fixtures
|
|
|
|
from _pytest import python
|
2020-07-31 14:46:56 +08:00
|
|
|
from _pytest.compat import getfuncargnames
|
2020-08-26 02:51:08 +08:00
|
|
|
from _pytest.compat import NOTSET
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
from _pytest.outcomes import fail
|
2020-12-10 13:47:58 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2023-08-10 01:13:45 +08:00
|
|
|
from _pytest.python import Function
|
2021-12-17 04:02:27 +08:00
|
|
|
from _pytest.python import IdMaker
|
2021-08-01 17:11:56 +08:00
|
|
|
from _pytest.scope import Scope
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2016-04-02 10:45:44 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMetafunc:
|
2019-11-19 00:36:15 +08:00
|
|
|
def Metafunc(self, func, config=None) -> python.Metafunc:
|
2020-07-18 17:35:13 +08:00
|
|
|
# The unit tests of this class check if things work correctly
|
2012-11-02 23:04:57 +08:00
|
|
|
# on the funcarg level, so we don't need a full blown
|
2020-07-18 17:35:13 +08:00
|
|
|
# initialization.
|
2020-02-14 16:50:45 +08:00
|
|
|
class FuncFixtureInfoMock:
|
2023-08-10 01:13:45 +08:00
|
|
|
name2fixturedefs: Dict[str, List[fixtures.FixtureDef[object]]] = {}
|
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
|
|
|
|
2023-08-10 01:13:45 +08:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class FixtureManagerMock:
|
|
|
|
config: Any
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class SessionMock:
|
|
|
|
_fixturemanager: FixtureManagerMock
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2019-11-19 00:36:15 +08:00
|
|
|
class DefinitionMock(python.FunctionDefinition):
|
2023-01-20 17:13:36 +08:00
|
|
|
_nodeid: str
|
|
|
|
obj: object
|
2018-03-18 05:04:22 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
names = getfuncargnames(func)
|
2020-10-06 09:13:05 +08:00
|
|
|
fixtureinfo: Any = FuncFixtureInfoMock(names)
|
2023-01-20 17:13:36 +08:00
|
|
|
definition: Any = DefinitionMock._create(obj=func, _nodeid="mock::nodeid")
|
2023-08-10 01:13:45 +08:00
|
|
|
definition._fixtureinfo = fixtureinfo
|
|
|
|
definition.session = SessionMock(FixtureManagerMock({}))
|
2020-12-27 02:49:17 +08:00
|
|
|
return python.Metafunc(definition, fixtureinfo, config, _ispytest=True)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_no_funcargs(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def function():
|
|
|
|
pass
|
2018-03-18 04:42:43 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(function)
|
|
|
|
assert not metafunc.fixturenames
|
|
|
|
repr(metafunc._calls)
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_function_basic(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(arg1, arg2="qwe"):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
assert len(metafunc.fixturenames) == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "arg1" in metafunc.fixturenames
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc.function is func
|
|
|
|
assert metafunc.cls is None
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_error(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2017-07-17 07:25:08 +08:00
|
|
|
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]))
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
with pytest.raises(TypeError, match="^ids must be a callable or an iterable$"):
|
2020-07-10 14:44:14 +08:00
|
|
|
metafunc.parametrize("y", [5, 6], ids=42) # type: ignore[arg-type]
|
2019-11-12 21:52:19 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_error_iterator(self) -> None:
|
2019-11-12 21:52:19 +08:00
|
|
|
def func(x):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
class Exc(Exception):
|
|
|
|
def __repr__(self):
|
|
|
|
return "Exc(from_gen)"
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def gen() -> Iterator[Union[int, None, Exc]]:
|
2019-11-12 21:52:19 +08:00
|
|
|
yield 0
|
|
|
|
yield None
|
|
|
|
yield Exc()
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func)
|
2020-02-14 16:50:45 +08:00
|
|
|
# When the input is an iterator, only len(args) are taken,
|
|
|
|
# so the bad Exc isn't reached.
|
2020-07-10 14:44:14 +08:00
|
|
|
metafunc.parametrize("x", [1, 2], ids=gen()) # type: ignore[arg-type]
|
2023-08-10 01:13:45 +08:00
|
|
|
assert [(x.params, x.id) for x in metafunc._calls] == [
|
2019-11-12 21:52:19 +08:00
|
|
|
({"x": 1}, "0"),
|
|
|
|
({"x": 2}, "2"),
|
|
|
|
]
|
|
|
|
with pytest.raises(
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
fail.Exception,
|
2019-11-12 21:52:19 +08:00
|
|
|
match=(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
r"In func: ids contains unsupported value Exc\(from_gen\) \(type: <class .*Exc'>\) at index 2. "
|
|
|
|
r"Supported types are: .*"
|
2019-11-12 21:52:19 +08:00
|
|
|
),
|
|
|
|
):
|
2020-07-10 14:44:14 +08:00
|
|
|
metafunc.parametrize("x", [1, 2, 3], ids=gen()) # type: ignore[arg-type]
|
2019-11-12 21:52:19 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_bad_scope(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2016-09-07 02:16:25 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2018-10-04 07:07:59 +08:00
|
|
|
with pytest.raises(
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
fail.Exception,
|
2018-10-04 07:07:59 +08:00
|
|
|
match=r"parametrize\(\) call in func got an unexpected scope value 'doggy'",
|
|
|
|
):
|
2020-07-10 14:44:14 +08:00
|
|
|
metafunc.parametrize("x", [1], scope="doggy") # type: ignore[arg-type]
|
2016-09-07 02:16:25 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_request_name(self, pytester: Pytester) -> None:
|
2019-11-14 04:51:14 +08:00
|
|
|
"""Show proper error when 'request' is used as a parameter name in parametrize (#6183)"""
|
|
|
|
|
|
|
|
def func(request):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
with pytest.raises(
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
fail.Exception,
|
2019-11-14 04:51:14 +08:00
|
|
|
match=r"'request' is a reserved name and cannot be used in @pytest.mark.parametrize",
|
|
|
|
):
|
|
|
|
metafunc.parametrize("request", [1])
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_find_parametrized_scope(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for _find_parametrized_scope (#3941)."""
|
2018-09-15 08:13:58 +08:00
|
|
|
from _pytest.python import _find_parametrized_scope
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyFixtureDef:
|
2023-01-20 17:13:36 +08:00
|
|
|
_scope: Scope
|
2018-09-15 08:13:58 +08:00
|
|
|
|
2020-06-29 00:31:35 +08:00
|
|
|
fixtures_defs = cast(
|
2020-08-01 18:06:13 +08:00
|
|
|
Dict[str, Sequence[fixtures.FixtureDef[object]]],
|
2020-06-29 00:31:35 +08:00
|
|
|
dict(
|
2021-08-01 17:11:56 +08:00
|
|
|
session_fix=[DummyFixtureDef(Scope.Session)],
|
|
|
|
package_fix=[DummyFixtureDef(Scope.Package)],
|
|
|
|
module_fix=[DummyFixtureDef(Scope.Module)],
|
|
|
|
class_fix=[DummyFixtureDef(Scope.Class)],
|
|
|
|
func_fix=[DummyFixtureDef(Scope.Function)],
|
2023-08-06 21:59:54 +08:00
|
|
|
mixed_fix=[DummyFixtureDef(Scope.Module), DummyFixtureDef(Scope.Class)],
|
2020-06-29 00:31:35 +08:00
|
|
|
),
|
2018-09-15 08:13:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# use arguments to determine narrow scope; the cause of the bug is that it would look on all
|
|
|
|
# fixture defs given to the method
|
|
|
|
def find_scope(argnames, indirect):
|
|
|
|
return _find_parametrized_scope(argnames, fixtures_defs, indirect=indirect)
|
|
|
|
|
2021-08-01 17:11:56 +08:00
|
|
|
assert find_scope(["func_fix"], indirect=True) == Scope.Function
|
|
|
|
assert find_scope(["class_fix"], indirect=True) == Scope.Class
|
|
|
|
assert find_scope(["module_fix"], indirect=True) == Scope.Module
|
|
|
|
assert find_scope(["package_fix"], indirect=True) == Scope.Package
|
|
|
|
assert find_scope(["session_fix"], indirect=True) == Scope.Session
|
2018-09-15 08:13:58 +08:00
|
|
|
|
2021-08-01 17:11:56 +08:00
|
|
|
assert find_scope(["class_fix", "func_fix"], indirect=True) == Scope.Function
|
|
|
|
assert find_scope(["func_fix", "session_fix"], indirect=True) == Scope.Function
|
|
|
|
assert find_scope(["session_fix", "class_fix"], indirect=True) == Scope.Class
|
|
|
|
assert (
|
|
|
|
find_scope(["package_fix", "session_fix"], indirect=True) == Scope.Package
|
|
|
|
)
|
|
|
|
assert find_scope(["module_fix", "session_fix"], indirect=True) == Scope.Module
|
2018-09-15 08:13:58 +08:00
|
|
|
|
|
|
|
# when indirect is False or is not for all scopes, always use function
|
2021-08-01 17:11:56 +08:00
|
|
|
assert (
|
|
|
|
find_scope(["session_fix", "module_fix"], indirect=False) == Scope.Function
|
|
|
|
)
|
2018-09-15 08:13:58 +08:00
|
|
|
assert (
|
|
|
|
find_scope(["session_fix", "module_fix"], indirect=["module_fix"])
|
2021-08-01 17:11:56 +08:00
|
|
|
== Scope.Function
|
2018-09-15 08:13:58 +08:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
find_scope(
|
|
|
|
["session_fix", "module_fix"], indirect=["session_fix", "module_fix"]
|
|
|
|
)
|
2021-08-01 17:11:56 +08:00
|
|
|
== Scope.Module
|
2018-09-15 08:13:58 +08:00
|
|
|
)
|
2023-08-06 21:59:54 +08:00
|
|
|
assert find_scope(["mixed_fix"], indirect=True) == Scope.Class
|
2018-09-15 08:13:58 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_and_id(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x", [1, 2], ids=["basic", "advanced"])
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc.parametrize("y", ["abc", "def"])
|
|
|
|
ids = [x.id for x in metafunc._calls]
|
|
|
|
assert ids == ["basic-abc", "basic-def", "advanced-abc", "advanced-def"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_and_id_unicode(self) -> None:
|
2016-09-03 05:25:26 +08:00
|
|
|
"""Allow unicode strings for "ids" parameter in Python 2 (##1905)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2016-09-03 05:25:26 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2019-06-03 06:32:00 +08:00
|
|
|
metafunc.parametrize("x", [1, 2], ids=["basic", "advanced"])
|
2016-09-03 05:25:26 +08:00
|
|
|
ids = [x.id for x in metafunc._calls]
|
2019-06-03 06:32:00 +08:00
|
|
|
assert ids == ["basic", "advanced"]
|
2016-09-03 05:25:26 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_with_wrong_number_of_ids(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2013-05-29 10:59:47 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
with pytest.raises(fail.Exception):
|
2018-10-04 07:07:59 +08:00
|
|
|
metafunc.parametrize("x", [1, 2], ids=["basic"])
|
2013-05-29 10:59:47 +08:00
|
|
|
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
with pytest.raises(fail.Exception):
|
2018-10-04 07:07:59 +08:00
|
|
|
metafunc.parametrize(
|
2018-05-23 22:48:46 +08:00
|
|
|
("x", "y"), [("abc", "def"), ("ghi", "jkl")], ids=["one"]
|
2018-10-04 07:07:59 +08:00
|
|
|
)
|
2013-05-29 10:59:47 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_ids_iterator_without_mark(self) -> None:
|
2019-11-12 21:52:19 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
|
|
|
|
|
|
|
it = itertools.count()
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize("x", [1, 2], ids=it)
|
|
|
|
metafunc.parametrize("y", [3, 4], ids=it)
|
|
|
|
ids = [x.id for x in metafunc._calls]
|
|
|
|
assert ids == ["0-2", "0-3", "1-2", "1-3"]
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
metafunc.parametrize("x", [1, 2], ids=it)
|
|
|
|
metafunc.parametrize("y", [3, 4], ids=it)
|
|
|
|
ids = [x.id for x in metafunc._calls]
|
|
|
|
assert ids == ["4-6", "4-7", "5-6", "5-7"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_empty_list(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#510"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(y):
|
|
|
|
pass
|
2017-12-17 22:25:56 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MockConfig:
|
2017-12-17 22:25:56 +08:00
|
|
|
def getini(self, name):
|
2018-05-23 22:48:46 +08:00
|
|
|
return ""
|
2017-12-17 22:25:56 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hook(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def pytest_make_parametrize_id(self, **kw):
|
|
|
|
pass
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func, MockConfig())
|
2016-04-22 02:57:53 +08:00
|
|
|
metafunc.parametrize("y", [])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "skip" == metafunc._calls[0].marks[0].name
|
2016-04-22 02:57:53 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_with_userobjects(self) -> None:
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
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())
|
2018-05-23 22:48:46 +08:00
|
|
|
@hypothesis.settings(
|
|
|
|
deadline=400.0
|
|
|
|
) # very close to std deadline and CI boxes are not reliable in CPU power
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idval_hypothesis(self, value) -> None:
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
escaped = IdMaker([], [], None, None, None, None, None)._idval(value, "a", 6)
|
2019-06-03 06:32:00 +08:00
|
|
|
assert isinstance(escaped, str)
|
2018-11-19 06:32:32 +08:00
|
|
|
escaped.encode("ascii")
|
2016-04-02 10:45:44 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_unicode_idval(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that Unicode strings outside the ASCII character set get
|
2016-04-02 00:27:17 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
values = [
|
2020-07-18 17:35:13 +08:00
|
|
|
("", r""),
|
|
|
|
("ascii", r"ascii"),
|
|
|
|
("ação", r"a\xe7\xe3o"),
|
|
|
|
("josé@blah.com", r"jos\xe9@blah.com"),
|
2017-07-17 07:25:10 +08:00
|
|
|
(
|
2020-07-18 17:35:13 +08:00
|
|
|
r"δοκ.ιμή@παράδειγμα.δοκιμή",
|
|
|
|
r"\u03b4\u03bf\u03ba.\u03b9\u03bc\u03ae@\u03c0\u03b1\u03c1\u03ac\u03b4\u03b5\u03b9\u03b3"
|
|
|
|
r"\u03bc\u03b1.\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae",
|
2017-07-17 07:25:10 +08:00
|
|
|
),
|
2015-09-30 04:57:49 +08:00
|
|
|
]
|
|
|
|
for val, expected in values:
|
2021-12-17 04:02:27 +08:00
|
|
|
assert (
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
|
|
|
|
== expected
|
2021-12-17 04:02:27 +08:00
|
|
|
)
|
2015-09-30 05:20:30 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_unicode_idval_with_config(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for expected behavior to obtain ids with
|
2019-05-22 02:56:52 +08:00
|
|
|
disable_test_id_escaping_and_forfeit_all_rights_to_community_support
|
2020-07-18 17:35:13 +08:00
|
|
|
option (#5294)."""
|
2019-05-22 02:56:52 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MockConfig:
|
2019-05-22 02:56:52 +08:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hook(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def pytest_make_parametrize_id(self, **kw):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getini(self, name):
|
|
|
|
return self.config[name]
|
|
|
|
|
|
|
|
option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support"
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
values: List[Tuple[str, Any, str]] = [
|
2019-06-03 06:32:00 +08:00
|
|
|
("ação", MockConfig({option: True}), "ação"),
|
|
|
|
("ação", MockConfig({option: False}), "a\\xe7\\xe3o"),
|
2020-10-06 09:13:05 +08:00
|
|
|
]
|
2019-05-22 02:56:52 +08:00
|
|
|
for val, config, expected in values:
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
actual = IdMaker([], [], None, None, config, None, None)._idval(val, "a", 6)
|
2020-02-14 16:50:45 +08:00
|
|
|
assert actual == expected
|
2019-05-22 02:56:52 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_bytes_idval(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for the expected behavior to obtain ids for parametrized
|
|
|
|
bytes values: bytes objects are always escaped using "binary escape"."""
|
2015-09-30 05:20:30 +08:00
|
|
|
values = [
|
2020-07-18 17:35:13 +08:00
|
|
|
(b"", r""),
|
|
|
|
(b"\xc3\xb4\xff\xe4", r"\xc3\xb4\xff\xe4"),
|
|
|
|
(b"ascii", r"ascii"),
|
|
|
|
("αρά".encode(), r"\xce\xb1\xcf\x81\xce\xac"),
|
2015-09-30 05:20:30 +08:00
|
|
|
]
|
|
|
|
for val, expected in values:
|
2021-12-17 04:02:27 +08:00
|
|
|
assert (
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
|
|
|
|
== expected
|
2021-12-17 04:02:27 +08:00
|
|
|
)
|
2015-09-30 04:57:49 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_class_or_function_idval(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for the expected behavior to obtain ids for parametrized
|
|
|
|
values that are classes or functions: their __name__."""
|
2017-11-30 18:29:05 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestClass:
|
2017-11-30 18:29:05 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_function():
|
|
|
|
pass
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
values = [(TestClass, "TestClass"), (test_function, "test_function")]
|
2017-11-30 18:19:29 +08:00
|
|
|
for val, expected in values:
|
2021-12-17 04:02:27 +08:00
|
|
|
assert (
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
|
|
|
|
== expected
|
2021-12-17 04:02:27 +08:00
|
|
|
)
|
2017-11-30 18:19:29 +08:00
|
|
|
|
2020-08-26 02:51:08 +08:00
|
|
|
def test_notset_idval(self) -> None:
|
|
|
|
"""Test that a NOTSET value (used by an empty parameterset) generates
|
|
|
|
a proper ID.
|
|
|
|
|
|
|
|
Regression test for #7686.
|
|
|
|
"""
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
assert (
|
|
|
|
IdMaker([], [], None, None, None, None, None)._idval(NOTSET, "a", 0) == "a0"
|
|
|
|
)
|
2020-08-26 02:51:08 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_autoname(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#250"""
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
|
|
|
("a", "b"),
|
|
|
|
[pytest.param("string", 1.0), pytest.param("st-ring", 2.0)],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result == ["string-1.0", "st-ring-2.0"]
|
|
|
|
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
|
|
|
("a", "b"),
|
|
|
|
[pytest.param(object(), 1.0), pytest.param(object(), object())],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
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
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
("a", "b"), [pytest.param({}, b"\xc3\xb4")], None, None, None, None, None
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result == ["a0-\\xc3\\xb4"]
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_bytes_regex(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
("a"), [pytest.param(re.compile(b"foo"), 1.0)], None, None, None, None, None
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2016-03-06 01:23:13 +08:00
|
|
|
assert result == ["foo"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_native_strings(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-05-23 22:48:46 +08:00
|
|
|
("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({7}, set("seven")),
|
|
|
|
pytest.param(tuple("eight"), (8, -8, 8)),
|
|
|
|
pytest.param(b"\xc3\xb4", b"name"),
|
2019-06-03 06:32:00 +08:00
|
|
|
pytest.param(b"\xc3\xb4", "other"),
|
2021-07-13 03:27:24 +08:00
|
|
|
pytest.param(1.0j, -2.0j),
|
2018-05-23 22:48:46 +08:00
|
|
|
],
|
2021-12-17 04:02:27 +08:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-06-26 21:35:27 +08:00
|
|
|
assert result == [
|
|
|
|
"1.0--1.1",
|
|
|
|
"2--202",
|
|
|
|
"three-three hundred",
|
|
|
|
"True-False",
|
|
|
|
"None-None",
|
|
|
|
"foo-bar",
|
|
|
|
"str-int",
|
|
|
|
"a7-b7",
|
|
|
|
"a8-b8",
|
|
|
|
"a9-b9",
|
|
|
|
"\\xc3\\xb4-name",
|
|
|
|
"\\xc3\\xb4-other",
|
2021-07-13 03:27:24 +08:00
|
|
|
"1j-(-0-2j)",
|
2018-06-26 21:35:27 +08:00
|
|
|
]
|
2015-08-07 13:31:04 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_non_printable_characters(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-11-19 06:32:32 +08:00
|
|
|
("s", "n"),
|
|
|
|
[
|
|
|
|
pytest.param("\x00", 1),
|
|
|
|
pytest.param("\x05", 2),
|
|
|
|
pytest.param(b"\x00", 3),
|
|
|
|
pytest.param(b"\x05", 4),
|
2018-11-19 07:12:43 +08:00
|
|
|
pytest.param("\t", 5),
|
|
|
|
pytest.param(b"\t", 6),
|
2018-11-19 06:32:32 +08:00
|
|
|
],
|
2021-12-17 04:02:27 +08:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-11-19 07:12:43 +08:00
|
|
|
assert result == ["\\x00-1", "\\x05-2", "\\x00-3", "\\x05-4", "\\t-5", "\\t-6"]
|
2018-11-19 06:32:32 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_manual_ids_must_be_printable(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-11-19 06:39:17 +08:00
|
|
|
("s",),
|
|
|
|
[
|
|
|
|
pytest.param("x00", id="hello \x00"),
|
|
|
|
pytest.param("x05", id="hello \x05"),
|
|
|
|
],
|
2021-12-17 04:02:27 +08:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-11-19 06:39:17 +08:00
|
|
|
assert result == ["hello \\x00", "hello \\x05"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_enum(self) -> None:
|
2015-08-07 13:31:04 +08:00
|
|
|
enum = pytest.importorskip("enum")
|
|
|
|
e = enum.Enum("Foo", "one, two")
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
("a", "b"), [pytest.param(e.one, e.two)], None, None, None, None, None
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2021-07-12 22:32:27 +08:00
|
|
|
assert result == ["Foo.one-Foo.two"]
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_idfn(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#351"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def ids(val: object) -> Optional[str]:
|
2014-04-18 03:08:49 +08:00
|
|
|
if isinstance(val, Exception):
|
|
|
|
return repr(val)
|
2020-02-14 16:50:45 +08:00
|
|
|
return None
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-05-23 22:48:46 +08:00
|
|
|
("a", "b"),
|
|
|
|
[
|
|
|
|
pytest.param(10.0, IndexError()),
|
|
|
|
pytest.param(20, KeyError()),
|
|
|
|
pytest.param("three", [1, 2, 3]),
|
|
|
|
],
|
2021-12-17 04:02:27 +08:00
|
|
|
ids,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result == ["10.0-IndexError()", "20-KeyError()", "three-b2"]
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_idfn_unique_names(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#351"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def ids(val: object) -> str:
|
2018-05-23 22:48:46 +08:00
|
|
|
return "a"
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-05-23 22:48:46 +08:00
|
|
|
("a", "b"),
|
|
|
|
[
|
|
|
|
pytest.param(10.0, IndexError()),
|
|
|
|
pytest.param(20, KeyError()),
|
|
|
|
pytest.param("three", [1, 2, 3]),
|
|
|
|
],
|
2021-12-17 04:02:27 +08:00
|
|
|
ids,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result == ["a-a0", "a-a1", "a-a2"]
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_idfn_and_config(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for expected behavior to create ids with idfn and
|
2019-05-22 02:56:52 +08:00
|
|
|
disable_test_id_escaping_and_forfeit_all_rights_to_community_support
|
2020-07-18 17:35:13 +08:00
|
|
|
option (#5294).
|
2019-05-22 02:56:52 +08:00
|
|
|
"""
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MockConfig:
|
2019-05-22 02:56:52 +08:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hook(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def pytest_make_parametrize_id(self, **kw):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getini(self, name):
|
|
|
|
return self.config[name]
|
|
|
|
|
|
|
|
option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support"
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
values: List[Tuple[Any, str]] = [
|
2019-06-03 06:32:00 +08:00
|
|
|
(MockConfig({option: True}), "ação"),
|
2019-05-22 02:56:52 +08:00
|
|
|
(MockConfig({option: False}), "a\\xe7\\xe3o"),
|
2020-10-06 09:13:05 +08:00
|
|
|
]
|
2019-05-22 02:56:52 +08:00
|
|
|
for config, expected in values:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
("a",),
|
|
|
|
[pytest.param("string")],
|
|
|
|
lambda _: "ação",
|
|
|
|
None,
|
|
|
|
config,
|
|
|
|
None,
|
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2019-05-22 02:56:52 +08:00
|
|
|
assert result == [expected]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_ids_and_config(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Unit test for expected behavior to create ids with ids and
|
2019-05-22 02:56:52 +08:00
|
|
|
disable_test_id_escaping_and_forfeit_all_rights_to_community_support
|
2020-07-18 17:35:13 +08:00
|
|
|
option (#5294).
|
2019-05-22 02:56:52 +08:00
|
|
|
"""
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class MockConfig:
|
2019-05-22 02:56:52 +08:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hook(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def pytest_make_parametrize_id(self, **kw):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getini(self, name):
|
|
|
|
return self.config[name]
|
|
|
|
|
|
|
|
option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support"
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
values: List[Tuple[Any, str]] = [
|
2019-06-03 06:32:00 +08:00
|
|
|
(MockConfig({option: True}), "ação"),
|
2019-05-22 02:56:52 +08:00
|
|
|
(MockConfig({option: False}), "a\\xe7\\xe3o"),
|
2020-10-06 09:13:05 +08:00
|
|
|
]
|
2019-05-22 02:56:52 +08:00
|
|
|
for config, expected in values:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
("a",), [pytest.param("string")], None, ["ação"], config, None, None
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2019-05-22 02:56:52 +08:00
|
|
|
assert result == [expected]
|
|
|
|
|
2023-10-28 04:23:57 +08:00
|
|
|
def test_idmaker_duplicated_empty_str(self) -> None:
|
|
|
|
"""Regression test for empty strings parametrized more than once (#11563)."""
|
|
|
|
result = IdMaker(
|
|
|
|
("a",), [pytest.param(""), pytest.param("")], None, None, None, None, None
|
|
|
|
).make_unique_parameterset_ids()
|
|
|
|
assert result == ["0", "1"]
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_ids_exception(self, pytester: Pytester) -> None:
|
2016-12-31 01:37:09 +08:00
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
:param pytester: the instance of Pytester class, a temporary
|
2016-12-31 01:37:09 +08:00
|
|
|
test directory.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-12-31 01:37:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def ids(arg):
|
|
|
|
raise Exception("bad ids")
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
|
|
|
|
def test_foo(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2018-12-15 03:09:27 +08:00
|
|
|
"*Exception: bad ids",
|
2019-06-15 21:16:59 +08:00
|
|
|
"*test_foo: error raised while trying to determine id of parameter 'arg' at position 0",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2014-04-18 03:08:49 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_ids_returns_non_string(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-02-08 03:07:20 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def ids(d):
|
|
|
|
return d
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg", ({1: 2}, {3, 4}), ids=ids)
|
|
|
|
def test(arg):
|
|
|
|
assert arg
|
2019-11-12 21:52:19 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg", (1, 2.0, True), ids=ids)
|
|
|
|
def test_int(arg):
|
|
|
|
assert arg
|
2019-02-08 03:07:20 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-vv", "-s")
|
2019-11-12 21:52:19 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_parametrize_ids_returns_non_string.py::test[arg0] PASSED",
|
|
|
|
"test_parametrize_ids_returns_non_string.py::test[arg1] PASSED",
|
|
|
|
"test_parametrize_ids_returns_non_string.py::test_int[1] PASSED",
|
|
|
|
"test_parametrize_ids_returns_non_string.py::test_int[2.0] PASSED",
|
|
|
|
"test_parametrize_ids_returns_non_string.py::test_int[True] PASSED",
|
|
|
|
]
|
|
|
|
)
|
2019-02-08 03:07:20 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_ids(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
|
|
|
("a", "b"),
|
|
|
|
[pytest.param(1, 2), pytest.param(3, 4)],
|
|
|
|
None,
|
|
|
|
["a", None],
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2016-03-20 03:23:49 +08:00
|
|
|
assert result == ["a", "3-4"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_paramset_id(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
2018-05-23 22:48:46 +08:00
|
|
|
("a", "b"),
|
|
|
|
[pytest.param(1, 2, id="me"), pytest.param(3, 4, id="you")],
|
2021-12-17 04:02:27 +08:00
|
|
|
None,
|
|
|
|
["a", None],
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2016-09-07 17:00:27 +08:00
|
|
|
assert result == ["me", "you"]
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_idmaker_with_ids_unique_names(self) -> None:
|
2021-12-17 04:02:27 +08:00
|
|
|
result = IdMaker(
|
|
|
|
("a"),
|
|
|
|
list(map(pytest.param, [1, 2, 3, 4, 5])),
|
|
|
|
None,
|
|
|
|
["a", "a", "b", "c", "b"],
|
|
|
|
None,
|
|
|
|
None,
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
None,
|
2021-12-17 04:02:27 +08:00
|
|
|
).make_unique_parameterset_ids()
|
2016-03-23 05:58:28 +08:00
|
|
|
assert result == ["a0", "a1", "b0", "c", "b1"]
|
2016-03-20 03:42:47 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x", [1], indirect=True)
|
|
|
|
metafunc.parametrize("y", [2, 3], indirect=True)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert len(metafunc._calls) == 2
|
2017-07-17 07:25:08 +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
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_list(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-08-02 21:40:40 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x, y", [("a", "b")], indirect=["x"])
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[0].params == dict(x="a", y="b")
|
|
|
|
# Since `y` is a direct parameter, its pseudo-fixture would
|
|
|
|
# be registered.
|
|
|
|
assert list(metafunc._arg2fixturedefs.keys()) == ["y"]
|
2015-08-02 21:40:40 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_list_all(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-08-02 21:40:40 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "y"])
|
|
|
|
assert metafunc._calls[0].params == dict(x="a", y="b")
|
2023-08-10 01:13:45 +08:00
|
|
|
assert list(metafunc._arg2fixturedefs.keys()) == []
|
2015-08-02 21:40:40 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_list_empty(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-08-02 21:40:40 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x, y", [("a", "b")], indirect=[])
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[0].params == dict(x="a", y="b")
|
|
|
|
assert list(metafunc._arg2fixturedefs.keys()) == ["x", "y"]
|
2015-08-02 21:40:40 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_wrong_type(self) -> None:
|
2019-07-12 23:47:52 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
|
|
|
|
|
|
|
metafunc = self.Metafunc(func)
|
|
|
|
with pytest.raises(
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
fail.Exception,
|
2019-07-12 23:47:52 +08:00
|
|
|
match="In func: expected Sequence or boolean for indirect, got dict",
|
|
|
|
):
|
2020-07-10 14:44:14 +08:00
|
|
|
metafunc.parametrize("x, y", [("a", "b")], indirect={}) # type: ignore[arg-type]
|
2019-07-12 23:47:52 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_indirect_list_functional(self, pytester: Pytester) -> None:
|
2015-08-04 05:02:03 +08:00
|
|
|
"""
|
2019-04-27 22:25:37 +08:00
|
|
|
#714
|
2015-08-04 05:02:03 +08:00
|
|
|
Test parametrization with 'indirect' parameter applied on
|
2021-04-20 04:39:08 +08:00
|
|
|
particular arguments. As y is direct, its value should
|
|
|
|
be used directly rather than being passed to the fixture y.
|
2015-08-04 05:02:03 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
:param pytester: the instance of Pytester class, a temporary
|
2015-08-04 05:02:03 +08:00
|
|
|
test directory.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_simple*a-b*", "*1 passed*"])
|
2015-08-02 21:40:40 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_list_error(self) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def func(x, y):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-08-03 04:53:44 +08:00
|
|
|
metafunc = self.Metafunc(func)
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
with pytest.raises(fail.Exception):
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "z"])
|
2015-08-03 04:06:24 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_uses_no_fixture_error_indirect_false(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2015-08-09 01:20:09 +08:00
|
|
|
"""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.
|
2019-04-27 22:25:37 +08:00
|
|
|
|
|
|
|
#714
|
2015-08-09 01:20:09 +08:00
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-09 01:20:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=False)
|
|
|
|
def test_simple(x):
|
|
|
|
assert len(x) == 3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*uses no argument 'y'*"])
|
2015-08-09 01:20:09 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_uses_no_fixture_error_indirect_true(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-09 01:20:09 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
|
2015-08-09 01:20:09 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_uses_no_fixture_error_indirect_string(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-24 16:47:06 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
|
2016-07-24 16:47:06 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_indirect_uses_no_fixture_error_indirect_list(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-09 01:20:09 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
|
2015-08-09 01:20:09 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_argument_not_in_indirect_list(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#714"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-23 22:49:20 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*uses no argument 'y'*"])
|
2016-07-23 22:49:20 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
def test_parametrize_gives_indicative_error_on_function_with_default_argument(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-04-05 00:44:01 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('x, y', [('a', 'b')])
|
|
|
|
def test_simple(x, y=1):
|
|
|
|
assert len(x) == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*already takes an argument 'y' with a default value"]
|
|
|
|
)
|
2018-04-05 00:44:01 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_functional(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_simple*1-2*", "*test_simple*2-2*", "*2 passed*"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_onearg(self) -> None:
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(lambda x: None)
|
2017-07-17 07:25:08 +08:00
|
|
|
metafunc.parametrize("x", [1, 2])
|
2012-11-02 23:04:57 +08:00
|
|
|
assert len(metafunc._calls) == 2
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[0].params == dict(x=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc._calls[0].id == "1"
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[1].params == dict(x=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc._calls[1].id == "2"
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_onearg_indirect(self) -> None:
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc = self.Metafunc(lambda x: None)
|
2017-07-17 07:25:08 +08:00
|
|
|
metafunc.parametrize("x", [1, 2], indirect=True)
|
2012-11-02 23:04:57 +08:00
|
|
|
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"
|
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_twoargs(self) -> None:
|
2017-07-17 07:25:08 +08:00
|
|
|
metafunc = self.Metafunc(lambda x, y: None)
|
|
|
|
metafunc.parametrize(("x", "y"), [(1, 2), (3, 4)])
|
2012-11-02 23:04:57 +08:00
|
|
|
assert len(metafunc._calls) == 2
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[0].params == dict(x=1, y=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc._calls[0].id == "1-2"
|
2023-08-10 01:13:45 +08:00
|
|
|
assert metafunc._calls[1].params == dict(x=3, y=4)
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc._calls[1].id == "3-4"
|
|
|
|
|
2023-08-10 01:13:45 +08:00
|
|
|
def test_high_scoped_parametrize_reordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg2", [3, 4])
|
|
|
|
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
|
|
|
|
def test1(arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test2():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
|
|
|
|
def test3(arg1):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest("--collect-only")
|
|
|
|
result.stdout.re_match_lines(
|
|
|
|
[
|
|
|
|
r" <Function test1\[0-3\]>",
|
|
|
|
r" <Function test1\[0-4\]>",
|
|
|
|
r" <Function test3\[0\]>",
|
|
|
|
r" <Function test1\[1-3\]>",
|
|
|
|
r" <Function test1\[1-4\]>",
|
|
|
|
r" <Function test3\[1\]>",
|
|
|
|
r" <Function test1\[2-3\]>",
|
|
|
|
r" <Function test1\[2-4\]>",
|
|
|
|
r" <Function test3\[2\]>",
|
|
|
|
r" <Function test2>",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_multiple_times(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_CSV(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-05-28 16:32:54 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("x, y,", [(1,2), (2,3)])
|
|
|
|
def test_func(x, y):
|
|
|
|
assert x+1 == y
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-05-28 16:32:54 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_class_scenarios(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
# 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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
*test_1*1*
|
|
|
|
*test_2*1*
|
|
|
|
*test_3*1*
|
|
|
|
*test_1*2*
|
|
|
|
*test_2*2*
|
|
|
|
*test_3*2*
|
|
|
|
*6 passed*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2015-09-17 04:52:37 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMetafuncFunctional:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_attributes(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
# assumes that generate/provide runs in the same process
|
2019-06-03 06:53:45 +08:00
|
|
|
import sys, pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('metafunc', [metafunc])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def metafunc(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
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__
|
2019-06-03 06:53:45 +08:00
|
|
|
unbound = TestClass.test_method
|
2012-11-02 23:04:57 +08:00
|
|
|
assert metafunc.function == unbound
|
|
|
|
assert metafunc.cls == TestClass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(p, "-v")
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_two_functions(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('arg1', [10, 20], ids=['0', '1'])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_func1(arg1):
|
|
|
|
assert arg1 == 10
|
2018-12-02 02:41:59 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_func2(arg1):
|
|
|
|
assert arg1 in (10, 20)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_func1*0*PASS*",
|
|
|
|
"*test_func1*1*FAIL*",
|
|
|
|
"*test_func2*PASS*",
|
2018-12-02 02:41:59 +08:00
|
|
|
"*test_func2*PASS*",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*1 failed, 3 passed*",
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_noself_in_method(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(p)
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_generate_tests_in_class(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('hello', ['world'], ids=['hellow'])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_myfunc(self, hello):
|
|
|
|
assert hello == "world"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_myfunc*hello*PASS*", "*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_two_functions_not_same_instance(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('arg1', [10, 20], ids=["0", "1"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_func*0*PASS*", "*test_func*1*PASS*", "*2 pass*"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue28_setup_method_in_generate_tests(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-12-02 02:41:59 +08:00
|
|
|
metafunc.parametrize('arg1', [1])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(p)
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_functional2(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize("arg1", [1,2])
|
|
|
|
metafunc.parametrize("arg2", [4,5])
|
|
|
|
def test_hello(arg1, arg2):
|
|
|
|
assert 0, (arg1, arg2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*(1, 4)*", "*(1, 5)*", "*(2, 4)*", "*(2, 5)*", "*4 failed*"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_and_inner_getfixturevalue(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_func1*1*PASS*", "*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_on_setup_arg(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_func*1*PASS*", "*1 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_ids(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-09-28 01:42:55 +08:00
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
["*test_function*basic*PASSED", "*test_function*advanced*FAILED"]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_without_ids(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.parametrize(("a", "b"),
|
|
|
|
[(1,object()), (1.3,object())])
|
|
|
|
|
|
|
|
def test_function(a, b):
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
*test_function*1-b0*
|
|
|
|
*test_function*1.3-b1*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_with_None_in_ids(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-03-20 03:38:24 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2016-03-20 03:38:24 +08:00
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
[
|
|
|
|
"*test_function*basic*PASSED*",
|
|
|
|
"*test_function*1-1*PASSED*",
|
|
|
|
"*test_function*advanced*FAILED*",
|
|
|
|
]
|
|
|
|
)
|
2016-03-20 03:38:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_parametrized_empty_ids(self, pytester: Pytester) -> None:
|
2016-08-24 05:14:15 +08:00
|
|
|
"""Fixtures parametrized with empty ids cause an internal error (#1849)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-24 05:14:15 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", ids=[], params=[])
|
|
|
|
def temp(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_temp(temp):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 skipped *"])
|
2016-08-24 05:14:15 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrized_empty_ids(self, pytester: Pytester) -> None:
|
2016-08-24 05:14:15 +08:00
|
|
|
"""Tests parametrized with empty ids cause an internal error (#1849)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-24 05:14:15 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('temp', [], ids=list())
|
|
|
|
def test_temp(temp):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 skipped *"])
|
2016-08-24 05:14:15 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrized_ids_invalid_type(self, pytester: Pytester) -> None:
|
2019-11-12 21:52:19 +08:00
|
|
|
"""Test error with non-strings/non-ints, without generator (#1857)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-24 08:07:41 +08:00
|
|
|
import pytest
|
|
|
|
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
@pytest.mark.parametrize("x, expected", [(1, 2), (3, 4), (5, 6)], ids=(None, 2, OSError()))
|
2016-08-24 08:07:41 +08:00
|
|
|
def test_ids_numbers(x,expected):
|
|
|
|
assert x * 2 == expected
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-10-04 07:07:59 +08:00
|
|
|
[
|
python: unify code to generate ID from value
In the following
@pytest.mark.parametrize(..., ids=[val])
the ID values are only allowed to be `str`, `float`, `int` or `bool`.
In the following
@pytest.mark.parametrize(..., [val])
@pytest.mark.parametrize(..., [pytest.param(..., id=val])
a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.
In the interest of consistency, use the latter code path for all cases.
2022-02-05 18:25:48 +08:00
|
|
|
"In test_ids_numbers: ids contains unsupported value OSError() (type: <class 'OSError'>) at index 2. "
|
|
|
|
"Supported types are: str, bytes, int, float, complex, bool, enum, regex or anything with a __name__."
|
2018-10-04 07:07:59 +08:00
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-08-24 08:07:41 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_with_identical_ids_get_unique_names(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-03-20 03:42:47 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2016-03-20 03:42:47 +08:00
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
["*test_function*a0*PASSED*", "*test_function*a1*FAILED*"]
|
|
|
|
)
|
2016-03-20 03:42:47 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(("scope", "length"), [("module", 2), ("function", 4)])
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_scope_overrides(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester, scope: str, length: int
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2019-06-25 11:06:02 +08:00
|
|
|
if "arg" in metafunc.fixturenames:
|
2012-11-02 23:04:57 +08:00
|
|
|
metafunc.parametrize("arg", [1,2], indirect=True,
|
|
|
|
scope=%r)
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(request.param)
|
2012-11-02 23:04:57 +08:00
|
|
|
return request.param
|
|
|
|
def test_hello(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
def test_world(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
def test_checklength():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == %d
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
% (scope, length)
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_issue323(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-06-28 18:57:10 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("--collect-only")
|
2013-06-28 18:57:10 +08:00
|
|
|
assert not reprec.getcalls("pytest_internalerror")
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_usefixtures_seen_in_generate_tests(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert "abc" in metafunc.fixturenames
|
|
|
|
metafunc.parametrize("abc", [1])
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("abc")
|
|
|
|
def test_function():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
reprec.assert_outcomes(passed=1)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_generate_tests_only_done_in_subdir(self, pytester: Pytester) -> None:
|
|
|
|
sub1 = pytester.mkpydir("sub1")
|
|
|
|
sub2 = pytester.mkpydir("sub2")
|
|
|
|
sub1.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert metafunc.function.__name__ == "test_1"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
sub2.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert metafunc.function.__name__ == "test_2"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
|
|
|
sub1.joinpath("test_in_sub1.py").write_text(
|
|
|
|
"def test_1(): pass", encoding="utf-8"
|
|
|
|
)
|
|
|
|
sub2.joinpath("test_in_sub2.py").write_text(
|
|
|
|
"def test_2(): pass", encoding="utf-8"
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_generate_same_function_names_issue403(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
reprec.assert_outcomes(passed=4)
|
2013-12-16 14:47:59 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_misspelling(self, pytester: Pytester) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#463"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-03-22 06:06:25 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-11-19 22:58:46 +08:00
|
|
|
@pytest.mark.parametrise("x", range(2))
|
2015-03-22 06:06:25 +08:00
|
|
|
def test_foo(x):
|
|
|
|
pass
|
2019-11-19 22:58:46 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2023-10-11 05:16:24 +08:00
|
|
|
result = pytester.runpytest("--collect-only")
|
2018-10-04 07:07:59 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2019-11-19 22:58:46 +08:00
|
|
|
"collected 0 items / 1 error",
|
|
|
|
"",
|
|
|
|
"*= ERRORS =*",
|
|
|
|
"*_ ERROR collecting test_parametrize_misspelling.py _*",
|
|
|
|
"test_parametrize_misspelling.py:3: in <module>",
|
|
|
|
' @pytest.mark.parametrise("x", range(2))',
|
|
|
|
"E Failed: Unknown 'parametrise' mark, did you mean 'parametrize'?",
|
|
|
|
"*! Interrupted: 1 error during collection !*",
|
2020-11-08 22:45:10 +08:00
|
|
|
"*= no tests collected, 1 error in *",
|
2018-10-04 07:07:59 +08:00
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-03-22 06:06:25 +08:00
|
|
|
|
2023-07-28 18:54:11 +08:00
|
|
|
@pytest.mark.parametrize("scope", ["class", "package"])
|
|
|
|
def test_parametrize_missing_scope_doesnt_crash(
|
|
|
|
self, pytester: Pytester, scope: str
|
|
|
|
) -> None:
|
|
|
|
"""Doesn't crash when parametrize(scope=<scope>) is used without a
|
|
|
|
corresponding <scope> node."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
f"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", [0], scope="{scope}")
|
|
|
|
def test_it(x): pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2023-08-10 01:13:45 +08:00
|
|
|
def test_parametrize_module_level_test_with_class_scope(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Test that a class-scoped parametrization without a corresponding `Class`
|
|
|
|
gets module scope, i.e. we only create a single FixtureDef for it per module.
|
|
|
|
"""
|
|
|
|
module = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", [0, 1], scope="class")
|
|
|
|
def test_1(x):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", [1, 2], scope="module")
|
|
|
|
def test_2(x):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
test_1_0, _, test_2_0, _ = pytester.genitems((pytester.getmodulecol(module),))
|
|
|
|
|
|
|
|
assert isinstance(test_1_0, Function)
|
|
|
|
assert test_1_0.name == "test_1[0]"
|
|
|
|
test_1_fixture_x = test_1_0._fixtureinfo.name2fixturedefs["x"][-1]
|
|
|
|
|
|
|
|
assert isinstance(test_2_0, Function)
|
|
|
|
assert test_2_0.name == "test_2[1]"
|
|
|
|
test_2_fixture_x = test_2_0._fixtureinfo.name2fixturedefs["x"][-1]
|
|
|
|
|
|
|
|
assert test_1_fixture_x is test_2_fixture_x
|
|
|
|
|
2023-08-06 21:59:54 +08:00
|
|
|
def test_reordering_with_scopeless_and_just_indirect_parametrization(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def fixture1():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixture0():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixture1(fixture0):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("fixture1", [0], indirect=True)
|
|
|
|
def test_0(fixture1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixture():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("fixture", [0], indirect=True)
|
|
|
|
def test_1(fixture):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_2():
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def fixture(self, fixture):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("fixture", [0], indirect=True)
|
|
|
|
def test_3(self, fixture):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest("-v")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_0*",
|
|
|
|
"*test_1*",
|
|
|
|
"*test_2*",
|
|
|
|
"*test_3*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMetafuncFunctionalAuto:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Tests related to automatically find out the correct scope for
|
|
|
|
parametrized tests (#1832)."""
|
2016-08-23 07:21:31 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_auto_scope(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-23 07:21:31 +08:00
|
|
|
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'
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 3 passed *"])
|
2016-08-23 07:21:31 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_auto_scope_indirect(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-23 07:21:31 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 3 passed *"])
|
2016-08-23 07:21:31 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_auto_scope_override_fixture(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-23 07:21:31 +08:00
|
|
|
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')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed *"])
|
2016-08-23 07:21:31 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_all_indirects(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-23 07:21:31 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 3 passed *"])
|
2016-08-23 07:21:31 +08:00
|
|
|
|
2020-02-14 16:50:45 +08:00
|
|
|
def test_parametrize_some_arguments_auto_scope(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester, monkeypatch
|
2020-02-14 16:50:45 +08:00
|
|
|
) -> None:
|
2018-09-15 08:13:58 +08:00
|
|
|
"""Integration test for (#3941)"""
|
2020-10-06 09:13:05 +08:00
|
|
|
class_fix_setup: List[object] = []
|
2018-09-15 08:13:58 +08:00
|
|
|
monkeypatch.setattr(sys, "class_fix_setup", class_fix_setup, raising=False)
|
2020-10-06 09:13:05 +08:00
|
|
|
func_fix_setup: List[object] = []
|
2018-09-15 08:13:58 +08:00
|
|
|
monkeypatch.setattr(sys, "func_fix_setup", func_fix_setup, raising=False)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-09-15 08:13:58 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class', autouse=True)
|
|
|
|
def class_fix(request):
|
|
|
|
sys.class_fix_setup.append(request.param)
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def func_fix():
|
|
|
|
sys.func_fix_setup.append(True)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('class_fix', [10, 20], indirect=True)
|
|
|
|
class Test:
|
|
|
|
def test_foo(self):
|
|
|
|
pass
|
|
|
|
def test_bar(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest_inprocess()
|
2018-09-15 08:13:58 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 4 passed in *"])
|
|
|
|
assert func_fix_setup == [True] * 4
|
|
|
|
assert class_fix_setup == [10, 20]
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_issue634(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-23 07:21:31 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2016-08-23 07:21:31 +08:00
|
|
|
output = result.stdout.str()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert output.count("preparing foo-2") == 1
|
|
|
|
assert output.count("preparing foo-3") == 1
|
2016-08-23 07:21:31 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMarkersWithParametrization:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#308"""
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_simple_mark(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(1, 3, marks=pytest.mark.bar),
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
items = pytester.getitems(s)
|
2013-05-17 16:46:36 +08:00
|
|
|
assert len(items) == 3
|
|
|
|
for item in items:
|
2018-05-23 22:48:46 +08:00
|
|
|
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-17 16:46:36 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_select_based_on_mark(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(2, 3, marks=pytest.mark.foo),
|
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
|
2013-05-17 16:46:36 +08:00
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
rec = pytester.inline_run("-m", "foo")
|
2013-05-17 16:46:36 +08:00
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
assert len(passed) == 1
|
|
|
|
assert len(skipped) == 0
|
|
|
|
assert len(fail) == 0
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_simple_xfail(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(1, 3, marks=pytest.mark.xfail),
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-20 10:52:20 +08:00
|
|
|
# xfail is skip??
|
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
2013-05-17 16:46:36 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_simple_xfail_single_argname(self, pytester: Pytester) -> None:
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize("n", [
|
|
|
|
2,
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(3, marks=pytest.mark.xfail),
|
2013-05-20 10:52:20 +08:00
|
|
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-17 16:46:36 +08:00
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_xfail_with_arg(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(1, 3, marks=pytest.mark.xfail("True")),
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-17 16:46:36 +08:00
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_xfail_with_kwarg(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-17 16:46:36 +08:00
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_xfail_with_arg_and_kwarg(self, pytester: Pytester) -> None:
|
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),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(1, 3, marks=pytest.mark.xfail("True", reason="some bug")),
|
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
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-17 16:46:36 +08:00
|
|
|
reprec.assertoutcome(passed=2, skipped=1)
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("strict", [True, False])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_xfail_passing_is_xpass(self, pytester: Pytester, strict: bool) -> None:
|
2013-05-17 16:46:36 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
m = pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict})
|
|
|
|
|
2013-05-20 10:52:20 +08:00
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
2013-05-17 16:46:36 +08:00
|
|
|
(1, 2),
|
2018-12-21 02:13:43 +08:00
|
|
|
pytest.param(2, 3, marks=m),
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
strict=strict
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.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
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_called_in_generate_tests(self, pytester: Pytester) -> None:
|
2013-05-20 10:52:20 +08:00
|
|
|
s = """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
passingTestData = [(1, 2),
|
|
|
|
(2, 3)]
|
|
|
|
failingTestData = [(1, 3),
|
|
|
|
(2, 2)]
|
|
|
|
|
2018-12-21 02:13:43 +08:00
|
|
|
testData = passingTestData + [pytest.param(*d, marks=pytest.mark.xfail)
|
2013-05-20 10:52:20 +08:00
|
|
|
for d in failingTestData]
|
|
|
|
metafunc.parametrize(("n", "expected"), testData)
|
|
|
|
|
|
|
|
|
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2013-05-20 10:52:20 +08:00
|
|
|
reprec.assertoutcome(passed=2, skipped=2)
|
2013-08-16 17:41:31 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_ID_generation_string_int_works(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#290"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-08-16 17:41:31 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def myfixture():
|
|
|
|
return 'example'
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'limit', (0, '0'))
|
|
|
|
def test_limit(limit, myfixture):
|
|
|
|
return
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-08-16 17:41:31 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2016-04-25 22:48:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("strict", [True, False])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_marked_value(self, pytester: Pytester, strict: bool) -> None:
|
2016-09-07 17:00:27 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
strict=strict
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(s)
|
|
|
|
reprec = pytester.inline_run()
|
2016-09-07 17:00:27 +08:00
|
|
|
passed, failed = (0, 2) if strict else (2, 0)
|
|
|
|
reprec.assertoutcome(passed=passed, failed=failed)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pytest_make_parametrize_id(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-04-25 22:48:28 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("x", range(2))
|
|
|
|
def test_func(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_func*0*PASS*", "*test_func*2*PASS*"])
|
2017-01-16 11:09:46 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pytest_make_parametrize_id_with_argname(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-01-16 11:09:46 +08:00
|
|
|
def pytest_make_parametrize_id(config, val, argname):
|
|
|
|
return str(val * 2 if argname == 'x' else val * 10)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-01-16 11:09:46 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_func_a*0*PASS*", "*test_func_a*2*PASS*", "*test_func_b*10*PASS*"]
|
|
|
|
)
|
2019-06-25 07:07:40 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_positional_args(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-25 07:07:40 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("a", [1], False)
|
|
|
|
def test_foo(a):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-25 07:07:40 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2019-11-12 21:52:19 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_iterator(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-11-12 21:52:19 +08:00
|
|
|
"""
|
|
|
|
import itertools
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
id_parametrize = pytest.mark.parametrize(
|
|
|
|
ids=("param%d" % i for i in itertools.count())
|
|
|
|
)
|
|
|
|
|
|
|
|
@id_parametrize('y', ['a', 'b'])
|
|
|
|
def test1(y):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@id_parametrize('y', ['a', 'b'])
|
|
|
|
def test2(y):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("a, b", [(1, 2), (3, 4)], ids=itertools.count())
|
|
|
|
def test_converted_to_str(a, b):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-vv", "-s")
|
2019-11-12 21:52:19 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_parametrize_iterator.py::test1[param0] PASSED",
|
|
|
|
"test_parametrize_iterator.py::test1[param1] PASSED",
|
|
|
|
"test_parametrize_iterator.py::test2[param0] PASSED",
|
|
|
|
"test_parametrize_iterator.py::test2[param1] PASSED",
|
|
|
|
"test_parametrize_iterator.py::test_converted_to_str[0] PASSED",
|
|
|
|
"test_parametrize_iterator.py::test_converted_to_str[1] PASSED",
|
|
|
|
"*= 6 passed in *",
|
|
|
|
]
|
|
|
|
)
|