本文整理汇总了Python中pytest.Function方法的典型用法代码示例。如果您正苦于以下问题:Python pytest.Function方法的具体用法?Python pytest.Function怎么用?Python pytest.Function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytest
的用法示例。
在下文中一共展示了pytest.Function方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pytest_runtest_setup
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_runtest_setup(item):
# here we seem to get called only based on what we collected
# in pytest_collection_modifyitems. So to do class-based stuff
# we have to tear that out.
global _current_class
if not isinstance(item, pytest.Function):
return
# ... so we're doing a little dance here to figure it out...
if _current_class is None:
class_setup(item.parent.parent)
_current_class = item.parent.parent
# this is needed for the class-level, to ensure that the
# teardown runs after the class is completed with its own
# class-level teardown...
def finalize():
global _current_class
class_teardown(item.parent.parent)
_current_class = None
item.parent.parent.addfinalizer(finalize)
test_setup(item)
示例2: test_custom_failure_repr
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_custom_failure_repr(self, testdir) -> None:
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
return "hello"
"""
)
reports = testdir.runitem(
"""
import pytest
def test_func():
assert 0
"""
)
rep = reports[1]
assert not rep.skipped
assert not rep.passed
assert rep.failed
# assert rep.outcome.when == "call"
# assert rep.failed.where.lineno == 3
# assert rep.failed.where.path.basename == "test_func.py"
# assert rep.failed.failurerepr == "hello"
示例3: test_getparent
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_getparent(self, testdir):
modcol = testdir.getmodulecol(
"""
class TestClass:
def test_foo(self):
pass
"""
)
cls = testdir.collect_by_name(modcol, "TestClass")
fn = testdir.collect_by_name(testdir.collect_by_name(cls, "()"), "test_foo")
parent = fn.getparent(pytest.Module)
assert parent is modcol
parent = fn.getparent(pytest.Function)
assert parent is fn
parent = fn.getparent(pytest.Class)
assert parent is cls
示例4: pytest_runtest_setup
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_runtest_setup(item):
# Conditionally skip tests that are pinned to a specific ansible version
if isinstance(item, pytest.Function):
# conditionally skip
if item.get_closest_marker('requires_ansible_v1') and not has_ansible_v1:
pytest.skip("requires < ansible-2.*")
if item.get_closest_marker('requires_ansible_v2') and has_ansible_v1:
pytest.skip("requires >= ansible-2.*")
if item.get_closest_marker('requires_ansible_v24') and not has_ansible_v24:
pytest.skip("requires >= ansible-2.4.*")
if item.get_closest_marker('requires_ansible_v28') and not has_ansible_v24:
pytest.skip("requires >= ansible-2.8.*")
# conditionally xfail
mark = item.get_closest_marker('ansible_v1_xfail')
if mark and has_ansible_v1:
item.add_marker(pytest.mark.xfail(reason="expected failure on < ansible-2.*",
raises=mark.kwargs.get('raises')))
mark = item.get_closest_marker('ansible_v2_xfail')
if mark and not has_ansible_v1:
item.add_marker(pytest.xfail(reason="expected failure on >= ansible-2.*",
raises=mark.kwargs.get('raises')))
示例5: pytest_runtest_setup
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_runtest_setup(item):
# here we seem to get called only based on what we collected
# in pytest_collection_modifyitems. So to do class-based stuff
# we have to tear that out.
global _current_class
if not isinstance(item, pytest.Function):
return
# ... so we're doing a little dance here to figure it out...
if item.parent.parent is not _current_class:
class_setup(item.parent.parent)
_current_class = item.parent.parent
# this is needed for the class-level, to ensure that the
# teardown runs after the class is completed with its own
# class-level teardown...
item.parent.parent.addfinalizer(
lambda: class_teardown(item.parent.parent))
test_setup(item)
示例6: pytest_runtest_setup
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_runtest_setup(item):
# here we seem to get called only based on what we collected
# in pytest_collection_modifyitems. So to do class-based stuff
# we have to tear that out.
global _current_class
if not isinstance(item, pytest.Function):
return
# ... so we're doing a little dance here to figure it out...
if _current_class is None:
class_setup(item.parent.parent)
_current_class = item.parent.parent
# this is needed for the class-level, to ensure that the
# teardown runs after the class is completed with its own
# class-level teardown...
def finalize():
global _current_class
class_teardown(item.parent.parent)
_current_class = None
item.parent.parent.addfinalizer(finalize)
test_setup(item)
示例7: pytest_runtest_setup
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_runtest_setup(item):
# here we seem to get called only based on what we collected
# in pytest_collection_modifyitems. So to do class-based stuff
# we have to tear that out.
global _current_class
if not isinstance(item, pytest.Function):
return
# ... so we're doing a little dance here to figure it out...
if item.parent.parent is not _current_class:
class_setup(item.parent.parent)
_current_class = item.parent.parent
# this is needed for the class-level, to ensure that the
# teardown runs after the class is completed with its own
# class-level teardown...
item.parent.parent.addfinalizer(lambda: class_teardown(item.parent.parent))
test_setup(item)
示例8: test_failure_in_setup_function_ignores_custom_repr
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_failure_in_setup_function_ignores_custom_repr(self, testdir) -> None:
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
assert 0
"""
)
reports = testdir.runitem(
"""
def setup_function(func):
raise ValueError(42)
def test_func():
pass
"""
)
assert len(reports) == 2
rep = reports[0]
print(rep)
assert not rep.skipped
assert not rep.passed
assert rep.failed
# assert rep.outcome.when == "setup"
# assert rep.outcome.where.lineno == 3
# assert rep.outcome.where.path.basename == "test_func.py"
# assert instanace(rep.failed.failurerepr, PythonFailureRepr)
示例9: pytest_pycollect_makeitem
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_pycollect_makeitem(collector, name, obj):
if inspect.isclass(obj) and plugin_base.want_class(obj):
return pytest.Class(name, parent=collector)
elif inspect.isfunction(obj) and \
isinstance(collector, pytest.Instance) and \
plugin_base.want_method(collector.cls, obj):
return pytest.Function(name, parent=collector)
else:
return []
示例10: pytest_pycollect_makeitem
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def pytest_pycollect_makeitem(collector, name, obj):
if collector.funcnamefilter(name):
item = pytest.Function(name, parent=collector)
if 'run_loop' in item.keywords:
return list(collector._genfunctions(name, obj))
示例11: test_check_equality
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_check_equality(self, testdir: Testdir) -> None:
modcol = testdir.getmodulecol(
"""
def test_pass(): pass
def test_fail(): assert 0
"""
)
fn1 = testdir.collect_by_name(modcol, "test_pass")
assert isinstance(fn1, pytest.Function)
fn2 = testdir.collect_by_name(modcol, "test_pass")
assert isinstance(fn2, pytest.Function)
assert fn1 == fn2
assert fn1 != modcol
assert hash(fn1) == hash(fn2)
fn3 = testdir.collect_by_name(modcol, "test_fail")
assert isinstance(fn3, pytest.Function)
assert not (fn1 == fn3)
assert fn1 != fn3
for fn in fn1, fn2, fn3:
assert isinstance(fn, pytest.Function)
assert fn != 3 # type: ignore[comparison-overlap] # noqa: F821
assert fn != modcol
assert fn != [1, 2, 3] # type: ignore[comparison-overlap] # noqa: F821
assert [1, 2, 3] != fn # type: ignore[comparison-overlap] # noqa: F821
assert modcol != fn
assert testdir.collect_by_name(modcol, "doesnotexist") is None
示例12: test_repr_produces_actual_test_id
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_repr_produces_actual_test_id(self, testdir):
f = self.make_function(
testdir, name=r"test[\xe5]", callobj=self.test_repr_produces_actual_test_id
)
assert repr(f) == r"<Function test[\xe5]>"
示例13: test_check_equality
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_check_equality(self, testdir) -> None:
modcol = testdir.getmodulecol(
"""
def test_pass(): pass
def test_fail(): assert 0
"""
)
fn1 = testdir.collect_by_name(modcol, "test_pass")
assert isinstance(fn1, pytest.Function)
fn2 = testdir.collect_by_name(modcol, "test_pass")
assert isinstance(fn2, pytest.Function)
assert fn1 == fn2
assert fn1 != modcol
assert hash(fn1) == hash(fn2)
fn3 = testdir.collect_by_name(modcol, "test_fail")
assert isinstance(fn3, pytest.Function)
assert not (fn1 == fn3)
assert fn1 != fn3
for fn in fn1, fn2, fn3:
assert fn != 3 # type: ignore[comparison-overlap] # noqa: F821
assert fn != modcol
assert fn != [1, 2, 3] # type: ignore[comparison-overlap] # noqa: F821
assert [1, 2, 3] != fn # type: ignore[comparison-overlap] # noqa: F821
assert modcol != fn
示例14: test_pytest_pycollect_makeitem
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_pytest_pycollect_makeitem(self, testdir):
testdir.makeconftest(
"""
import pytest
class MyFunction(pytest.Function):
pass
def pytest_pycollect_makeitem(collector, name, obj):
if name == "some":
return MyFunction(name, collector)
"""
)
testdir.makepyfile("def some(): pass")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*MyFunction*some*"])
示例15: test_itemreport_reportinfo
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import Function [as 别名]
def test_itemreport_reportinfo(self, testdir):
testdir.makeconftest(
"""
import pytest
class MyFunction(pytest.Function):
def reportinfo(self):
return "ABCDE", 42, "custom"
def pytest_pycollect_makeitem(collector, name, obj):
if name == "test_func":
return MyFunction.from_parent(name=name, parent=collector)
"""
)
item = testdir.getitem("def test_func(): pass")
item.config.pluginmanager.getplugin("runner")
assert item.location == ("ABCDE", 42, "custom")