2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2020-12-10 13:47:58 +08:00
|
|
|
import os
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2018-09-15 02:25:45 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
from _pytest.compat import getfuncargnames
|
2020-04-15 17:17:13 +08:00
|
|
|
from _pytest.config import ExitCode
|
2023-09-05 04:44:49 +08:00
|
|
|
from _pytest.fixtures import deduplicate_names
|
2023-07-16 05:37:33 +08:00
|
|
|
from _pytest.fixtures import TopRequest
|
2020-12-10 13:47:58 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2020-12-11 19:40:37 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-12-10 13:47:58 +08:00
|
|
|
from _pytest.python import Function
|
2015-11-27 22:43:01 +08:00
|
|
|
import pytest
|
2010-09-26 00:23:26 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-08-06 22:02:46 +08:00
|
|
|
def test_getfuncargnames_functions():
|
|
|
|
"""Test getfuncargnames for normal functions"""
|
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def f():
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert not getfuncargnames(f)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def g(arg):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert getfuncargnames(g) == ("arg",)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def h(arg1, arg2="hello"):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert getfuncargnames(h) == ("arg1",)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-07 00:13:02 +08:00
|
|
|
def j(arg1, arg2, arg3="hello"):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert getfuncargnames(j) == ("arg1", "arg2")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-08-06 22:02:46 +08:00
|
|
|
|
|
|
|
def test_getfuncargnames_methods():
|
|
|
|
"""Test getfuncargnames for normal methods"""
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class A:
|
2009-05-12 01:23:57 +08:00
|
|
|
def f(self, arg1, arg2="hello"):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert getfuncargnames(A().f) == ("arg1",)
|
2019-08-06 22:02:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_getfuncargnames_staticmethod():
|
|
|
|
"""Test getfuncargnames for staticmethods"""
|
|
|
|
|
|
|
|
class A:
|
2017-08-18 07:37:51 +08:00
|
|
|
@staticmethod
|
2019-08-06 22:02:46 +08:00
|
|
|
def static(arg1, arg2, x=1):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2017-08-18 07:37:51 +08:00
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
assert getfuncargnames(A.static, cls=A) == ("arg1", "arg2")
|
2017-08-18 07:37:51 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-31 11:00:37 +08:00
|
|
|
def test_getfuncargnames_staticmethod_inherited() -> None:
|
|
|
|
"""Test getfuncargnames for inherited staticmethods (#8061)"""
|
|
|
|
|
|
|
|
class A:
|
|
|
|
@staticmethod
|
|
|
|
def static(arg1, arg2, x=1):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
class B(A):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert getfuncargnames(B.static, cls=B) == ("arg1", "arg2")
|
|
|
|
|
|
|
|
|
2019-08-06 22:02:46 +08:00
|
|
|
def test_getfuncargnames_partial():
|
|
|
|
"""Check getfuncargnames for methods defined with functools.partial (#5701)"""
|
|
|
|
import functools
|
|
|
|
|
|
|
|
def check(arg1, arg2, i):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2019-08-06 22:02:46 +08:00
|
|
|
|
|
|
|
class T:
|
|
|
|
test_ok = functools.partial(check, i=2)
|
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
values = getfuncargnames(T().test_ok, name="test_ok")
|
2019-08-06 22:02:46 +08:00
|
|
|
assert values == ("arg1", "arg2")
|
|
|
|
|
|
|
|
|
|
|
|
def test_getfuncargnames_staticmethod_partial():
|
|
|
|
"""Check getfuncargnames for staticmethods defined with functools.partial (#5701)"""
|
|
|
|
import functools
|
|
|
|
|
|
|
|
def check(arg1, arg2, i):
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2019-08-06 22:02:46 +08:00
|
|
|
|
|
|
|
class T:
|
|
|
|
test_ok = staticmethod(functools.partial(check, i=2))
|
|
|
|
|
2020-07-31 14:46:56 +08:00
|
|
|
values = getfuncargnames(T().test_ok, name="test_ok")
|
2019-08-06 22:02:46 +08:00
|
|
|
assert values == ("arg1", "arg2")
|
|
|
|
|
|
|
|
|
2018-06-29 16:58:33 +08:00
|
|
|
@pytest.mark.pytester_example_path("fixtures/fill_fixtures")
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFillFixtures:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_lookupfails(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example()
|
|
|
|
result = pytester.runpytest() # "--collect-only")
|
2012-07-19 15:20:14 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines(
|
2018-06-29 16:58:33 +08:00
|
|
|
"""
|
|
|
|
*def test_func(some)*
|
|
|
|
*fixture*some*not found*
|
|
|
|
*xyzsomething*
|
|
|
|
"""
|
2012-07-19 15:20:14 +08:00
|
|
|
)
|
2009-04-14 08:23:42 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_detect_recursive_dependency_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example()
|
|
|
|
result = pytester.runpytest()
|
2018-10-20 20:44:42 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*recursive dependency involving fixture 'fix1' detected*"]
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_basic(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example()
|
|
|
|
item = pytester.getitem(Path("test_funcarg_basic.py"))
|
|
|
|
assert isinstance(item, Function)
|
2021-01-01 22:05:54 +08:00
|
|
|
# Execute's item's setup, which fills fixtures.
|
2021-01-31 18:14:06 +08:00
|
|
|
item.session._setupstate.setup(item)
|
2012-10-16 19:48:00 +08:00
|
|
|
del item.funcargs["request"]
|
2013-08-02 05:48:40 +08:00
|
|
|
assert len(get_public_names(item.funcargs)) == 2
|
2009-04-14 08:23:42 +08:00
|
|
|
assert item.funcargs["some"] == "test_func"
|
|
|
|
assert item.funcargs["other"] == 42
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_lookup_modulelevel(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example()
|
|
|
|
reprec = pytester.inline_run()
|
2012-10-05 16:21:35 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2009-04-14 08:23:42 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_lookup_classlevel(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.copy_example()
|
|
|
|
result = pytester.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2009-05-19 05:26:16 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_conftest_funcargs_only_available_in_subdir(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.copy_example()
|
|
|
|
result = pytester.runpytest("-v")
|
2015-04-28 17:54:46 +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_extend_fixture_module_class(self, pytester: Pytester) -> None:
|
|
|
|
testfile = pytester.copy_example()
|
|
|
|
result = pytester.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_extend_fixture_conftest_module(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.copy_example()
|
|
|
|
result = pytester.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(str(next(Path(str(p)).rglob("test_*.py"))))
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_extend_fixture_conftest_conftest(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.copy_example()
|
|
|
|
result = pytester.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(str(next(Path(str(p)).rglob("test_*.py"))))
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_extend_fixture_conftest_plugin(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-07-06 23:56:54 +08:00
|
|
|
testplugin="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
return 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makeconftest(
|
2013-07-06 23:56:54 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytest_plugins = 'testplugin'
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo + 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-07-06 23:56:54 +08:00
|
|
|
"""
|
|
|
|
def test_foo(foo):
|
|
|
|
assert foo == 14
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-07-06 23:56:54 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_extend_fixture_plugin_plugin(self, pytester: Pytester) -> None:
|
2013-08-02 01:58:28 +08:00
|
|
|
# Two plugins should extend each order in loading order
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-08-02 01:58:28 +08:00
|
|
|
testplugin0="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
return 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-08-02 01:58:28 +08:00
|
|
|
testplugin1="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo + 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(
|
2013-08-02 01:58:28 +08:00
|
|
|
"""
|
|
|
|
pytest_plugins = ['testplugin0', 'testplugin1']
|
|
|
|
|
|
|
|
def test_foo(foo):
|
|
|
|
assert foo == 14
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2013-08-02 01:58:28 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_parametrized_fixture_conftest_module(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2015-03-01 20:54:43 +08:00
|
|
|
"""Test override of the parametrized fixture with non-parametrized one on the test module level."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
testfile = pytester.makepyfile(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == 'spam'
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_parametrized_fixture_conftest_conftest(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2015-03-01 20:54:43 +08:00
|
|
|
"""Test override of the parametrized fixture with non-parametrized one on the conftest level."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
subdir = pytester.mkpydir("subdir")
|
|
|
|
subdir.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2015-03-01 20:54:43 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
"""
|
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
|
|
|
testfile = subdir.joinpath("test_spam.py")
|
|
|
|
testfile.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == "spam"
|
2015-11-27 22:43:01 +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
|
|
|
result = pytester.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_non_parametrized_fixture_conftest_module(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2015-03-01 20:54:43 +08:00
|
|
|
"""Test override of the non-parametrized fixture with parametrized one on the test module level."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
testfile = pytester.makepyfile(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
params = {'spam': 1}
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_non_parametrized_fixture_conftest_conftest(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2015-03-01 20:54:43 +08:00
|
|
|
"""Test override of the non-parametrized fixture with parametrized one on the conftest level."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2015-03-01 20:54:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
subdir = pytester.mkpydir("subdir")
|
|
|
|
subdir.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2015-03-01 20:54:43 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
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
|
|
|
testfile = subdir.joinpath("test_spam.py")
|
|
|
|
testfile.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
params = {'spam': 1}
|
2015-03-01 20:54:43 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
"""
|
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
|
|
|
result = pytester.runpytest()
|
2016-07-23 21:48:41 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2016-07-23 21:48:41 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
|
|
|
def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2016-07-23 21:48:41 +08:00
|
|
|
"""Test override of the autouse fixture with parametrized one on the conftest level.
|
|
|
|
This test covers the issue explained in issue 1601
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2016-07-23 21:48:41 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
subdir = pytester.mkpydir("subdir")
|
|
|
|
subdir.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2015-03-01 20:54:43 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
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
|
|
|
testfile = subdir.joinpath("test_spam.py")
|
|
|
|
testfile.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
params = {'spam': 1}
|
2015-03-01 20:54:43 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
"""
|
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
|
|
|
result = pytester.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_fixture_reusing_super_fixture_parametrization(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-09-10 23:38:43 +08:00
|
|
|
"""Override a fixture at a lower level, reusing the higher-level fixture that
|
|
|
|
is parametrized (#1953).
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo * 2
|
|
|
|
|
|
|
|
def test_spam(foo):
|
|
|
|
assert foo in (2, 4)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-09-10 23:38:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_parametrize_fixture_and_indirect(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-09-10 23:38:43 +08:00
|
|
|
"""Override a fixture at a lower level, reusing the higher-level fixture that
|
|
|
|
is parametrized, while also using indirect parametrization.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo * 2
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bar(request):
|
|
|
|
return request.param * 100
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("bar", [42], indirect=True)
|
|
|
|
def test_spam(bar, foo):
|
|
|
|
assert bar == 4200
|
|
|
|
assert foo in (2, 4)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-09-10 23:38:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
|
|
|
|
def test_override_top_level_fixture_reusing_super_fixture_parametrization(
|
2020-12-10 13:47:58 +08:00
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-09-10 23:38:43 +08:00
|
|
|
"""Same as the above test, but with another level of overwriting."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=['unused', 'unused'])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(self, foo):
|
|
|
|
return foo * 2
|
|
|
|
|
|
|
|
def test_spam(self, foo):
|
|
|
|
assert foo in (2, 4)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-09-10 23:38:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_parametrized_fixture_with_new_parametrized_fixture(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2020-09-10 23:38:43 +08:00
|
|
|
"""Overriding a parametrized fixture, while also parametrizing the new fixture and
|
|
|
|
simultaneously requesting the overwritten fixture as parameter, yields the same value
|
|
|
|
as ``request.param``.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=['ignored', 'ignored'])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2020-09-10 23:38:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[10, 20])
|
|
|
|
def foo(foo, request):
|
|
|
|
assert request.param == foo
|
|
|
|
return foo * 2
|
|
|
|
|
|
|
|
def test_spam(foo):
|
|
|
|
assert foo in (20, 40)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-09-10 23:38:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_autouse_fixture_plugin(self, pytester: Pytester) -> None:
|
2013-08-02 01:58:28 +08:00
|
|
|
# A fixture from a plugin has no baseid set, which screwed up
|
|
|
|
# the autouse fixture handling.
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-08-02 01:58:28 +08:00
|
|
|
testplugin="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def foo(request):
|
|
|
|
request.function.foo = 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(
|
2013-08-02 01:58:28 +08:00
|
|
|
"""
|
|
|
|
pytest_plugins = 'testplugin'
|
|
|
|
|
|
|
|
def test_foo(request):
|
|
|
|
assert request.function.foo == 7
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2013-08-02 01:58:28 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_lookup_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2016-08-03 06:43:39 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def a_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def b_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def c_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def d_fixture(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-10-12 21:39:22 +08:00
|
|
|
"""
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_lookup_error(unknown):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2016-08-12 06:57:29 +08:00
|
|
|
"*ERROR at setup of test_lookup_error*",
|
|
|
|
" def test_lookup_error(unknown):*",
|
|
|
|
"E fixture 'unknown' not found",
|
2019-08-29 01:50:13 +08:00
|
|
|
"> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*",
|
|
|
|
# sorted
|
2016-08-12 06:57:29 +08:00
|
|
|
"> use 'py*test --fixtures *' for help on them.",
|
2012-11-02 23:04:57 +08:00
|
|
|
"*1 error*",
|
|
|
|
]
|
|
|
|
)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNAL*")
|
2016-08-17 06:33:07 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_excinfo_leak(self, pytester: Pytester) -> None:
|
2015-01-10 02:55:49 +08:00
|
|
|
# on python2 sys.excinfo would leak into fixture executions
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2015-01-10 02:55:49 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def leak():
|
|
|
|
if sys.exc_info()[0]: # python3 bug :)
|
|
|
|
traceback.print_exc()
|
|
|
|
#fails
|
|
|
|
assert sys.exc_info() == (None, None, None)
|
|
|
|
|
|
|
|
def test_leak(leak):
|
|
|
|
if sys.exc_info()[0]: # python3 bug :)
|
|
|
|
traceback.print_exc()
|
|
|
|
assert sys.exc_info() == (None, None, None)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2015-01-10 02:55:49 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestBasic:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_attributes(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem(
|
2009-04-14 08:59:50 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request): pass
|
2009-04-14 08:59:50 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
assert isinstance(item, Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
req = TopRequest(item, _ispytest=True)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert req.function == item.obj
|
2012-09-17 23:32:23 +08:00
|
|
|
assert req.keywords == item.keywords
|
2009-05-12 01:23:57 +08:00
|
|
|
assert hasattr(req.module, "test_func")
|
|
|
|
assert req.cls is None
|
2010-07-27 03:15:15 +08:00
|
|
|
assert req.function.__name__ == "test_func"
|
|
|
|
assert req.config == item.config
|
2009-04-15 03:36:57 +08:00
|
|
|
assert repr(req).find(req.function.__name__) != -1
|
2009-05-12 01:23:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_attributes_method(self, pytester: Pytester) -> None:
|
|
|
|
(item,) = pytester.getitems(
|
2009-05-12 01:23:57 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestB(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(self, request):
|
2012-07-19 15:20:14 +08:00
|
|
|
return 1
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(self, something):
|
2009-05-12 01:23:57 +08:00
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
assert isinstance(item, Function)
|
2012-07-19 15:20:14 +08:00
|
|
|
req = item._request
|
2009-05-12 01:23:57 +08:00
|
|
|
assert req.cls.__name__ == "TestB"
|
2009-05-19 05:26:16 +08:00
|
|
|
assert req.instance.__class__ == req.cls
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_contains_funcarg_arg2fixturedefs(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol(
|
2009-04-14 08:23:42 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-04-14 08:23:42 +08:00
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-04-14 08:23:42 +08:00
|
|
|
def test_method(self, something):
|
2009-05-21 15:45:43 +08:00
|
|
|
pass
|
2009-04-14 08:23:42 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
(item1,) = pytester.genitems([modcol])
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(item1, Function)
|
2009-04-14 08:23:42 +08:00
|
|
|
assert item1.name == "test_method"
|
2023-07-16 05:37:33 +08:00
|
|
|
arg2fixturedefs = TopRequest(item1, _ispytest=True)._arg2fixturedefs
|
2012-10-16 19:59:12 +08:00
|
|
|
assert len(arg2fixturedefs) == 1
|
2016-07-12 09:03:53 +08:00
|
|
|
assert arg2fixturedefs["something"][0].argname == "something"
|
2009-04-14 08:59:50 +08:00
|
|
|
|
2018-09-15 02:25:45 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
hasattr(sys, "pypy_version_info"),
|
|
|
|
reason="this method of test doesn't work on pypy",
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_garbage(self, pytester: Pytester) -> None:
|
2018-10-15 02:17:08 +08:00
|
|
|
try:
|
2024-02-09 17:14:36 +08:00
|
|
|
import xdist # noqa: F401
|
2018-10-15 02:17:08 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
pytest.xfail("this test is flaky when executed with xdist")
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-02-22 13:35:35 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import pytest
|
2018-06-11 06:13:57 +08:00
|
|
|
from _pytest.fixtures import PseudoFixtureDef
|
2018-02-22 13:35:35 +08:00
|
|
|
import gc
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def something(request):
|
2018-09-15 02:25:45 +08:00
|
|
|
original = gc.get_debug()
|
|
|
|
gc.set_debug(gc.DEBUG_SAVEALL)
|
|
|
|
gc.collect()
|
2018-02-22 13:35:35 +08:00
|
|
|
|
2018-09-15 02:25:45 +08:00
|
|
|
yield
|
2018-02-22 13:35:35 +08:00
|
|
|
|
2018-09-15 02:25:45 +08:00
|
|
|
try:
|
2018-02-22 13:35:35 +08:00
|
|
|
gc.collect()
|
2018-11-24 18:29:39 +08:00
|
|
|
leaked = [x for _ in gc.garbage if isinstance(_, PseudoFixtureDef)]
|
|
|
|
assert leaked == []
|
2018-09-15 02:25:45 +08:00
|
|
|
finally:
|
|
|
|
gc.set_debug(original)
|
2018-02-22 13:35:35 +08:00
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
2018-02-22 13:35:35 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_getfixturevalue_recursive(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2009-06-12 01:49:25 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-06-12 01:49:25 +08:00
|
|
|
return 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-10-12 21:39:22 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
return request.getfixturevalue("something") + 1
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(something):
|
2009-06-12 01:49:25 +08:00
|
|
|
assert something == 2
|
2009-04-14 08:59:50 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:28 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2009-04-14 08:59:50 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_getfixturevalue_teardown(self, pytester: Pytester) -> None:
|
2019-03-01 07:59:37 +08:00
|
|
|
"""
|
|
|
|
Issue #1895
|
|
|
|
|
2019-03-03 22:20:00 +08:00
|
|
|
`test_inner` requests `inner` fixture, which in turn requests `resource`
|
|
|
|
using `getfixturevalue`. `test_func` then requests `resource`.
|
2019-03-01 07:59:37 +08:00
|
|
|
|
|
|
|
`resource` is teardown before `inner` because the fixture mechanism won't consider
|
2019-03-03 22:20:00 +08:00
|
|
|
`inner` dependent on `resource` when it is used via `getfixturevalue`: `test_func`
|
2019-03-01 07:59:37 +08:00
|
|
|
will then cause the `resource`'s finalizer to be called first because of this.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-03-01 07:59:37 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def resource():
|
|
|
|
r = ['value']
|
|
|
|
yield r
|
|
|
|
r.pop()
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def inner(request):
|
|
|
|
resource = request.getfixturevalue('resource')
|
|
|
|
assert resource == ['value']
|
|
|
|
yield
|
|
|
|
assert resource == ['value']
|
|
|
|
|
|
|
|
def test_inner(inner):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func(resource):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed in *"])
|
2019-03-01 07:59:37 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_getfixturevalue(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem(
|
2009-05-21 15:45:43 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2021-01-01 22:05:54 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2021-01-01 22:05:54 +08:00
|
|
|
def something(request):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
values = [2]
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def other(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
return values.pop()
|
2021-01-01 22:05:54 +08:00
|
|
|
|
2009-05-21 15:45:43 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
assert isinstance(item, Function)
|
2012-07-19 15:20:14 +08:00
|
|
|
req = item._request
|
2019-06-30 22:02:46 +08:00
|
|
|
|
2021-01-01 22:05:54 +08:00
|
|
|
# Execute item's setup.
|
2021-01-31 18:14:06 +08:00
|
|
|
item.session._setupstate.setup(item)
|
2021-01-01 22:05:54 +08:00
|
|
|
|
2019-11-28 00:10:18 +08:00
|
|
|
with pytest.raises(pytest.FixtureLookupError):
|
2019-06-30 22:02:46 +08:00
|
|
|
req.getfixturevalue("notexists")
|
|
|
|
val = req.getfixturevalue("something")
|
|
|
|
assert val == 1
|
|
|
|
val = req.getfixturevalue("something")
|
|
|
|
assert val == 1
|
|
|
|
val2 = req.getfixturevalue("other")
|
|
|
|
assert val2 == 2
|
|
|
|
val2 = req.getfixturevalue("other") # see about caching
|
|
|
|
assert val2 == 2
|
|
|
|
assert item.funcargs["something"] == 1
|
|
|
|
assert len(get_public_names(item.funcargs)) == 2
|
|
|
|
assert "request" in item.funcargs
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_addfinalizer(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem(
|
2009-04-14 08:59:50 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2009-05-21 20:37:30 +08:00
|
|
|
teardownlist = []
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-06-23 23:10:52 +08:00
|
|
|
request.addfinalizer(lambda: teardownlist.append(1))
|
2009-04-14 08:59:50 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
assert isinstance(item, Function)
|
2021-01-31 18:14:06 +08:00
|
|
|
item.session._setupstate.setup(item)
|
2020-04-17 21:05:43 +08:00
|
|
|
item._request._fillfixtures()
|
2010-07-27 03:15:15 +08:00
|
|
|
# successively check finalization calls
|
2020-12-10 13:47:58 +08:00
|
|
|
parent = item.getparent(pytest.Module)
|
|
|
|
assert parent is not None
|
|
|
|
teardownlist = parent.obj.teardownlist
|
2011-05-27 06:08:56 +08:00
|
|
|
ss = item.session._setupstate
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not teardownlist
|
2021-01-01 18:08:12 +08:00
|
|
|
ss.teardown_exact(None)
|
2009-08-30 02:04:48 +08:00
|
|
|
print(ss.stack)
|
2009-06-23 23:10:52 +08:00
|
|
|
assert teardownlist == [1]
|
2009-05-19 01:06:16 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_addfinalizer_failing_setup(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-07-17 16:29:11 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1]
|
2013-07-17 16:29:11 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def myfix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(values.pop)
|
2013-07-17 16:29:11 +08:00
|
|
|
assert 0
|
|
|
|
def test_fix(myfix):
|
|
|
|
pass
|
|
|
|
def test_finalizer_ran():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2013-07-17 16:29:11 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2013-07-17 16:29:11 +08:00
|
|
|
reprec.assertoutcome(failed=1, passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_addfinalizer_failing_setup_module(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-07-17 16:29:11 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1, 2]
|
2013-07-17 16:29:11 +08:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def myfix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(values.pop)
|
|
|
|
request.addfinalizer(values.pop)
|
2013-07-17 16:29:11 +08:00
|
|
|
assert 0
|
|
|
|
def test_fix(myfix):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2013-07-17 16:29:11 +08:00
|
|
|
mod = reprec.getcalls("pytest_runtest_setup")[0].item.module
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not mod.values
|
2013-07-17 16:29:11 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_addfinalizer_partial_setup_failure(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2009-07-05 20:22:01 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append(None))
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(something, missingarg):
|
2009-07-05 20:22:01 +08:00
|
|
|
pass
|
|
|
|
def test_second():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2009-07-05 20:22:01 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2012-07-19 15:20:14 +08:00
|
|
|
["*1 error*"] # XXX the whole module collection fails
|
2017-07-17 07:25:07 +08:00
|
|
|
)
|
2009-07-05 20:22:01 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_subrequest_addfinalizer_exceptions(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2017-06-14 06:54:14 +08:00
|
|
|
"""
|
2024-03-03 04:43:56 +08:00
|
|
|
Ensure exceptions raised during teardown by finalizers are suppressed
|
|
|
|
until all finalizers are called, then re-reaised together in an
|
|
|
|
exception group (#2440)
|
2017-06-14 06:54:14 +08:00
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2017-06-13 08:39:42 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2017-06-14 06:54:14 +08:00
|
|
|
def _excepts(where):
|
|
|
|
raise Exception('Error in %s fixture' % where)
|
2017-06-13 08:39:42 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def subrequest(request):
|
|
|
|
return request
|
|
|
|
@pytest.fixture
|
|
|
|
def something(subrequest):
|
2017-11-04 23:17:20 +08:00
|
|
|
subrequest.addfinalizer(lambda: values.append(1))
|
|
|
|
subrequest.addfinalizer(lambda: values.append(2))
|
2017-06-14 06:54:14 +08:00
|
|
|
subrequest.addfinalizer(lambda: _excepts('something'))
|
2017-06-13 08:39:42 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def excepts(subrequest):
|
2017-06-14 06:54:14 +08:00
|
|
|
subrequest.addfinalizer(lambda: _excepts('excepts'))
|
2017-11-04 23:17:20 +08:00
|
|
|
subrequest.addfinalizer(lambda: values.append(3))
|
2017-06-13 08:39:42 +08:00
|
|
|
def test_first(something, excepts):
|
|
|
|
pass
|
|
|
|
def test_second():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [3, 2, 1]
|
2017-06-13 08:39:42 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2024-03-03 04:43:56 +08:00
|
|
|
result.assert_outcomes(passed=2, errors=1)
|
2017-06-14 06:54:14 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2024-03-03 04:43:56 +08:00
|
|
|
[
|
|
|
|
' | *ExceptionGroup: errors while tearing down fixture "subrequest" of <Function test_first> (2 sub-exceptions)', # noqa: E501
|
|
|
|
" +-+---------------- 1 ----------------",
|
|
|
|
" | Exception: Error in something fixture",
|
|
|
|
" +---------------- 2 ----------------",
|
|
|
|
" | Exception: Error in excepts fixture",
|
|
|
|
" +------------------------------------",
|
|
|
|
],
|
2017-06-14 06:54:14 +08:00
|
|
|
)
|
2009-07-05 20:22:01 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_getmodulepath(self, pytester: Pytester) -> None:
|
|
|
|
modcol = pytester.getmodulecol("def test_somefunc(): pass")
|
|
|
|
(item,) = pytester.genitems([modcol])
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(item, Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
req = TopRequest(item, _ispytest=True)
|
2021-01-18 04:20:29 +08:00
|
|
|
assert req.path == modcol.path
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_fixturenames(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2013-08-02 05:48:40 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def arg1():
|
|
|
|
pass
|
|
|
|
@pytest.fixture()
|
|
|
|
def farg(arg1):
|
|
|
|
pass
|
|
|
|
@pytest.fixture(autouse=True)
|
2021-03-14 03:22:54 +08:00
|
|
|
def sarg(tmp_path):
|
2012-11-02 23:04:57 +08:00
|
|
|
pass
|
|
|
|
def test_function(request, farg):
|
2013-08-02 05:48:40 +08:00
|
|
|
assert set(get_public_names(request.fixturenames)) == \
|
2021-03-14 03:22:54 +08:00
|
|
|
set(["sarg", "arg1", "request", "farg",
|
2019-01-27 21:19:23 +08:00
|
|
|
"tmp_path", "tmp_path_factory"])
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_fixturenames_dynamic_fixture(self, pytester: Pytester) -> None:
|
2018-10-04 05:50:14 +08:00
|
|
|
"""Regression test for #3057"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.copy_example("fixtures/test_getfixturevalue_dynamic.py")
|
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2018-10-04 05:50:14 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setupdecorator_and_xunit(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
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
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def setup_module():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("module")
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_function():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("function")
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup_class(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("class")
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_method(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("method")
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
def test_all():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "function", "class",
|
2012-11-02 23:04:57 +08:00
|
|
|
"function", "method", "function"]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixtures_sub_subdir_normalize_sep(self, pytester: Pytester) -> None:
|
2015-03-05 00:00:24 +08:00
|
|
|
# this tests that normalization of nodeids takes place
|
2020-12-10 13:47:58 +08:00
|
|
|
b = pytester.path.joinpath("tests", "unit")
|
|
|
|
b.mkdir(parents=True)
|
|
|
|
b.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
pass
|
2015-11-27 22:43:01 +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
|
|
|
p = b.joinpath("test_module.py")
|
2023-06-20 19:55:39 +08:00
|
|
|
p.write_text("def test_func(arg1): pass", encoding="utf-8")
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest(p, "--fixtures")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*fixtures defined*conftest*
|
|
|
|
*arg1*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_color_yes(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("def test_this(): assert 1")
|
|
|
|
result = pytester.runpytest("--color=yes", "--fixtures")
|
2021-03-14 03:22:54 +08:00
|
|
|
assert "\x1b[32mtmp_path" in result.stdout.str()
|
2015-07-19 03:39:55 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_newstyle_with_request(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture()
|
|
|
|
def arg(request):
|
|
|
|
pass
|
|
|
|
def test_1(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setupcontext_no_param(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=[1,2])
|
|
|
|
def arg(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mysetup(request, arg):
|
|
|
|
assert not hasattr(request, "param")
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2021-09-24 12:15:53 +08:00
|
|
|
class TestRequestSessionScoped:
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def session_request(self, request):
|
|
|
|
return request
|
|
|
|
|
2021-10-16 15:45:05 +08:00
|
|
|
@pytest.mark.parametrize("name", ["path", "module"])
|
2021-09-24 12:15:53 +08:00
|
|
|
def test_session_scoped_unavailable_attributes(self, session_request, name):
|
|
|
|
with pytest.raises(
|
|
|
|
AttributeError,
|
2021-10-16 15:45:05 +08:00
|
|
|
match=f"{name} not available in session-scoped context",
|
2021-09-24 12:15:53 +08:00
|
|
|
):
|
|
|
|
getattr(session_request, name)
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestMarking:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_applymarker(self, pytester: Pytester) -> None:
|
|
|
|
item1, item2 = pytester.getitems(
|
2017-07-17 07:25:08 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2010-06-08 08:34:51 +08:00
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-09-17 23:32:23 +08:00
|
|
|
def test_func1(self, something):
|
|
|
|
pass
|
|
|
|
def test_func2(self, something):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(item1, Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
req1 = TopRequest(item1, _ispytest=True)
|
2012-09-17 23:32:23 +08:00
|
|
|
assert "xfail" not in item1.keywords
|
|
|
|
req1.applymarker(pytest.mark.xfail)
|
|
|
|
assert "xfail" in item1.keywords
|
|
|
|
assert "skipif" not in item1.keywords
|
|
|
|
req1.applymarker(pytest.mark.skipif)
|
|
|
|
assert "skipif" in item1.keywords
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(ValueError):
|
2020-10-03 19:21:41 +08:00
|
|
|
req1.applymarker(42) # type: ignore[arg-type]
|
2012-09-17 23:32:23 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_accesskeywords(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-17 23:32:23 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-10-18 19:52:32 +08:00
|
|
|
def keywords(request):
|
|
|
|
return request.keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
@pytest.mark.XYZ
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_function(keywords):
|
|
|
|
assert keywords["XYZ"]
|
|
|
|
assert "abc" not in keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-09-17 23:32:23 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_accessmarker_dynamic(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-09-17 23:32:23 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-10-18 19:52:32 +08:00
|
|
|
def keywords(request):
|
|
|
|
return request.keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-09-17 23:32:23 +08:00
|
|
|
def marking(request):
|
|
|
|
request.applymarker(pytest.mark.XYZ("hello"))
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-09-17 23:32:23 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_fun1(keywords):
|
|
|
|
assert keywords["XYZ"] is not None
|
|
|
|
assert "abc" not in keywords
|
|
|
|
def test_fun2(keywords):
|
|
|
|
assert keywords["XYZ"] is not None
|
|
|
|
assert "abc" not in keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-09-17 23:32:23 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureUsages:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_noargfixturedec(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-07 03:03:55 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_func(arg1):
|
|
|
|
assert arg1 == 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-07 03:03:55 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_receives_funcargs(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-23 16:55:09 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg1():
|
2012-07-23 16:55:09 +08:00
|
|
|
return 1
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
def test_all(arg1, arg2):
|
|
|
|
assert arg1 == 1
|
|
|
|
assert arg2 == 2
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-23 16:55:09 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_receives_funcargs_scope_mismatch(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-23 16:55:09 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg1():
|
2012-07-23 16:55:09 +08:00
|
|
|
return 1
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-07-23 16:55:09 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2024-03-09 05:08:28 +08:00
|
|
|
"*ScopeMismatch*Requesting fixture stack*",
|
2019-03-15 09:47:06 +08:00
|
|
|
"test_receives_funcargs_scope_mismatch.py:6: def arg2(arg1)",
|
2024-03-09 05:08:28 +08:00
|
|
|
"Requested fixture:",
|
2019-03-15 09:47:06 +08:00
|
|
|
"test_receives_funcargs_scope_mismatch.py:2: def arg1()",
|
2012-07-23 16:55:09 +08:00
|
|
|
"*1 error*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_receives_funcargs_scope_mismatch_issue660(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-04-02 00:42:48 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def arg1():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def arg2(arg1):
|
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg1, arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2015-04-02 00:42:48 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2024-03-09 05:08:28 +08:00
|
|
|
[
|
|
|
|
"*ScopeMismatch*Requesting fixture stack*",
|
|
|
|
"* def arg2(arg1)",
|
|
|
|
"Requested fixture:",
|
|
|
|
"* def arg1()",
|
|
|
|
"*1 error*",
|
|
|
|
],
|
2015-04-02 00:42:48 +08:00
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_invalid_scope(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-09-07 02:16:25 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="functions")
|
|
|
|
def badscope():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_nothing(badscope):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest_inprocess()
|
2016-09-07 02:16:25 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2019-04-12 19:52:47 +08:00
|
|
|
"*Fixture 'badscope' from test_invalid_scope.py got an unexpected scope value 'functions'"
|
2016-09-07 02:16:25 +08:00
|
|
|
)
|
|
|
|
|
2020-01-26 03:28:00 +08:00
|
|
|
@pytest.mark.parametrize("scope", ["function", "session"])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parameters_without_eq_semantics(self, scope, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-01-26 03:28:00 +08:00
|
|
|
f"""
|
|
|
|
class NoEq1: # fails on `a == b` statement
|
|
|
|
def __eq__(self, _):
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
class NoEq2: # fails on `if a == b:` statement
|
|
|
|
def __eq__(self, _):
|
|
|
|
class NoBool:
|
|
|
|
def __bool__(self):
|
|
|
|
raise RuntimeError
|
|
|
|
return NoBool()
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=[NoEq1(), NoEq2()], scope={scope!r})
|
|
|
|
def no_eq(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test1(no_eq):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test2(no_eq):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-01-26 03:28:00 +08:00
|
|
|
result.stdout.fnmatch_lines(["*4 passed*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_parametrized_and_used_twice(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-23 16:55:09 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg1(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-23 16:55:09 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg1, arg2):
|
|
|
|
assert arg2 == arg1 + 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == arg1
|
2012-07-23 16:55:09 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-07-23 16:55:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2012-06-25 23:35:33 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_factory_uses_unknown_funcarg_as_dependency_error(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-23 01:49:50 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-08-23 03:20:18 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-23 03:20:18 +08:00
|
|
|
def fail(missing):
|
|
|
|
return
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-23 03:20:18 +08:00
|
|
|
def call_fail(fail):
|
2012-08-23 01:49:50 +08:00
|
|
|
return
|
|
|
|
|
2012-08-23 03:20:18 +08:00
|
|
|
def test_missing(call_fail):
|
2012-08-23 01:49:50 +08:00
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-10-05 20:24:45 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*pytest.fixture()*
|
|
|
|
*def call_fail(fail)*
|
|
|
|
*pytest.fixture()*
|
|
|
|
*def fail*
|
|
|
|
*fixture*'missing'*not found*
|
|
|
|
"""
|
|
|
|
)
|
2012-08-23 01:49:50 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_factory_setup_as_classes_fails(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-18 20:00:47 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class arg1(object):
|
2012-09-18 20:00:47 +08:00
|
|
|
def __init__(self, request):
|
|
|
|
self.x = 1
|
2012-10-05 16:21:35 +08:00
|
|
|
arg1 = pytest.fixture()(arg1)
|
2012-09-18 20:00:47 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getfailedcollections()
|
|
|
|
assert len(values) == 1
|
2012-09-18 20:00:47 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_usefixtures_marker(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-06 01:20:40 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-06 01:20:40 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def myfix(request):
|
|
|
|
request.cls.hello = "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-10-06 01:20:40 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_one(self):
|
|
|
|
assert self.hello == "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_two(self):
|
|
|
|
assert self.hello == "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-10-06 01:20:40 +08:00
|
|
|
pytest.mark.usefixtures("myfix")(TestClass)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-06 01:20:40 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_usefixtures_ini(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2012-10-06 01:20:40 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
usefixtures = myfix
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2012-10-06 01:20:40 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def myfix(request):
|
|
|
|
request.cls.hello = "world"
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-10-06 01:20:40 +08:00
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_one(self):
|
|
|
|
assert self.hello == "world"
|
|
|
|
def test_two(self):
|
|
|
|
assert self.hello == "world"
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-06 01:20:40 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2012-07-16 16:46:44 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_usefixtures_seen_in_showmarkers(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--markers")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*usefixtures(fixturename1*mark tests*fixtures*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_instance_issue203(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-20 15:59:20 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-20 15:59:20 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def setup1(self, request):
|
|
|
|
assert self == request.instance
|
|
|
|
self.arg1 = 1
|
|
|
|
def test_hello(self, setup1):
|
|
|
|
assert self.arg1 == 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-20 15:59:20 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_parametrized_with_iterator(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-12-09 17:48:15 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-12-09 17:48:15 +08:00
|
|
|
def f():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
dec = pytest.fixture(scope="module", params=f())
|
|
|
|
|
|
|
|
@dec
|
|
|
|
def arg(request):
|
|
|
|
return request.param
|
|
|
|
@dec
|
|
|
|
def arg2(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2013-12-09 17:48:15 +08:00
|
|
|
def test_2(arg2):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg2*10)
|
2013-12-09 17:48:15 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2013-12-09 17:48:15 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 2, 10, 20]
|
2013-12-09 17:48:15 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_functions_as_fixtures(self, pytester: Pytester) -> None:
|
2018-10-07 07:30:18 +08:00
|
|
|
"""Ensure setup_* methods obey fixture scope rules (#517, #3094)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-10-07 07:30:18 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
DB_INITIALIZED = None
|
|
|
|
|
2020-06-25 19:05:46 +08:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2018-10-07 07:30:18 +08:00
|
|
|
def db():
|
|
|
|
global DB_INITIALIZED
|
|
|
|
DB_INITIALIZED = True
|
|
|
|
yield
|
|
|
|
DB_INITIALIZED = False
|
|
|
|
|
|
|
|
def setup_module():
|
|
|
|
assert DB_INITIALIZED
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
assert DB_INITIALIZED
|
|
|
|
|
|
|
|
class TestClass(object):
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
assert DB_INITIALIZED
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
assert DB_INITIALIZED
|
|
|
|
|
|
|
|
def test_printer_1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_printer_2(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2018-10-07 07:30:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 2 passed in *"])
|
|
|
|
|
2013-12-09 17:48:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureManagerParseFactories:
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2020-12-10 13:47:58 +08:00
|
|
|
def pytester(self, pytester: Pytester) -> Pytester:
|
|
|
|
pytester.makeconftest(
|
2012-07-20 20:16:28 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hello(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "conftest"
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fm(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
return request._fixturemanager
|
2012-07-20 20:16:28 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def item(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return request._pyfuncitem
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
return pytester
|
2012-07-20 20:16:28 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parsefactories_evil_objects_issue214(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-11-01 00:00:55 +08:00
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2012-11-01 00:00:55 +08:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
|
|
|
def __getattr__(self, name):
|
|
|
|
raise RuntimeError()
|
|
|
|
a = A()
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-01 00:00:55 +08:00
|
|
|
reprec.assertoutcome(passed=1, failed=0)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parsefactories_conftest(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:28 +08:00
|
|
|
"""
|
|
|
|
def test_hello(item, fm):
|
|
|
|
for name in ("fm", "hello", "item"):
|
2024-01-06 19:12:02 +08:00
|
|
|
faclist = fm.getfixturedefs(name, item)
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(faclist) == 1
|
|
|
|
fac = faclist[0]
|
2016-07-12 09:03:53 +08:00
|
|
|
assert fac.func.__name__ == name
|
2012-07-20 20:16:28 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-07-20 20:16:28 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parsefactories_conftest_and_module_and_class(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-03 06:53:45 +08:00
|
|
|
"""\
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hello(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "module"
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def hello(self, request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "class"
|
|
|
|
def test_hello(self, item, fm):
|
2024-01-06 19:12:02 +08:00
|
|
|
faclist = fm.getfixturedefs("hello", item)
|
2018-11-22 16:15:14 +08:00
|
|
|
print(faclist)
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(faclist) == 3
|
2018-07-22 22:10:44 +08:00
|
|
|
|
2018-08-01 09:09:19 +08:00
|
|
|
assert faclist[0].func(item._request) == "conftest"
|
|
|
|
assert faclist[1].func(item._request) == "module"
|
|
|
|
assert faclist[2].func(item._request) == "class"
|
2019-06-03 06:53:45 +08:00
|
|
|
"""
|
2012-07-20 20:16:28 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-07-20 20:16:28 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2012-07-20 20:16:46 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parsefactories_relative_node_ids(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2014-09-15 18:44:16 +08:00
|
|
|
# example mostly taken from:
|
|
|
|
# https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html
|
2020-12-10 13:47:58 +08:00
|
|
|
runner = pytester.mkdir("runner")
|
|
|
|
package = pytester.mkdir("package")
|
|
|
|
package.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2014-09-15 18:44:16 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def one():
|
|
|
|
return 1
|
2018-08-24 00:06:17 +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
|
|
|
package.joinpath("test_x.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2014-09-15 18:44:16 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_x(one):
|
|
|
|
assert one == 1
|
|
|
|
"""
|
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
|
|
|
sub = package.joinpath("sub")
|
|
|
|
sub.mkdir()
|
|
|
|
sub.joinpath("__init__.py").touch()
|
|
|
|
sub.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2014-09-15 18:44:16 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def one():
|
|
|
|
return 2
|
|
|
|
"""
|
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
|
|
|
sub.joinpath("test_y.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2014-09-15 18:44:16 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_x(one):
|
|
|
|
assert one == 2
|
|
|
|
"""
|
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
|
|
|
reprec = pytester.inline_run()
|
2014-09-15 18:44:16 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2020-12-10 13:47:58 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.chdir(runner)
|
|
|
|
reprec = pytester.inline_run("..")
|
2014-09-15 18:44:16 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_xunit_fixture(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-07-06 05:15:17 +08:00
|
|
|
__init__="""\
|
2018-04-12 06:39:42 +08:00
|
|
|
values = []
|
2018-07-06 05:15:17 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
package = pytester.mkdir("package")
|
|
|
|
package.joinpath("__init__.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
from .. import values
|
|
|
|
def setup_module():
|
|
|
|
values.append("package")
|
|
|
|
def teardown_module():
|
|
|
|
values[:] = []
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
package.joinpath("test_x.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
from .. import values
|
|
|
|
def test_x():
|
|
|
|
assert values == ["package"]
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
package = pytester.mkdir("package2")
|
|
|
|
package.joinpath("__init__.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
from .. import values
|
|
|
|
def setup_module():
|
|
|
|
values.append("package2")
|
|
|
|
def teardown_module():
|
|
|
|
values[:] = []
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
package.joinpath("test_x.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
from .. import values
|
|
|
|
def test_x():
|
|
|
|
assert values == ["package2"]
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2018-04-12 06:39:42 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_package_fixture_complex(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-07-06 05:15:17 +08:00
|
|
|
__init__="""\
|
2018-04-25 04:32:58 +08:00
|
|
|
values = []
|
2018-07-06 05:15:17 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.syspathinsert(pytester.path.name)
|
|
|
|
package = pytester.mkdir("package")
|
2023-06-20 19:55:39 +08:00
|
|
|
package.joinpath("__init__.py").write_text("", encoding="utf-8")
|
2020-12-10 13:47:58 +08:00
|
|
|
package.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import pytest
|
|
|
|
from .. import values
|
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def one():
|
|
|
|
values.append("package")
|
|
|
|
yield values
|
|
|
|
values.pop()
|
|
|
|
@pytest.fixture(scope="package", autouse=True)
|
|
|
|
def two():
|
|
|
|
values.append("package-auto")
|
|
|
|
yield values
|
|
|
|
values.pop()
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
package.joinpath("test_x.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-07-06 05:15:17 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
from .. import values
|
|
|
|
def test_package_autouse():
|
|
|
|
assert values == ["package-auto"]
|
|
|
|
def test_package(one):
|
|
|
|
assert values == ["package-auto", "package"]
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2018-04-25 04:32:58 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_collect_custom_items(self, pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example("fixtures/custom_item")
|
|
|
|
result = pytester.runpytest("foo")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*passed*"])
|
2018-08-01 04:50:55 +08:00
|
|
|
|
2014-09-15 18:44:16 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAutouseDiscovery:
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2020-12-10 13:47:58 +08:00
|
|
|
def pytester(self, pytester: Pytester) -> Pytester:
|
|
|
|
pytester.makeconftest(
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2021-03-14 03:22:54 +08:00
|
|
|
def perfunction(request, tmp_path):
|
2012-07-30 17:51:50 +08:00
|
|
|
pass
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2021-03-14 03:22:54 +08:00
|
|
|
def arg1(tmp_path):
|
2012-07-24 18:10:04 +08:00
|
|
|
pass
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def perfunction2(arg1):
|
2012-07-24 18:10:04 +08:00
|
|
|
pass
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fm(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
return request._fixturemanager
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def item(request):
|
2012-07-24 18:10:04 +08:00
|
|
|
return request._pyfuncitem
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
return pytester
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parsefactories_conftest(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
2013-08-02 05:48:40 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2012-07-24 18:10:04 +08:00
|
|
|
def test_check_setup(item, fm):
|
2024-01-06 19:12:02 +08:00
|
|
|
autousenames = list(fm._getautousenames(item))
|
2013-08-02 05:48:40 +08:00
|
|
|
assert len(get_public_names(autousenames)) == 2
|
2012-10-16 22:13:12 +08:00
|
|
|
assert "perfunction2" in autousenames
|
|
|
|
assert "perfunction" in autousenames
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-07-24 18:10:04 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_two_classes_separated_autouse(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-16 19:47:59 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA(object):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 19:47:59 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
self.values.append(1)
|
2012-10-16 19:47:59 +08:00
|
|
|
def test_setup1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert self.values == [1]
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestB(object):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 19:47:59 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
self.values.append(1)
|
2012-10-16 19:47:59 +08:00
|
|
|
def test_setup2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert self.values == [1]
|
2012-10-16 19:47:59 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-16 19:47:59 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_at_classlevel(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-18 16:54:12 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-18 16:54:12 +08:00
|
|
|
def permethod(self, request):
|
|
|
|
request.instance.funcname = request.function.__name__
|
|
|
|
def test_method1(self):
|
|
|
|
assert self.funcname == "test_method1"
|
|
|
|
def test_method2(self):
|
|
|
|
assert self.funcname == "test_method2"
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-09-18 16:54:12 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-09-25 21:04:30 +08:00
|
|
|
@pytest.mark.xfail(reason="'enabled' feature not implemented")
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_enabled_functionnode(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-25 21:04:30 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def enabled(parentnode, markers):
|
|
|
|
return "needsdb" in markers
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-25 21:04:30 +08:00
|
|
|
def db(request):
|
|
|
|
return request.param
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(enabled=enabled, autouse=True)
|
2012-09-25 21:04:30 +08:00
|
|
|
def createdb(db):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func1(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
assert "db" not in request.fixturenames
|
2012-09-25 21:04:30 +08:00
|
|
|
|
|
|
|
@pytest.mark.needsdb
|
|
|
|
def test_func2(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
assert "db" in request.fixturenames
|
2012-09-25 21:04:30 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-09-25 21:04:30 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_callables_nocode(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""An imported mock.call would break setup/factory discovery due to
|
|
|
|
it being callable and __code__ not being a code object."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-08-19 18:36:49 +08:00
|
|
|
"""
|
|
|
|
class _call(tuple):
|
|
|
|
def __call__(self, *k, **kw):
|
|
|
|
pass
|
|
|
|
def __getattr__(self, k):
|
|
|
|
return self
|
|
|
|
|
|
|
|
call = _call()
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-08-19 18:36:49 +08:00
|
|
|
reprec.assertoutcome(failed=0, passed=0)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_autouse_in_conftests(self, pytester: Pytester) -> None:
|
|
|
|
a = pytester.mkdir("a")
|
|
|
|
b = pytester.mkdir("a1")
|
|
|
|
conftest = pytester.makeconftest(
|
2012-10-16 22:13:12 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def hello():
|
|
|
|
xxx
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
conftest.rename(a.joinpath(conftest.name))
|
2023-06-20 19:55:39 +08:00
|
|
|
a.joinpath("test_something.py").write_text(
|
|
|
|
"def test_func(): pass", encoding="utf-8"
|
|
|
|
)
|
|
|
|
b.joinpath("test_otherthing.py").write_text(
|
|
|
|
"def test_func(): pass", encoding="utf-8"
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-10-16 22:13:12 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*1 passed*1 error*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_autouse_in_module_and_two_classes(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-16 22:13:12 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 22:13:12 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def append1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("module")
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_x():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module"]
|
2012-10-16 22:13:12 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA(object):
|
2012-10-16 22:13:12 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def append2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("A")
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_hello(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "module", "A"], values
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA2(object):
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_world(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "module", "A", "module"], values
|
2012-10-16 22:13:12 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-10-16 22:13:12 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-11-20 05:17:55 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAutouseManagement:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_autouse_conftest_mid_directory(self, pytester: Pytester) -> None:
|
|
|
|
pkgdir = pytester.mkpydir("xyz123")
|
|
|
|
pkgdir.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def app():
|
|
|
|
import sys
|
|
|
|
sys._myapp = "hello"
|
2015-11-27 22:43:01 +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
|
|
|
sub = pkgdir.joinpath("tests")
|
|
|
|
sub.mkdir()
|
|
|
|
t = sub.joinpath("test_app.py")
|
|
|
|
t.touch()
|
|
|
|
t.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import sys
|
|
|
|
def test_app():
|
|
|
|
assert sys._myapp == "hello"
|
2015-11-27 22:43:01 +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
|
|
|
reprec = pytester.inline_run("-s")
|
2012-11-20 05:17:55 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_and_setup(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-24 18:10:04 +08:00
|
|
|
return 0
|
2013-07-07 03:30:24 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def something(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_hello(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
|
|
|
assert values == [1,2]
|
2012-07-24 18:10:04 +08:00
|
|
|
assert arg == 0
|
|
|
|
|
|
|
|
def test_hello2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
|
|
|
assert values == [1,2]
|
2012-07-24 18:10:04 +08:00
|
|
|
assert arg == 0
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-24 18:10:04 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_uses_parametrized_resource(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def something(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_hello():
|
2017-11-04 23:17:20 +08:00
|
|
|
if len(values) == 1:
|
|
|
|
assert values == [1]
|
|
|
|
elif len(values) == 2:
|
|
|
|
assert values == [1, 2]
|
2012-07-24 18:10:04 +08:00
|
|
|
else:
|
|
|
|
0/0
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-07-24 18:10:04 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_session_parametrized_function(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, arg):
|
|
|
|
if request.function.__name__ == "test_some":
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_some():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_result(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == arg
|
|
|
|
assert values[:arg] == [1,2][:arg]
|
2012-07-24 18:10:04 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v", "-s")
|
2012-07-24 18:10:04 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_class_function_parametrization_finalization(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makeconftest(
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import pprint
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def farg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="class", params=list("ab"))
|
2012-09-17 22:36:10 +08:00
|
|
|
def carg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, farg, carg):
|
2012-08-01 15:07:32 +08:00
|
|
|
def fin():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_%s%s" % (carg, farg))
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(fin)
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-11 19:40:37 +08:00
|
|
|
pytester.makepyfile(
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_1(self):
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass2(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_2(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-11 19:40:37 +08:00
|
|
|
reprec = pytester.inline_run("-v", "-s", "--confcutdir", pytester.path)
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=8)
|
|
|
|
config = reprec.getcalls("pytest_unconfigure")[0].config
|
2022-01-09 19:58:29 +08:00
|
|
|
values = config.pluginmanager._getconftestmodules(p)[0].values
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_ordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-13 19:37:14 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def fappend2():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def classappend3():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(3)
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def mappend():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-08-13 19:37:14 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestHallo(object):
|
2012-08-13 19:37:14 +08:00
|
|
|
def test_method(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,3,2]
|
2012-08-13 19:37:14 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-08-13 19:37:14 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrization_setup_teardown_ordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-26 18:24:04 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-09-26 18:24:04 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-03-18 04:42:43 +08:00
|
|
|
if metafunc.cls is None:
|
|
|
|
assert metafunc.function is test_finish
|
2012-09-26 18:24:04 +08:00
|
|
|
if metafunc.cls is not None:
|
|
|
|
metafunc.parametrize("item", [1,2], scope="class")
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-09-26 18:24:04 +08:00
|
|
|
def addteardown(self, item, request):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("setup-%d" % item)
|
|
|
|
request.addfinalizer(lambda: values.append("teardown-%d" % item))
|
2012-09-26 18:24:04 +08:00
|
|
|
def test_step1(self, item):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("step1-%d" % item)
|
2012-09-26 18:24:04 +08:00
|
|
|
def test_step2(self, item):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("step2-%d" % item)
|
2012-09-26 18:24:04 +08:00
|
|
|
|
|
|
|
def test_finish():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup-1", "step1-1", "step2-1", "teardown-1",
|
2012-09-26 18:24:04 +08:00
|
|
|
"setup-2", "step1-2", "step2-2", "teardown-2",]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-09-26 18:24:04 +08:00
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_ordering_autouse_before_explicit(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
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
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def fix1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def arg1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello(arg1):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2012-09-26 18:24:04 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
@pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"])
|
|
|
|
@pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_ordering_dependencies_torndown_first(
|
|
|
|
self, pytester: Pytester, param1, param2
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#226"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-11-16 17:03:51 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-11-16 17:03:51 +08:00
|
|
|
@pytest.fixture(%(param1)s)
|
|
|
|
def arg1(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin1"))
|
|
|
|
values.append("new1")
|
2012-11-16 17:03:51 +08:00
|
|
|
@pytest.fixture(%(param2)s)
|
|
|
|
def arg2(request, arg1):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin2"))
|
|
|
|
values.append("new2")
|
2012-11-16 17:03:51 +08:00
|
|
|
|
|
|
|
def test_arg(arg2):
|
|
|
|
pass
|
|
|
|
def test_check():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["new1", "new2", "fin2", "fin1"]
|
2012-11-16 17:03:51 +08:00
|
|
|
""" # noqa: UP031 (python syntax issues)
|
|
|
|
% locals()
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2012-11-16 17:03:51 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2014-01-27 19:53:44 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureMarker:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=["a", "b", "c"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_param(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_result():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == list("abc")
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:46 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_multiple_parametrization_issue_736(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-08-08 08:09:08 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1,2,3])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('foobar', [4,5,6])
|
|
|
|
def test_issue(foo, foobar):
|
|
|
|
assert foo in [1,2,3]
|
|
|
|
assert foobar in [4,5,6]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-08-08 08:09:08 +08:00
|
|
|
reprec.assertoutcome(passed=9)
|
|
|
|
|
2015-08-31 20:38:39 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"param_args",
|
|
|
|
["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"],
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_override_parametrized_fixture_issue_979(
|
|
|
|
self, pytester: Pytester, param_args
|
|
|
|
) -> None:
|
2015-08-31 21:11:57 +08:00
|
|
|
"""Make sure a parametrized argument can override a parametrized fixture.
|
|
|
|
|
|
|
|
This was a regression introduced in the fix for #736.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2015-08-31 20:38:39 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def fixt(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(%s, [(3, 'x'), (4, 'x')])
|
|
|
|
def test_foo(fixt, val):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
% param_args
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-08-31 20:38:39 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_session(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-20 20:16:46 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 1
|
|
|
|
def test_2(arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-20 20:16:46 +08:00
|
|
|
def test3(self, arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:46 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_session_exc(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2014-04-16 10:22:41 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-04-16 10:22:41 +08:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def fix():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2014-04-16 10:22:41 +08:00
|
|
|
pytest.skip('skipping')
|
|
|
|
|
|
|
|
def test_1(fix):
|
|
|
|
pass
|
|
|
|
def test_2(fix):
|
|
|
|
pass
|
|
|
|
def test_last():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2014-04-16 10:22:41 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2014-04-16 10:22:41 +08:00
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_session_exc_two_fix(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2014-06-16 02:57:52 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-06-16 02:57:52 +08:00
|
|
|
m = []
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def a():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2014-06-16 02:57:52 +08:00
|
|
|
pytest.skip('skipping')
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def b(a):
|
|
|
|
m.append(1)
|
|
|
|
|
|
|
|
def test_1(b):
|
|
|
|
pass
|
|
|
|
def test_2(b):
|
|
|
|
pass
|
|
|
|
def test_last():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2014-06-16 02:57:52 +08:00
|
|
|
assert m == []
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2014-06-16 02:57:52 +08:00
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_exc(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2014-07-18 08:30:29 +08:00
|
|
|
test_foo="""
|
|
|
|
def test_foo(fix):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
test_bar="""
|
|
|
|
def test_bar(fix):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
conftest="""
|
|
|
|
import pytest
|
|
|
|
reqs = []
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def fix(request):
|
|
|
|
reqs.append(1)
|
|
|
|
pytest.skip()
|
|
|
|
@pytest.fixture
|
|
|
|
def req_list():
|
|
|
|
return reqs
|
|
|
|
""",
|
|
|
|
test_real="""
|
|
|
|
def test_last(req_list):
|
|
|
|
assert req_list == [1]
|
|
|
|
""",
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2014-07-18 08:30:29 +08:00
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_module_uses_session(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-20 20:16:46 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 1
|
|
|
|
def test_2(arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-20 20:16:46 +08:00
|
|
|
def test3(self, arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:46 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_module_and_finalizer(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
finalized_list = []
|
|
|
|
created_list = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2016-07-12 09:03:53 +08:00
|
|
|
created_list.append(1)
|
2012-09-17 22:36:10 +08:00
|
|
|
assert request.scope == "module"
|
2016-07-12 09:03:53 +08:00
|
|
|
request.addfinalizer(lambda: finalized_list.append(1))
|
|
|
|
@pytest.fixture
|
|
|
|
def created(request):
|
|
|
|
return len(created_list)
|
|
|
|
@pytest.fixture
|
|
|
|
def finalized(request):
|
|
|
|
return len(finalized_list)
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:46 +08:00
|
|
|
test_mod1="""
|
|
|
|
def test_1(arg, created, finalized):
|
|
|
|
assert created == 1
|
|
|
|
assert finalized == 0
|
|
|
|
def test_2(arg, created, finalized):
|
|
|
|
assert created == 1
|
|
|
|
assert finalized == 0""",
|
|
|
|
test_mod2="""
|
|
|
|
def test_3(arg, created, finalized):
|
|
|
|
assert created == 2
|
|
|
|
assert finalized == 1""",
|
|
|
|
test_mode3="""
|
|
|
|
def test_4(arg, created, finalized):
|
|
|
|
assert created == 3
|
|
|
|
assert finalized == 2
|
|
|
|
""",
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:46 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_mismatch_various(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-07-20 20:16:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
finalized = []
|
|
|
|
created = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-07-20 20:16:46 +08:00
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:46 +08:00
|
|
|
test_mod1="""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2018-12-02 01:58:55 +08:00
|
|
|
request.getfixturevalue("arg")
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_1(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-07-20 20:16:46 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines(
|
2012-07-23 16:55:09 +08:00
|
|
|
["*ScopeMismatch*You tried*function*session*request*"]
|
2012-07-20 20:16:46 +08:00
|
|
|
)
|
|
|
|
|
2023-07-18 04:58:35 +08:00
|
|
|
def test_scope_mismatch_already_computed_dynamic(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
test_it="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def fixfunc(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixmod(fixfunc): pass
|
|
|
|
|
|
|
|
def test_it(request, fixfunc):
|
|
|
|
request.getfixturevalue("fixmod")
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2024-03-09 05:08:28 +08:00
|
|
|
"*ScopeMismatch*Requesting fixture stack*",
|
2023-07-18 04:58:35 +08:00
|
|
|
"test_it.py:6: def fixmod(fixfunc)",
|
2024-03-09 05:08:28 +08:00
|
|
|
"Requested fixture:",
|
|
|
|
"test_it.py:3: def fixfunc()",
|
2023-07-18 04:58:35 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_dynamic_scope(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2019-08-21 20:41:37 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--extend-scope", action="store_true", default=False)
|
|
|
|
|
|
|
|
|
|
|
|
def dynamic_scope(fixture_name, config):
|
|
|
|
if config.getoption("--extend-scope"):
|
|
|
|
return "session"
|
|
|
|
return "function"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope=dynamic_scope)
|
|
|
|
def dynamic_fixture(calls=[]):
|
|
|
|
calls.append("call")
|
|
|
|
return len(calls)
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-08-21 20:41:37 +08:00
|
|
|
"""
|
|
|
|
def test_first(dynamic_fixture):
|
|
|
|
assert dynamic_fixture == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_second(dynamic_fixture):
|
|
|
|
assert dynamic_fixture == 2
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-08-21 20:41:37 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("--extend-scope")
|
2019-08-21 20:41:37 +08:00
|
|
|
reprec.assertoutcome(passed=1, failed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_dynamic_scope_bad_return(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-08-21 20:41:37 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def dynamic_scope(**_):
|
|
|
|
return "wrong-scope"
|
|
|
|
|
|
|
|
@pytest.fixture(scope=dynamic_scope)
|
|
|
|
def fixture():
|
|
|
|
pass
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-08-21 20:41:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"Fixture 'fixture' from test_dynamic_scope_bad_return.py "
|
|
|
|
"got an unexpected scope value 'wrong-scope'"
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_register_only_with_mark(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-07-20 20:16:49 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2012-07-20 20:16:49 +08:00
|
|
|
return 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:49 +08:00
|
|
|
test_mod1="""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg(arg):
|
|
|
|
return arg + 1
|
2012-07-20 20:16:49 +08:00
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 2
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-07-20 20:16:49 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_and_scope(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-20 20:16:50 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=["a", "b", "c"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-20 20:16:50 +08:00
|
|
|
def test_param(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-20 20:16:50 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert len(values) == 3
|
|
|
|
assert "a" in values
|
|
|
|
assert "b" in values
|
|
|
|
assert "c" in values
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scope_mismatch(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-07-30 16:46:03 +08:00
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg(arg):
|
2012-07-30 16:46:03 +08:00
|
|
|
pass
|
|
|
|
def test_mismatch(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-07-30 16:46:03 +08:00
|
|
|
result.stdout.fnmatch_lines(["*ScopeMismatch*", "*1 error*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_separated_order(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 1, 2, 2]
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_module_parametrized_ordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2017-09-28 01:42:55 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session", params="s1 s2".split())
|
2012-08-01 19:57:09 +08:00
|
|
|
def sarg():
|
2012-08-01 15:07:32 +08:00
|
|
|
pass
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params="m1 m2".split())
|
2012-08-01 19:57:09 +08:00
|
|
|
def marg():
|
2012-08-01 15:07:32 +08:00
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-08-01 15:07:32 +08:00
|
|
|
test_mod1="""
|
|
|
|
def test_func(sarg):
|
|
|
|
pass
|
|
|
|
def test_func1(marg):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
test_mod2="""
|
|
|
|
def test_func2(sarg):
|
|
|
|
pass
|
|
|
|
def test_func3(sarg, marg):
|
|
|
|
pass
|
|
|
|
def test_func3b(sarg, marg):
|
|
|
|
pass
|
|
|
|
def test_func4(marg):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-08-01 07:29:35 +08:00
|
|
|
test_mod1.py::test_func[s1] PASSED
|
|
|
|
test_mod2.py::test_func2[s1] PASSED
|
|
|
|
test_mod2.py::test_func3[s1-m1] PASSED
|
|
|
|
test_mod2.py::test_func3b[s1-m1] PASSED
|
|
|
|
test_mod2.py::test_func3[s1-m2] PASSED
|
|
|
|
test_mod2.py::test_func3b[s1-m2] PASSED
|
|
|
|
test_mod1.py::test_func[s2] PASSED
|
|
|
|
test_mod2.py::test_func2[s2] PASSED
|
|
|
|
test_mod2.py::test_func3[s2-m1] PASSED
|
|
|
|
test_mod2.py::test_func3b[s2-m1] PASSED
|
|
|
|
test_mod2.py::test_func4[m1] PASSED
|
|
|
|
test_mod2.py::test_func3[s2-m2] PASSED
|
|
|
|
test_mod2.py::test_func3b[s2-m2] PASSED
|
|
|
|
test_mod2.py::test_func4[m2] PASSED
|
|
|
|
test_mod1.py::test_func1[m1] PASSED
|
|
|
|
test_mod1.py::test_func1[m2] PASSED
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_dynamic_parametrized_ordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2018-02-02 05:07:45 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2018-02-02 05:07:45 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
class DynamicFixturePlugin(object):
|
|
|
|
@pytest.fixture(scope='session', params=['flavor1', 'flavor2'])
|
|
|
|
def flavor(self, request):
|
|
|
|
return request.param
|
|
|
|
config.pluginmanager.register(DynamicFixturePlugin(), 'flavor-fixture')
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', params=['vxlan', 'vlan'])
|
|
|
|
def encap(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse='True')
|
|
|
|
def reprovision(request, flavor, encap):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-02-02 05:07:45 +08:00
|
|
|
"""
|
|
|
|
def test(reprovision):
|
|
|
|
pass
|
|
|
|
def test2(reprovision):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-02-02 05:07:45 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
test_dynamic_parametrized_ordering.py::test[flavor1-vxlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test2[flavor1-vxlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test[flavor1-vlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test2[flavor1-vlan] PASSED
|
2024-02-21 16:00:16 +08:00
|
|
|
test_dynamic_parametrized_ordering.py::test[flavor2-vlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test2[flavor2-vlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test[flavor2-vxlan] PASSED
|
|
|
|
test_dynamic_parametrized_ordering.py::test2[flavor2-vxlan] PASSED
|
2018-02-02 05:07:45 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_ordering(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2017-09-28 01:42:55 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2013-10-12 21:39:22 +08:00
|
|
|
"""
|
2012-08-01 15:07:32 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def farg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="class", params=list("ab"))
|
2012-09-17 22:36:10 +08:00
|
|
|
def carg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, farg, carg):
|
2012-08-01 15:07:32 +08:00
|
|
|
def fin():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_%s%s" % (carg, farg))
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(fin)
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass2(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
def test_2(self):
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_3(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-vs")
|
2018-03-14 07:59:10 +08:00
|
|
|
result.stdout.re_match_lines(
|
|
|
|
r"""
|
2018-03-14 08:08:21 +08:00
|
|
|
test_class_ordering.py::TestClass2::test_1\[a-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1\[a-2\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2\[a-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2\[a-2\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1\[b-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1\[b-2\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2\[b-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2\[b-2\] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3\[a-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3\[a-2\] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3\[b-1\] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3\[b-2\] PASSED
|
2012-08-01 15:07:32 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_separated_order_higher_scope_first(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
param = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin:%s" % param))
|
|
|
|
values.append("create:%s" % param)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=["mod1", "mod2"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def modarg(request):
|
|
|
|
param = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin:%s" % param))
|
|
|
|
values.append("create:%s" % param)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test1")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(modarg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test2")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3(arg, modarg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test3")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_4(modarg, arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test4")
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2013-12-07 23:37:46 +08:00
|
|
|
reprec.assertoutcome(passed=12)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
2013-12-07 23:37:46 +08:00
|
|
|
expected = [
|
|
|
|
"create:1",
|
|
|
|
"test1",
|
|
|
|
"fin:1",
|
|
|
|
"create:2",
|
|
|
|
"test1",
|
|
|
|
"fin:2",
|
|
|
|
"create:mod1",
|
|
|
|
"test2",
|
|
|
|
"create:1",
|
|
|
|
"test3",
|
|
|
|
"fin:1",
|
|
|
|
"create:2",
|
|
|
|
"test3",
|
|
|
|
"fin:2",
|
|
|
|
"create:1",
|
|
|
|
"test4",
|
|
|
|
"fin:1",
|
|
|
|
"create:2",
|
|
|
|
"test4",
|
|
|
|
"fin:2",
|
|
|
|
"fin:mod1",
|
|
|
|
"create:mod2",
|
|
|
|
"test2",
|
|
|
|
"create:1",
|
|
|
|
"test3",
|
|
|
|
"fin:1",
|
|
|
|
"create:2",
|
|
|
|
"test3",
|
|
|
|
"fin:2",
|
|
|
|
"create:1",
|
|
|
|
"test4",
|
2018-05-23 22:48:46 +08:00
|
|
|
"fin:1",
|
|
|
|
"create:2",
|
2013-12-07 23:37:46 +08:00
|
|
|
"test4",
|
|
|
|
"fin:2",
|
2017-07-17 07:25:07 +08:00
|
|
|
"fin:mod2",
|
|
|
|
]
|
2013-12-07 23:37:46 +08:00
|
|
|
import pprint
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(list(zip(values, expected)))
|
|
|
|
assert values == expected
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrized_fixture_teardown_order(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-10-22 18:16:54 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=[1,2], scope="class")
|
|
|
|
def param1(request):
|
|
|
|
return request.param
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-22 18:16:54 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-22 18:16:54 +08:00
|
|
|
@classmethod
|
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup1(self, request, param1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-10-22 18:16:54 +08:00
|
|
|
request.addfinalizer(self.teardown1)
|
|
|
|
@classmethod
|
|
|
|
def teardown1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values.pop() == 1
|
2012-10-22 18:16:54 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup2(self, request, param1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-10-22 18:16:54 +08:00
|
|
|
request.addfinalizer(self.teardown2)
|
|
|
|
@classmethod
|
|
|
|
def teardown2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values.pop() == 2
|
2012-10-22 18:16:54 +08:00
|
|
|
def test(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_finish():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2012-10-22 18:16:54 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2012-10-22 18:16:54 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*3 passed*
|
|
|
|
"""
|
|
|
|
)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*error*")
|
2012-10-22 18:16:54 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_finalizer(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2013-11-21 16:26:45 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def browser(request):
|
|
|
|
|
|
|
|
def finalize():
|
2023-06-20 19:55:39 +08:00
|
|
|
sys.stdout.write_text('Finalized', encoding='utf-8')
|
2013-11-21 16:26:45 +08:00
|
|
|
request.addfinalizer(finalize)
|
|
|
|
return {}
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
b = pytester.mkdir("subdir")
|
|
|
|
b.joinpath("test_overridden_fixture_finalizer.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def browser(browser):
|
|
|
|
browser['visited'] = True
|
|
|
|
return browser
|
2013-11-21 16:26:45 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_browser(browser):
|
|
|
|
assert browser['visited'] is True
|
|
|
|
"""
|
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
|
|
|
reprec = pytester.runpytest("-s")
|
2013-11-21 16:26:45 +08:00
|
|
|
for test in ["test_browser"]:
|
2019-03-23 18:36:18 +08:00
|
|
|
reprec.stdout.fnmatch_lines(["*Finalized*"])
|
2013-11-21 16:26:45 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_scope_with_normal_tests(self, pytester: Pytester) -> None:
|
|
|
|
testpath = pytester.makepyfile(
|
2013-11-21 16:26:45 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Box(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
value = 0
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
def a(request):
|
|
|
|
Box.value += 1
|
|
|
|
return Box.value
|
|
|
|
|
|
|
|
def test_a(a):
|
|
|
|
assert a == 1
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Test1(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
def test_b(self, a):
|
|
|
|
assert a == 2
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Test2(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
def test_c(self, a):
|
|
|
|
assert a == 3"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run(testpath)
|
2013-11-21 16:26:45 +08:00
|
|
|
for test in ["test_a", "test_b", "test_c"]:
|
|
|
|
assert reprec.matchreport(test).passed
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_request_is_clean(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-11-21 19:21:52 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-11-21 19:21:52 +08:00
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def fix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append(request.param))
|
2013-11-21 19:21:52 +08:00
|
|
|
def test_fix(fix):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-s")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 2]
|
2013-11-21 19:21:52 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_separated_lifecycle(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
x = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % x))
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-vs")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
2012-08-01 15:07:32 +08:00
|
|
|
import pprint
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(values)
|
|
|
|
# assert len(values) == 6
|
|
|
|
assert values[0] == values[1] == 1
|
|
|
|
assert values[2] == "fin1"
|
|
|
|
assert values[3] == values[4] == 2
|
|
|
|
assert values[5] == "fin2"
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_function_scoped_finalizers_called(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
x = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % x))
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 8
|
|
|
|
assert values == [1, "fin1", 2, "fin2", 1, "fin1", 2, "fin2"]
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2012-07-30 16:46:03 +08:00
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2013-11-21 21:16:44 +08:00
|
|
|
@pytest.mark.parametrize("scope", ["session", "function", "module"])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_finalizer_order_on_parametrization(
|
|
|
|
self, scope, pytester: Pytester
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#246"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-11-21 16:42:24 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-11-21 16:42:24 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r, params=["1"])
|
|
|
|
def fix1(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r)
|
|
|
|
def fix2(request, base):
|
|
|
|
def cleanup_fix2():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values, "base should not have been finalized"
|
2013-11-21 16:42:24 +08:00
|
|
|
request.addfinalizer(cleanup_fix2)
|
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r)
|
|
|
|
def base(request, fix1):
|
|
|
|
def cleanup_base():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_base")
|
2018-11-22 16:15:14 +08:00
|
|
|
print("finalizing base")
|
2013-11-21 16:42:24 +08:00
|
|
|
request.addfinalizer(cleanup_base)
|
|
|
|
|
2013-12-04 23:09:37 +08:00
|
|
|
def test_begin():
|
|
|
|
pass
|
2013-11-21 16:42:24 +08:00
|
|
|
def test_baz(base, fix2):
|
|
|
|
pass
|
2013-11-21 21:16:44 +08:00
|
|
|
def test_other():
|
|
|
|
pass
|
2013-11-21 16:42:24 +08:00
|
|
|
""" # noqa: UP031 (python syntax issues)
|
2024-02-02 21:49:15 +08:00
|
|
|
% {"scope": scope}
|
2013-11-21 16:42:24 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-lvs")
|
2013-12-04 23:09:37 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
2013-11-21 16:42:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_scope_parametrization_ordering(self, pytester: Pytester) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#396"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2013-12-08 03:55:17 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-12-08 03:55:17 +08:00
|
|
|
@pytest.fixture(params=["John", "Doe"], scope="class")
|
|
|
|
def human(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin %s" % request.param))
|
2013-12-08 03:55:17 +08:00
|
|
|
return request.param
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGreetings(object):
|
2013-12-08 03:55:17 +08:00
|
|
|
def test_hello(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_hello")
|
2013-12-08 03:55:17 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMetrics(object):
|
2013-12-08 03:55:17 +08:00
|
|
|
def test_name(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_name")
|
2013-12-08 03:55:17 +08:00
|
|
|
|
|
|
|
def test_population(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_population")
|
2013-12-08 03:55:17 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-12-08 03:55:17 +08:00
|
|
|
reprec.assertoutcome(passed=6)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
2018-06-26 21:35:27 +08:00
|
|
|
assert values == [
|
|
|
|
"test_hello",
|
|
|
|
"fin John",
|
|
|
|
"test_hello",
|
|
|
|
"fin Doe",
|
|
|
|
"test_name",
|
|
|
|
"test_population",
|
|
|
|
"fin John",
|
|
|
|
"test_name",
|
|
|
|
"test_population",
|
|
|
|
"fin Doe",
|
|
|
|
]
|
2013-12-08 03:55:17 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_parametrize_setup_function(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-07-30 16:46:03 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def mysetup(request, arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % arg))
|
|
|
|
values.append("setup%s" % arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3():
|
|
|
|
import pprint
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(values)
|
2012-08-01 15:07:32 +08:00
|
|
|
if arg == 1:
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup1", 1, 1, ]
|
2012-08-01 15:07:32 +08:00
|
|
|
elif arg == 2:
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup1", 1, 1, "fin1",
|
2012-08-01 15:07:32 +08:00
|
|
|
"setup2", 2, 2, ]
|
2012-07-30 16:46:03 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=6)
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_marked_function_not_collected_as_test(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-04-29 16:31:51 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def test_app():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_something(test_app):
|
|
|
|
assert test_app == 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-04-29 16:31:51 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_params_and_ids(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-12-16 06:15:15 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[object(), object()],
|
|
|
|
ids=['alpha', 'beta'])
|
|
|
|
def fix(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_foo(fix):
|
|
|
|
assert 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
res = pytester.runpytest("-v")
|
2013-12-16 06:15:15 +08:00
|
|
|
res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_params_and_ids_yieldfixture(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-12-16 06:15:15 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2020-06-25 19:05:46 +08:00
|
|
|
@pytest.fixture(params=[object(), object()], ids=['alpha', 'beta'])
|
2013-12-16 06:15:15 +08:00
|
|
|
def fix(request):
|
|
|
|
yield request.param
|
|
|
|
|
|
|
|
def test_foo(fix):
|
|
|
|
assert 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
res = pytester.runpytest("-v")
|
2013-12-16 06:15:15 +08:00
|
|
|
res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"])
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_deterministic_fixture_collection(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#920"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2017-07-26 17:08:54 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module",
|
|
|
|
params=["A",
|
|
|
|
"B",
|
|
|
|
"C"])
|
|
|
|
def A(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module",
|
|
|
|
params=["DDDDDDDDD", "EEEEEEEEEEEE", "FFFFFFFFFFF", "banansda"])
|
|
|
|
def B(request, A):
|
|
|
|
return request.param
|
2013-04-29 16:31:51 +08:00
|
|
|
|
2017-07-26 17:08:54 +08:00
|
|
|
def test_foo(B):
|
|
|
|
# Something funky is going on here.
|
|
|
|
# Despite specified seeds, on what is collected,
|
|
|
|
# sometimes we get unexpected passes. hashing B seems
|
|
|
|
# to help?
|
|
|
|
assert hash(B) or True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
monkeypatch.setenv("PYTHONHASHSEED", "1")
|
2020-12-10 13:47:58 +08:00
|
|
|
out1 = pytester.runpytest_subprocess("-v")
|
2017-07-26 17:08:54 +08:00
|
|
|
monkeypatch.setenv("PYTHONHASHSEED", "2")
|
2020-12-10 13:47:58 +08:00
|
|
|
out2 = pytester.runpytest_subprocess("-v")
|
|
|
|
output1 = [
|
2017-07-26 17:08:54 +08:00
|
|
|
line
|
|
|
|
for line in out1.outlines
|
|
|
|
if line.startswith("test_deterministic_fixture_collection.py::test_foo")
|
|
|
|
]
|
2020-12-10 13:47:58 +08:00
|
|
|
output2 = [
|
2017-07-26 17:08:54 +08:00
|
|
|
line
|
|
|
|
for line in out2.outlines
|
|
|
|
if line.startswith("test_deterministic_fixture_collection.py::test_foo")
|
|
|
|
]
|
2020-12-10 13:47:58 +08:00
|
|
|
assert len(output1) == 12
|
|
|
|
assert output1 == output2
|
2017-07-26 17:08:54 +08:00
|
|
|
|
2013-04-29 16:31:51 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestScopeAccess:
|
2017-07-17 07:25:08 +08:00
|
|
|
pytestmark = pytest.mark.parametrize(
|
|
|
|
("scope", "ok", "error"),
|
|
|
|
[
|
2021-03-15 04:20:53 +08:00
|
|
|
["session", "", "path class function module"],
|
|
|
|
["module", "module path", "cls function"],
|
|
|
|
["class", "module path cls", "function"],
|
|
|
|
["function", "module path cls function", ""],
|
2012-09-17 23:32:23 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup(self, pytester: Pytester, scope, ok, error) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-01 19:57:09 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope=%r, autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def myscoped(request):
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
2012-09-17 22:36:10 +08:00
|
|
|
assert hasattr(request, x)
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
|
|
|
pytest.raises(AttributeError, lambda:
|
2012-09-17 22:36:10 +08:00
|
|
|
getattr(request, x))
|
|
|
|
assert request.session
|
|
|
|
assert request.config
|
2012-08-01 19:57:09 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2017-07-17 07:25:08 +08:00
|
|
|
""" # noqa: UP031 (python syntax issues)
|
|
|
|
% (scope, ok.split(), error.split())
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run("-l")
|
2012-08-01 19:57:09 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg(self, pytester: Pytester, scope, ok, error) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-01 19:57:09 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope=%r)
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
2012-09-17 22:36:10 +08:00
|
|
|
assert hasattr(request, x)
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
|
|
|
pytest.raises(AttributeError, lambda:
|
2012-09-17 22:36:10 +08:00
|
|
|
getattr(request, x))
|
|
|
|
assert request.session
|
|
|
|
assert request.config
|
2012-08-01 19:57:09 +08:00
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2017-07-17 07:25:08 +08:00
|
|
|
""" # noqa: UP031 (python syntax issues)
|
|
|
|
% (scope, ok.split(), error.split())
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-08-01 19:57:09 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestErrors:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_subfactory_missing_funcarg(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-08 20:53:47 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-09-17 22:36:10 +08:00
|
|
|
def gen(qwe123):
|
2012-08-08 20:53:47 +08:00
|
|
|
return 1
|
|
|
|
def test_something(gen):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-08-08 20:53:47 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines(
|
2012-10-05 20:24:45 +08:00
|
|
|
["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"]
|
2012-08-08 20:53:47 +08:00
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_issue498_fixture_finalizer_failing(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2014-04-07 19:29:57 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix1(request):
|
|
|
|
def f():
|
|
|
|
raise KeyError
|
2014-04-15 05:42:02 +08:00
|
|
|
request.addfinalizer(f)
|
2014-04-07 19:29:57 +08:00
|
|
|
return object()
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_1(fix1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(fix1)
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_2(fix1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(fix1)
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_3():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values[0] != values[1]
|
2014-04-07 19:29:57 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2014-04-07 19:29:57 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*ERROR*teardown*test_1*
|
|
|
|
*KeyError*
|
|
|
|
*ERROR*teardown*test_2*
|
|
|
|
*KeyError*
|
2019-10-27 23:02:37 +08:00
|
|
|
*3 pass*2 errors*
|
2014-04-07 19:29:57 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-04-15 05:42:02 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setupfunc_missing_funcarg(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-08 20:53:47 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def gen(qwe123):
|
2012-08-08 20:53:47 +08:00
|
|
|
return 1
|
|
|
|
def test_something():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2012-08-08 20:53:47 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines(
|
2012-10-05 20:24:45 +08:00
|
|
|
["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"]
|
2012-08-08 20:53:47 +08:00
|
|
|
)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestShowFixtures:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_funcarg_compat(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfigure("--funcargs")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert config.option.showfixtures
|
2012-08-08 17:48:53 +08:00
|
|
|
|
2023-01-21 19:19:54 +08:00
|
|
|
def test_show_help(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--fixtures", "--help")
|
|
|
|
assert not result.ret
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--fixtures")
|
2019-05-07 06:33:48 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2021-11-10 22:16:41 +08:00
|
|
|
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
|
2019-05-07 06:33:48 +08:00
|
|
|
"*for the test session*",
|
2021-11-10 22:16:41 +08:00
|
|
|
"tmp_path -- .../_pytest/tmpdir.py:*",
|
2019-05-07 06:33:48 +08:00
|
|
|
"*temporary directory*",
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_verbose(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--fixtures", "-v")
|
2019-05-07 06:33:48 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2021-11-10 22:16:41 +08:00
|
|
|
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
|
2019-05-07 06:33:48 +08:00
|
|
|
"*for the test session*",
|
2021-11-10 22:16:41 +08:00
|
|
|
"tmp_path -- .../_pytest/tmpdir.py:*",
|
2019-05-07 06:33:48 +08:00
|
|
|
"*temporary directory*",
|
|
|
|
]
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_testmodule(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2012-11-02 23:04:57 +08:00
|
|
|
'''
|
2012-08-01 19:57:09 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def _arg0():
|
|
|
|
""" hidden """
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
""" hello world """
|
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2021-05-14 20:38:55 +08:00
|
|
|
*tmp_path -- *
|
2012-11-02 23:04:57 +08:00
|
|
|
*fixtures defined from*
|
2021-05-14 20:38:55 +08:00
|
|
|
*arg1 -- test_show_fixtures_testmodule.py:6*
|
2012-11-02 23:04:57 +08:00
|
|
|
*hello world*
|
2012-08-01 19:57:09 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*arg0*")
|
2012-08-01 19:57:09 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.mark.parametrize("testmod", [True, False])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_conftest(self, pytester: Pytester, testmod) -> None:
|
|
|
|
pytester.makeconftest(
|
2012-11-02 23:04:57 +08:00
|
|
|
'''
|
2012-08-01 19:57:09 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
""" hello world """
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
if testmod:
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2012-11-02 23:04:57 +08:00
|
|
|
"""
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2021-03-14 03:22:54 +08:00
|
|
|
*tmp_path*
|
2012-11-02 23:04:57 +08:00
|
|
|
*fixtures defined from*conftest*
|
|
|
|
*arg1*
|
|
|
|
*hello world*
|
2012-08-01 19:57:09 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-09-17 22:36:10 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_trimmed_doc(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
line2
|
2015-03-05 00:00:24 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
|
|
|
@pytest.fixture
|
|
|
|
def arg2():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
line2
|
2015-03-05 00:00:24 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
|
|
|
'''
|
2017-07-15 19:33:11 +08:00
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures", p)
|
2017-07-15 19:33:11 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
* fixtures defined from test_show_fixtures_trimmed_doc *
|
2021-05-14 20:38:55 +08:00
|
|
|
arg2 -- test_show_fixtures_trimmed_doc.py:10
|
2018-08-24 00:06:17 +08:00
|
|
|
line1
|
|
|
|
line2
|
2021-05-14 20:38:55 +08:00
|
|
|
arg1 -- test_show_fixtures_trimmed_doc.py:3
|
2018-08-24 00:06:17 +08:00
|
|
|
line1
|
|
|
|
line2
|
2017-07-15 19:33:11 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-07-15 19:33:11 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_indented_doc(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture1():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
indented line
|
|
|
|
"""
|
2017-07-15 19:33:11 +08:00
|
|
|
'''
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures", p)
|
2017-07-15 19:33:11 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
* fixtures defined from test_show_fixtures_indented_doc *
|
2021-05-14 20:38:55 +08:00
|
|
|
fixture1 -- test_show_fixtures_indented_doc.py:3
|
2018-08-24 00:06:17 +08:00
|
|
|
line1
|
|
|
|
indented line
|
2017-07-15 19:33:11 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-07-15 19:33:11 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_indented_doc_first_line_unindented(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture1():
|
|
|
|
"""line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""
|
2017-07-15 19:33:11 +08:00
|
|
|
'''
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures", p)
|
2017-07-15 19:33:11 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
* fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
|
2021-05-14 20:38:55 +08:00
|
|
|
fixture1 -- test_show_fixtures_indented_doc_first_line_unindented.py:3
|
2018-08-24 00:06:17 +08:00
|
|
|
line1
|
|
|
|
line2
|
|
|
|
indented line
|
2017-07-15 19:33:11 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2013-05-05 20:48:37 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_indented_in_class(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
import pytest
|
|
|
|
class TestClass(object):
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture1(self):
|
|
|
|
"""line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""
|
2017-07-15 19:33:11 +08:00
|
|
|
'''
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures", p)
|
2017-07-15 19:33:11 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
* fixtures defined from test_show_fixtures_indented_in_class *
|
2021-05-14 20:38:55 +08:00
|
|
|
fixture1 -- test_show_fixtures_indented_in_class.py:4
|
2018-08-24 00:06:17 +08:00
|
|
|
line1
|
|
|
|
line2
|
|
|
|
indented line
|
2017-07-15 19:33:11 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2013-05-05 20:48:37 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_different_files(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`--fixtures` only shows fixtures from first file (#833)."""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2015-07-13 04:32:39 +08:00
|
|
|
test_a='''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_a():
|
|
|
|
"""Fixture A"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_a(fix_a):
|
|
|
|
pass
|
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2015-07-13 04:32:39 +08:00
|
|
|
test_b='''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_b():
|
|
|
|
"""Fixture B"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_b(fix_b):
|
|
|
|
pass
|
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures")
|
2015-07-13 04:32:39 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
* fixtures defined from test_a *
|
2021-05-14 20:38:55 +08:00
|
|
|
fix_a -- test_a.py:4
|
2015-07-13 04:32:39 +08:00
|
|
|
Fixture A
|
|
|
|
|
|
|
|
* fixtures defined from test_b *
|
2021-05-14 20:38:55 +08:00
|
|
|
fix_b -- test_b.py:4
|
2015-07-13 04:32:39 +08:00
|
|
|
Fixture B
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_show_fixtures_with_same_name(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2016-05-31 18:46:35 +08:00
|
|
|
'''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""Hello World in conftest.py"""
|
|
|
|
return "Hello World"
|
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-05-31 18:46:35 +08:00
|
|
|
def test_foo(arg1):
|
|
|
|
assert arg1 == "Hello World"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-05-31 18:46:35 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2016-05-31 18:46:35 +08:00
|
|
|
'''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""Hi from test module"""
|
|
|
|
return "Hi"
|
|
|
|
def test_bar(arg1):
|
|
|
|
assert arg1 == "Hi"
|
|
|
|
'''
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("--fixtures")
|
2016-05-31 18:46:35 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-05-31 18:46:35 +08:00
|
|
|
* fixtures defined from conftest *
|
2021-05-14 20:38:55 +08:00
|
|
|
arg1 -- conftest.py:3
|
2016-05-31 18:46:35 +08:00
|
|
|
Hello World in conftest.py
|
|
|
|
|
|
|
|
* fixtures defined from test_show_fixtures_with_same_name *
|
2021-05-14 20:38:55 +08:00
|
|
|
arg1 -- test_show_fixtures_with_same_name.py:3
|
2016-05-31 18:46:35 +08:00
|
|
|
Hi from test module
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-05-31 18:46:35 +08:00
|
|
|
)
|
|
|
|
|
2018-04-24 09:17:46 +08:00
|
|
|
def test_fixture_disallow_twice(self):
|
|
|
|
"""Test that applying @pytest.fixture twice generates an error (#2334)."""
|
|
|
|
with pytest.raises(ValueError):
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2018-04-24 09:17:46 +08:00
|
|
|
@pytest.fixture
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-04-24 09:17:46 +08:00
|
|
|
|
2015-07-13 04:32:39 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestContextManagerFixtureFuncs:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_simple(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("setup")
|
2013-05-05 20:48:37 +08:00
|
|
|
yield 1
|
2018-11-22 16:15:14 +08:00
|
|
|
print("teardown")
|
2013-05-05 20:48:37 +08:00
|
|
|
def test_1(arg1):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("test1", arg1)
|
2013-05-05 20:48:37 +08:00
|
|
|
def test_2(arg1):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("test2", arg1)
|
2013-05-05 20:48:37 +08:00
|
|
|
assert 0
|
2018-06-28 20:36:35 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2013-09-27 18:28:34 +08:00
|
|
|
*setup*
|
|
|
|
*test1 1*
|
|
|
|
*teardown*
|
|
|
|
*setup*
|
|
|
|
*test2 1*
|
|
|
|
*teardown*
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_scoped(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("setup")
|
2013-05-05 20:48:37 +08:00
|
|
|
yield 1
|
2018-11-22 16:15:14 +08:00
|
|
|
print("teardown")
|
2013-05-05 20:48:37 +08:00
|
|
|
def test_1(arg1):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("test1", arg1)
|
2013-05-05 20:48:37 +08:00
|
|
|
def test_2(arg1):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("test2", arg1)
|
2018-06-28 20:36:35 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2013-09-27 18:28:34 +08:00
|
|
|
*setup*
|
|
|
|
*test1 1*
|
|
|
|
*test2 1*
|
|
|
|
*teardown*
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_setup_exception(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
pytest.fail("setup")
|
|
|
|
yield 1
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2018-06-28 20:36:35 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*pytest.fail*setup*
|
|
|
|
*1 error*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_teardown_exception(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
yield 1
|
|
|
|
pytest.fail("teardown")
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2018-06-28 20:36:35 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*pytest.fail*teardown*
|
|
|
|
*1 passed*1 error*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_yields_more_than_one(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-05-05 20:48:37 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2018-06-28 22:08:23 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*fixture function*
|
|
|
|
*test_yields*:2*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_custom_name(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-06-08 07:05:07 +08:00
|
|
|
"""
|
2020-11-01 04:44:10 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(name='meow')
|
2016-06-08 07:05:07 +08:00
|
|
|
def arg1():
|
|
|
|
return 'mew'
|
|
|
|
def test_1(meow):
|
|
|
|
print(meow)
|
2018-06-28 20:36:35 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*mew*"])
|
2016-06-21 18:09:55 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestParameterizedSubRequest:
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_call_from_fixture(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-06-28 22:08:23 +08:00
|
|
|
test_call_from_fixture="""
|
2016-06-20 23:20:24 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def get_named_fixture(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
return request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
|
|
|
|
def test_foo(request, get_named_fixture):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2016-06-20 23:20:24 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-10-04 07:07:59 +08:00
|
|
|
[
|
|
|
|
"The requested fixture has no parameter defined for test:",
|
|
|
|
" test_call_from_fixture.py::test_foo",
|
|
|
|
"Requested fixture 'fix_with_param' defined in:",
|
|
|
|
"test_call_from_fixture.py:4",
|
|
|
|
"Requested here:",
|
|
|
|
"test_call_from_fixture.py:9",
|
|
|
|
"*1 error in*",
|
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_call_from_test(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-06-28 22:08:23 +08:00
|
|
|
test_call_from_test="""
|
2016-06-20 23:20:24 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_foo(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2016-06-20 23:20:24 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-10-04 07:07:59 +08:00
|
|
|
[
|
|
|
|
"The requested fixture has no parameter defined for test:",
|
|
|
|
" test_call_from_test.py::test_foo",
|
|
|
|
"Requested fixture 'fix_with_param' defined in:",
|
|
|
|
"test_call_from_test.py:4",
|
|
|
|
"Requested here:",
|
|
|
|
"test_call_from_test.py:8",
|
|
|
|
"*1 failed*",
|
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_external_fixture(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2016-06-20 23:20:24 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-06-28 22:08:23 +08:00
|
|
|
test_external_fixture="""
|
2016-06-20 23:20:24 +08:00
|
|
|
def test_foo(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2016-06-20 23:20:24 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-10-04 07:07:59 +08:00
|
|
|
[
|
|
|
|
"The requested fixture has no parameter defined for test:",
|
|
|
|
" test_external_fixture.py::test_foo",
|
|
|
|
"",
|
|
|
|
"Requested fixture 'fix_with_param' defined in:",
|
|
|
|
"conftest.py:4",
|
|
|
|
"Requested here:",
|
|
|
|
"test_external_fixture.py:2",
|
|
|
|
"*1 failed*",
|
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_non_relative_path(self, pytester: Pytester) -> None:
|
|
|
|
tests_dir = pytester.mkdir("tests")
|
|
|
|
fixdir = pytester.mkdir("fixtures")
|
|
|
|
fixfile = fixdir.joinpath("fix.py")
|
|
|
|
fixfile.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
testfile = tests_dir.joinpath("test_foos.py")
|
|
|
|
testfile.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
from fix import fix_with_param
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_foo(request):
|
|
|
|
request.getfixturevalue('fix_with_param')
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-20 23:20:24 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
os.chdir(tests_dir)
|
|
|
|
pytester.syspathinsert(fixdir)
|
|
|
|
result = pytester.runpytest()
|
2016-06-20 23:20:24 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2018-10-04 07:07:59 +08:00
|
|
|
[
|
|
|
|
"The requested fixture has no parameter defined for test:",
|
|
|
|
" test_foos.py::test_foo",
|
|
|
|
"",
|
|
|
|
"Requested fixture 'fix_with_param' defined in:",
|
2020-10-03 04:16:22 +08:00
|
|
|
f"{fixfile}:4",
|
2018-10-04 07:07:59 +08:00
|
|
|
"Requested here:",
|
|
|
|
"test_foos.py:4",
|
|
|
|
"*1 failed*",
|
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-21 18:09:55 +08:00
|
|
|
|
2020-01-29 01:39:47 +08:00
|
|
|
# With non-overlapping rootdir, passing tests_dir.
|
2020-12-10 13:47:58 +08:00
|
|
|
rootdir = pytester.mkdir("rootdir")
|
|
|
|
os.chdir(rootdir)
|
|
|
|
result = pytester.runpytest("--rootdir", rootdir, tests_dir)
|
2020-01-29 01:39:47 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"The requested fixture has no parameter defined for test:",
|
|
|
|
" test_foos.py::test_foo",
|
|
|
|
"",
|
|
|
|
"Requested fixture 'fix_with_param' defined in:",
|
2020-10-03 04:16:22 +08:00
|
|
|
f"{fixfile}:4",
|
2020-01-29 01:39:47 +08:00
|
|
|
"Requested here:",
|
2020-10-03 04:16:22 +08:00
|
|
|
f"{testfile}:4",
|
2020-01-29 01:39:47 +08:00
|
|
|
"*1 failed*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2016-06-21 18:09:55 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_pytest_fixture_setup_and_post_finalizer_hook(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2016-12-10 18:45:40 +08:00
|
|
|
"""
|
2017-11-12 21:14:55 +08:00
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
print('ROOT setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
def pytest_fixture_post_finalizer(fixturedef, request):
|
|
|
|
print('ROOT finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
2016-12-10 18:45:40 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2017-11-12 21:14:55 +08:00
|
|
|
**{
|
|
|
|
"tests/conftest.py": """
|
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
print('TESTS setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
def pytest_fixture_post_finalizer(fixturedef, request):
|
|
|
|
print('TESTS finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
""",
|
|
|
|
"tests/test_hooks.py": """
|
|
|
|
import pytest
|
2016-12-10 18:45:40 +08:00
|
|
|
|
2017-11-12 21:14:55 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def my_fixture():
|
|
|
|
return 'some'
|
2016-12-10 18:45:40 +08:00
|
|
|
|
2017-11-12 21:14:55 +08:00
|
|
|
def test_func(my_fixture):
|
|
|
|
print('TEST test_func')
|
|
|
|
assert my_fixture == 'some'
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-s")
|
2016-12-10 18:45:40 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2017-11-12 21:14:55 +08:00
|
|
|
"*TESTS setup hook called for my_fixture from test_func*",
|
|
|
|
"*ROOT setup hook called for my_fixture from test_func*",
|
|
|
|
"*TEST test_func*",
|
|
|
|
"*TESTS finalizer hook called for my_fixture from test_func*",
|
|
|
|
"*ROOT finalizer hook called for my_fixture from test_func*",
|
2016-12-10 18:45:40 +08:00
|
|
|
]
|
|
|
|
)
|
2018-03-14 08:08:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestScopeOrdering:
|
|
|
|
"""Class of tests that ensure fixtures are ordered based on their scopes (#2405)"""
|
|
|
|
|
2018-06-28 22:50:06 +08:00
|
|
|
@pytest.mark.parametrize("variant", ["mark", "autouse"])
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_module_auto(
|
|
|
|
self, pytester: Pytester, variant, monkeypatch
|
|
|
|
) -> None:
|
2018-03-14 08:08:21 +08:00
|
|
|
"""Semantically identical to the example posted in #2405 when ``use_mark=True``"""
|
2018-06-28 22:50:06 +08:00
|
|
|
monkeypatch.setenv("FIXTURE_ACTIVATION_VARIANT", variant)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
2018-06-28 22:50:06 +08:00
|
|
|
import warnings
|
|
|
|
import os
|
2018-03-14 08:08:21 +08:00
|
|
|
import pytest
|
2018-06-28 22:50:06 +08:00
|
|
|
VAR = 'FIXTURE_ACTIVATION_VARIANT'
|
|
|
|
VALID_VARS = ('autouse', 'mark')
|
|
|
|
|
|
|
|
VARIANT = os.environ.get(VAR)
|
|
|
|
if VARIANT is None or VARIANT not in VALID_VARS:
|
2018-06-29 13:13:18 +08:00
|
|
|
warnings.warn("{!r} is not in {}, assuming autouse".format(VARIANT, VALID_VARS) )
|
2018-06-28 22:50:06 +08:00
|
|
|
variant = 'mark'
|
2018-03-14 08:08:21 +08:00
|
|
|
|
2018-06-28 22:50:06 +08:00
|
|
|
@pytest.fixture(scope='module', autouse=VARIANT == 'autouse')
|
2018-03-14 08:08:21 +08:00
|
|
|
def m1(): pass
|
|
|
|
|
2018-06-28 22:50:06 +08:00
|
|
|
if VARIANT=='mark':
|
2018-03-14 08:08:21 +08:00
|
|
|
pytestmark = pytest.mark.usefixtures('m1')
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function', autouse=True)
|
|
|
|
def f1(): pass
|
|
|
|
|
|
|
|
def test_func(m1):
|
|
|
|
pass
|
2018-06-28 22:50:06 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-03-14 08:08:21 +08:00
|
|
|
assert request.fixturenames == "m1 f1".split()
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_with_native_fixtures(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2018-03-14 08:08:21 +08:00
|
|
|
"""Sanity check that verifies the order returned by the closures and the actual fixture execution order:
|
|
|
|
The execution order may differ because of fixture inter-dependencies.
|
|
|
|
"""
|
|
|
|
monkeypatch.setattr(pytest, "FIXTURE_ORDER", [], raising=False)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
FIXTURE_ORDER = pytest.FIXTURE_ORDER
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def s1():
|
|
|
|
FIXTURE_ORDER.append('s1')
|
|
|
|
|
2018-04-25 04:32:58 +08:00
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def p1():
|
|
|
|
FIXTURE_ORDER.append('p1')
|
|
|
|
|
2018-03-14 08:08:21 +08:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def m1():
|
|
|
|
FIXTURE_ORDER.append('m1')
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2021-03-14 03:22:54 +08:00
|
|
|
def my_tmp_path_factory():
|
|
|
|
FIXTURE_ORDER.append('my_tmp_path_factory')
|
2018-03-14 08:08:21 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
2021-03-14 03:22:54 +08:00
|
|
|
def my_tmp_path(my_tmp_path_factory):
|
|
|
|
FIXTURE_ORDER.append('my_tmp_path')
|
2018-03-14 08:08:21 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
2021-03-14 03:22:54 +08:00
|
|
|
def f1(my_tmp_path):
|
2018-03-14 08:08:21 +08:00
|
|
|
FIXTURE_ORDER.append('f1')
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def f2():
|
|
|
|
FIXTURE_ORDER.append('f2')
|
|
|
|
|
2018-04-25 04:32:58 +08:00
|
|
|
def test_foo(f1, p1, m1, f2, s1): pass
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-03-14 08:08:21 +08:00
|
|
|
# order of fixtures based on their scope and position in the parameter list
|
2018-07-06 05:15:17 +08:00
|
|
|
assert (
|
2021-03-14 03:22:54 +08:00
|
|
|
request.fixturenames
|
|
|
|
== "s1 my_tmp_path_factory p1 m1 f1 f2 my_tmp_path".split()
|
2018-07-06 05:15:17 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.runpytest()
|
2021-03-14 03:22:54 +08:00
|
|
|
# actual fixture execution differs: dependent fixtures must be created first ("my_tmp_path")
|
2020-07-10 14:44:14 +08:00
|
|
|
FIXTURE_ORDER = pytest.FIXTURE_ORDER # type: ignore[attr-defined]
|
2021-03-14 03:22:54 +08:00
|
|
|
assert FIXTURE_ORDER == "s1 my_tmp_path_factory p1 m1 my_tmp_path f1 f2".split()
|
2018-03-14 08:08:21 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_module(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def m1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f1(): pass
|
|
|
|
|
|
|
|
def test_func(f1, m1):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-03-14 08:08:21 +08:00
|
|
|
assert request.fixturenames == "m1 f1".split()
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_scopes_reordered(self, pytester: Pytester) -> None:
|
2018-03-14 08:08:21 +08:00
|
|
|
"""Test ensures that fixtures are ordered by scope regardless of the order of the parameters, although
|
|
|
|
fixtures of same scope keep the declared order
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def s1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def m1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f2(): pass
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
def c1(cls): pass
|
|
|
|
|
|
|
|
def test_func(self, f2, f1, c1, m1, s1):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-03-14 08:08:21 +08:00
|
|
|
assert request.fixturenames == "s1 m1 c1 f2 f1".split()
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_same_scope_closer_root_first(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
2018-03-14 08:08:21 +08:00
|
|
|
"""Auto-use fixtures of same scope are ordered by closer-to-root first"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def m_conf(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
**{
|
|
|
|
"sub/conftest.py": """
|
|
|
|
import pytest
|
|
|
|
|
2018-04-25 04:32:58 +08:00
|
|
|
@pytest.fixture(scope='package', autouse=True)
|
|
|
|
def p_sub(): pass
|
|
|
|
|
2018-03-14 08:08:21 +08:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def m_sub(): pass
|
|
|
|
""",
|
2018-07-06 05:15:17 +08:00
|
|
|
"sub/__init__.py": "",
|
2018-03-14 08:08:21 +08:00
|
|
|
"sub/test_func.py": """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def m_test(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f1(): pass
|
|
|
|
|
|
|
|
def test_func(m_test, f1):
|
|
|
|
pass
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
}
|
2018-03-14 08:08:21 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-07-06 05:15:17 +08:00
|
|
|
assert request.fixturenames == "p_sub m_conf m_sub m_test f1".split()
|
2018-03-14 08:08:21 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_func_closure_all_scopes_complex(self, pytester: Pytester) -> None:
|
2018-03-14 08:08:21 +08:00
|
|
|
"""Complex test involving all scopes and mixing autouse with normal fixtures"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def s1(): pass
|
2018-04-25 04:32:58 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope='package', autouse=True)
|
|
|
|
def p1(): pass
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(**{"__init__.py": ""})
|
|
|
|
pytester.makepyfile(
|
2018-03-14 08:08:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def m1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def m2(s1): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f1(): pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def f2(): pass
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class', autouse=True)
|
|
|
|
def c1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func(self, f2, f1, m2):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
items, _ = pytester.inline_genitems()
|
2023-07-10 19:50:25 +08:00
|
|
|
assert isinstance(items[0], Function)
|
2023-07-16 05:37:33 +08:00
|
|
|
request = TopRequest(items[0], _ispytest=True)
|
2018-07-06 05:15:17 +08:00
|
|
|
assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split()
|
2018-08-24 13:48:44 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_multiple_packages(self, pytester: Pytester) -> None:
|
2018-08-24 13:48:44 +08:00
|
|
|
"""Complex test involving multiple package fixtures. Make sure teardowns
|
|
|
|
are executed in order.
|
|
|
|
.
|
|
|
|
└── root
|
|
|
|
├── __init__.py
|
|
|
|
├── sub1
|
|
|
|
│ ├── __init__.py
|
|
|
|
│ ├── conftest.py
|
|
|
|
│ └── test_1.py
|
|
|
|
└── sub2
|
|
|
|
├── __init__.py
|
|
|
|
├── conftest.py
|
|
|
|
└── test_2.py
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
root = pytester.mkdir("root")
|
2023-06-20 19:55:39 +08:00
|
|
|
root.joinpath("__init__.py").write_text("values = []", encoding="utf-8")
|
2020-12-10 13:47:58 +08:00
|
|
|
sub1 = root.joinpath("sub1")
|
|
|
|
sub1.mkdir()
|
|
|
|
sub1.joinpath("__init__.py").touch()
|
|
|
|
sub1.joinpath("conftest.py").write_text(
|
2018-08-25 02:54:04 +08:00
|
|
|
textwrap.dedent(
|
2018-08-24 13:48:44 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
from .. import values
|
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def fix():
|
|
|
|
values.append("pre-sub1")
|
|
|
|
yield values
|
2018-08-25 06:51:42 +08:00
|
|
|
assert values.pop() == "pre-sub1"
|
2018-08-24 13:48:44 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-08-24 13:48:44 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
sub1.joinpath("test_1.py").write_text(
|
2018-08-25 02:54:04 +08:00
|
|
|
textwrap.dedent(
|
2018-08-24 13:48:44 +08:00
|
|
|
"""\
|
|
|
|
from .. import values
|
|
|
|
def test_1(fix):
|
|
|
|
assert values == ["pre-sub1"]
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-08-24 13:48:44 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
sub2 = root.joinpath("sub2")
|
|
|
|
sub2.mkdir()
|
|
|
|
sub2.joinpath("__init__.py").touch()
|
|
|
|
sub2.joinpath("conftest.py").write_text(
|
2018-08-25 02:54:04 +08:00
|
|
|
textwrap.dedent(
|
2018-08-24 13:48:44 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
from .. import values
|
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def fix():
|
|
|
|
values.append("pre-sub2")
|
|
|
|
yield values
|
2018-08-25 06:51:42 +08:00
|
|
|
assert values.pop() == "pre-sub2"
|
2018-08-24 13:48:44 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-08-24 13:48:44 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
sub2.joinpath("test_2.py").write_text(
|
2018-08-25 02:54:04 +08:00
|
|
|
textwrap.dedent(
|
2018-08-24 13:48:44 +08:00
|
|
|
"""\
|
|
|
|
from .. import values
|
|
|
|
def test_2(fix):
|
|
|
|
assert values == ["pre-sub2"]
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-08-24 13:48:44 +08:00
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2018-08-24 13:48:44 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2018-12-19 07:05:48 +08:00
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_class_fixture_self_instance(self, pytester: Pytester) -> None:
|
2019-08-30 22:20:19 +08:00
|
|
|
"""Check that plugin classes which implement fixtures receive the plugin instance
|
|
|
|
as self (see #2270).
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makeconftest(
|
2019-08-20 03:57:39 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.pluginmanager.register(MyPlugin())
|
|
|
|
|
|
|
|
class MyPlugin():
|
|
|
|
def __init__(self):
|
|
|
|
self.arg = 1
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def myfix(self):
|
|
|
|
assert isinstance(self, MyPlugin)
|
|
|
|
return self.arg
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-08-20 03:57:39 +08:00
|
|
|
"""
|
|
|
|
class TestClass(object):
|
|
|
|
def test_1(self, myfix):
|
|
|
|
assert myfix == 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-08-20 03:57:39 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2018-12-19 07:05:48 +08:00
|
|
|
|
|
|
|
def test_call_fixture_function_error():
|
|
|
|
"""Check if an error is raised if a fixture function is called directly (#4545)"""
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
2019-08-16 06:09:14 +08:00
|
|
|
raise NotImplementedError()
|
2018-12-19 07:05:48 +08:00
|
|
|
|
|
|
|
with pytest.raises(pytest.fail.Exception):
|
|
|
|
assert fix() == 1
|
2019-05-13 07:49:18 +08:00
|
|
|
|
|
|
|
|
2024-02-09 21:59:04 +08:00
|
|
|
def test_fixture_double_decorator(pytester: Pytester) -> None:
|
|
|
|
"""Check if an error is raised when using @pytest.fixture twice."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.fixture
|
|
|
|
def fixt():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.assert_outcomes(errors=1)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"E * ValueError: @pytest.fixture is being applied more than once to the same function 'fixt'"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_param_shadowing(pytester: Pytester) -> None:
|
2019-05-13 07:49:18 +08:00
|
|
|
"""Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-05-13 07:49:18 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=['a', 'b'])
|
|
|
|
def argroot(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(argroot):
|
|
|
|
return argroot
|
|
|
|
|
|
|
|
# This should only be parametrized directly
|
|
|
|
@pytest.mark.parametrize("arg", [1])
|
|
|
|
def test_direct(arg):
|
|
|
|
assert arg == 1
|
|
|
|
|
|
|
|
# This should be parametrized based on the fixtures
|
|
|
|
def test_normal_fixture(arg):
|
|
|
|
assert isinstance(arg, str)
|
|
|
|
|
|
|
|
# Indirect should still work:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
|
|
|
return 2*request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("arg2", [1], indirect=True)
|
|
|
|
def test_indirect(arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# Only one test should have run
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2019-05-13 07:49:18 +08:00
|
|
|
result.assert_outcomes(passed=4)
|
|
|
|
result.stdout.fnmatch_lines(["*::test_direct[[]1[]]*"])
|
|
|
|
result.stdout.fnmatch_lines(["*::test_normal_fixture[[]a[]]*"])
|
|
|
|
result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"])
|
|
|
|
result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"])
|
2019-07-01 00:14:38 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_named_request(pytester: Pytester) -> None:
|
|
|
|
pytester.copy_example("fixtures/test_fixture_named_request.py")
|
|
|
|
result = pytester.runpytest()
|
2019-07-01 00:14:38 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*'request' is a reserved word for fixtures, use another name:",
|
2024-01-28 21:12:42 +08:00
|
|
|
" *test_fixture_named_request.py:6",
|
2019-07-01 00:14:38 +08:00
|
|
|
]
|
|
|
|
)
|
2019-08-21 20:41:37 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_indirect_fixture_does_not_break_scope(pytester: Pytester) -> None:
|
2019-08-30 21:54:07 +08:00
|
|
|
"""Ensure that fixture scope is respected when using indirect fixtures (#570)"""
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-08-29 01:50:13 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2019-08-30 21:54:07 +08:00
|
|
|
instantiated = []
|
2019-08-29 01:50:13 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2019-08-30 21:54:07 +08:00
|
|
|
def fixture_1(request):
|
|
|
|
instantiated.append(("fixture_1", request.param))
|
2019-08-29 01:50:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def fixture_2(request):
|
2019-08-30 21:54:07 +08:00
|
|
|
instantiated.append(("fixture_2", request.param))
|
2019-08-29 01:50:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
scenarios = [
|
2019-08-30 21:54:07 +08:00
|
|
|
("A", "a1"),
|
|
|
|
("A", "a2"),
|
|
|
|
("B", "b1"),
|
|
|
|
("B", "b2"),
|
|
|
|
("C", "c1"),
|
|
|
|
("C", "c2"),
|
2019-08-29 01:50:13 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"fixture_1,fixture_2", scenarios, indirect=["fixture_1", "fixture_2"]
|
|
|
|
)
|
2019-08-30 21:54:07 +08:00
|
|
|
def test_create_fixtures(fixture_1, fixture_2):
|
|
|
|
pass
|
2019-08-29 01:50:13 +08:00
|
|
|
|
2019-08-30 21:54:07 +08:00
|
|
|
|
|
|
|
def test_check_fixture_instantiations():
|
|
|
|
assert instantiated == [
|
|
|
|
('fixture_1', 'A'),
|
|
|
|
('fixture_2', 'a1'),
|
|
|
|
('fixture_2', 'a2'),
|
|
|
|
('fixture_1', 'B'),
|
|
|
|
('fixture_2', 'b1'),
|
|
|
|
('fixture_2', 'b2'),
|
|
|
|
('fixture_1', 'C'),
|
|
|
|
('fixture_2', 'c1'),
|
|
|
|
('fixture_2', 'c2'),
|
|
|
|
]
|
2019-08-29 01:50:13 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-08-30 21:54:07 +08:00
|
|
|
result.assert_outcomes(passed=7)
|
2019-10-12 20:33:43 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_parametrization_nparray(pytester: Pytester) -> None:
|
2019-10-17 05:45:01 +08:00
|
|
|
pytest.importorskip("numpy")
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
pytester.makepyfile(
|
2019-10-12 21:08:47 +08:00
|
|
|
"""
|
2019-10-12 20:33:43 +08:00
|
|
|
from numpy import linspace
|
|
|
|
from pytest import fixture
|
|
|
|
|
|
|
|
@fixture(params=linspace(1, 10, 10))
|
|
|
|
def value(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_bug(value):
|
|
|
|
assert value == value
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2019-10-12 20:33:43 +08:00
|
|
|
result.assert_outcomes(passed=10)
|
2020-01-24 19:44:51 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_fixture_arg_ordering(pytester: Pytester) -> None:
|
2020-01-24 19:44:51 +08:00
|
|
|
"""
|
|
|
|
This test describes how fixtures in the same scope but without explicit dependencies
|
|
|
|
between them are created. While users should make dependencies explicit, often
|
|
|
|
they rely on this order, so this test exists to catch regressions in this regard.
|
|
|
|
See #6540 and #6492.
|
|
|
|
"""
|
2020-12-10 13:47:58 +08:00
|
|
|
p1 = pytester.makepyfile(
|
2020-01-24 19:44:51 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
suffixes = []
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_1(): suffixes.append("fix_1")
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_2(): suffixes.append("fix_2")
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_3(): suffixes.append("fix_3")
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_4(): suffixes.append("fix_4")
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_5(): suffixes.append("fix_5")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_combined(fix_1, fix_2, fix_3, fix_4, fix_5): pass
|
|
|
|
|
|
|
|
def test_suffix(fix_combined):
|
|
|
|
assert suffixes == ["fix_1", "fix_2", "fix_3", "fix_4", "fix_5"]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest("-vv", str(p1))
|
2020-01-24 19:44:51 +08:00
|
|
|
assert result.ret == 0
|
2020-04-15 17:17:13 +08:00
|
|
|
|
|
|
|
|
2020-12-10 13:47:58 +08:00
|
|
|
def test_yield_fixture_with_no_value(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-04-15 17:17:13 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(name='custom')
|
|
|
|
def empty_yield():
|
|
|
|
if False:
|
|
|
|
yield
|
|
|
|
|
|
|
|
def test_fixt(custom):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected = "E ValueError: custom did not yield a value"
|
2020-12-10 13:47:58 +08:00
|
|
|
result = pytester.runpytest()
|
2020-06-13 21:24:44 +08:00
|
|
|
result.assert_outcomes(errors=1)
|
2020-04-15 17:17:13 +08:00
|
|
|
result.stdout.fnmatch_lines([expected])
|
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
2023-09-05 04:44:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_deduplicate_names() -> None:
|
|
|
|
items = deduplicate_names("abacd")
|
|
|
|
assert items == ("a", "b", "c", "d")
|
2024-02-02 22:12:50 +08:00
|
|
|
items = deduplicate_names((*items, "g", "f", "g", "e", "b"))
|
2023-09-05 04:44:49 +08:00
|
|
|
assert items == ("a", "b", "c", "d", "g", "f", "e")
|
2024-03-09 15:08:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_staticmethod_classmethod_fixture_instance(pytester: Pytester) -> None:
|
|
|
|
"""Ensure that static and class methods get and have access to a fresh
|
|
|
|
instance.
|
|
|
|
|
|
|
|
This also ensures `setup_method` works well with static and class methods.
|
|
|
|
|
|
|
|
Regression test for #12065.
|
|
|
|
"""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
ran_setup_method = False
|
|
|
|
ran_fixture = False
|
|
|
|
|
|
|
|
def setup_method(self):
|
|
|
|
assert not self.ran_setup_method
|
|
|
|
self.ran_setup_method = True
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def fixture(self):
|
|
|
|
assert not self.ran_fixture
|
|
|
|
self.ran_fixture = True
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
assert self.ran_setup_method
|
|
|
|
assert self.ran_fixture
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def test_1(request):
|
|
|
|
assert request.instance.ran_setup_method
|
|
|
|
assert request.instance.ran_fixture
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def test_2(cls, request):
|
|
|
|
assert request.instance.ran_setup_method
|
|
|
|
assert request.instance.ran_fixture
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == ExitCode.OK
|
|
|
|
result.assert_outcomes(passed=3)
|
2024-03-15 19:26:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_scoped_fixture_caching(pytester: Pytester) -> None:
|
|
|
|
"""Make sure setup and finalization is only run once when using scoped fixture
|
|
|
|
multiple times."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Generator
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
executed: list[str] = []
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def fixture_1() -> Generator[None, None, None]:
|
|
|
|
executed.append("fix setup")
|
|
|
|
yield
|
|
|
|
executed.append("fix teardown")
|
|
|
|
|
|
|
|
|
|
|
|
class TestFixtureCaching:
|
|
|
|
def test_1(self, fixture_1: None) -> None:
|
|
|
|
assert executed == ["fix setup"]
|
|
|
|
|
|
|
|
def test_2(self, fixture_1: None) -> None:
|
|
|
|
assert executed == ["fix setup"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_expected_setup_and_teardown() -> None:
|
|
|
|
assert executed == ["fix setup", "fix teardown"]
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_scoped_fixture_caching_exception(pytester: Pytester) -> None:
|
|
|
|
"""Make sure setup & finalization is only run once for scoped fixture, with a cached exception."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
executed_crash: list[str] = []
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def fixture_crash(request: pytest.FixtureRequest) -> None:
|
|
|
|
executed_crash.append("fix_crash setup")
|
|
|
|
|
|
|
|
def my_finalizer() -> None:
|
|
|
|
executed_crash.append("fix_crash teardown")
|
|
|
|
|
|
|
|
request.addfinalizer(my_finalizer)
|
|
|
|
|
|
|
|
raise Exception("foo")
|
|
|
|
|
|
|
|
|
|
|
|
class TestFixtureCachingException:
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_crash_1(self, fixture_crash: None) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_crash_2(self, fixture_crash: None) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
def test_crash_expected_setup_and_teardown() -> None:
|
|
|
|
assert executed_crash == ["fix_crash setup", "fix_crash teardown"]
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == 0
|
2024-03-17 05:45:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_scoped_fixture_teardown_order(pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Make sure teardowns happen in reverse order of setup with scoped fixtures, when
|
|
|
|
a later test only depends on a subset of scoped fixtures.
|
|
|
|
|
|
|
|
Regression test for https://github.com/pytest-dev/pytest/issues/1489
|
|
|
|
"""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
from typing import Generator
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
last_executed = ""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixture_1() -> Generator[None, None, None]:
|
|
|
|
global last_executed
|
|
|
|
assert last_executed == ""
|
|
|
|
last_executed = "fixture_1_setup"
|
|
|
|
yield
|
|
|
|
assert last_executed == "fixture_2_teardown"
|
|
|
|
last_executed = "fixture_1_teardown"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def fixture_2() -> Generator[None, None, None]:
|
|
|
|
global last_executed
|
|
|
|
assert last_executed == "fixture_1_setup"
|
|
|
|
last_executed = "fixture_2_setup"
|
|
|
|
yield
|
|
|
|
assert last_executed == "run_test"
|
|
|
|
last_executed = "fixture_2_teardown"
|
|
|
|
|
|
|
|
|
|
|
|
def test_fixture_teardown_order(fixture_1: None, fixture_2: None) -> None:
|
|
|
|
global last_executed
|
|
|
|
assert last_executed == "fixture_2_setup"
|
|
|
|
last_executed = "run_test"
|
|
|
|
|
|
|
|
|
|
|
|
def test_2(fixture_1: None) -> None:
|
|
|
|
# This would previously queue an additional teardown of fixture_1,
|
|
|
|
# despite fixture_1's value being cached, which caused fixture_1 to be
|
|
|
|
# torn down before fixture_2 - violating the rule that teardowns should
|
|
|
|
# happen in reverse order of setup.
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == 0
|