def test_node_warn_is_no_longer_only_pytest_warnings(pytester: Pytester, warn_type: Type[Warning], msg: str) -> None: items = pytester.getitems(""" def test(): pass """) with pytest.warns(warn_type, match=msg): items[0].warn(warn_type(msg))
def test_function_equality_with_callspec(self, pytester: Pytester) -> None: items = pytester.getitems(""" import pytest @pytest.mark.parametrize('arg', [1,2]) def test_function(arg): pass """) assert items[0] != items[1] assert not (items[0] == items[1])
def test_node_warning_enforces_warning_types(pytester: Pytester) -> None: items = pytester.getitems(""" def test(): pass """) with pytest.raises( ValueError, match="warning must be an instance of Warning or subclass"): items[0].warn(Exception("ok")) # type: ignore[arg-type]
def test_std_warn_not_pytestwarning(pytester: Pytester) -> None: items = pytester.getitems( """ def test(): pass """ ) with pytest.raises(ValueError, match=".*instance of PytestWarning.*"): items[0].warn(UserWarning("some warning")) # type: ignore[arg-type]
def test_parametrize_with_empty_string_arguments( self, pytester: Pytester) -> None: items = pytester.getitems("""\ import pytest @pytest.mark.parametrize('v', ('', ' ')) @pytest.mark.parametrize('w', ('', ' ')) def test(v, w): ... """) names = {item.name for item in items} assert names == {"test[-]", "test[ -]", "test[- ]", "test[ - ]"}
def test_skipif_class(self, pytester: Pytester) -> None: (item, ) = pytester.getitems(""" import pytest class TestClass(object): pytestmark = pytest.mark.skipif("config._hackxyz") def test_func(self): pass """) item.config._hackxyz = 3 # type: ignore[attr-defined] skipped = evaluate_skip_marks(item) assert skipped assert skipped.reason == "condition: config._hackxyz"
def test_testcase_totally_incompatible_exception_info( pytester: Pytester) -> None: import _pytest.unittest (item, ) = pytester.getitems(""" from unittest import TestCase class MyTestCase(TestCase): def test_hello(self): pass """) assert isinstance(item, _pytest.unittest.TestCaseFunction) item.addError(None, 42) # type: ignore[arg-type] excinfo = item._excinfo assert excinfo is not None assert "ERROR: Unknown Incompatible" in str(excinfo.pop(0).getrepr())
def test_parametrize_with_mark(self, pytester: Pytester) -> None: items = pytester.getitems(""" import pytest @pytest.mark.foo @pytest.mark.parametrize('arg', [ 1, pytest.param(2, marks=[pytest.mark.baz, pytest.mark.bar]) ]) def test_function(arg): pass """) keywords = [item.keywords for item in items] assert ("foo" in keywords[0] and "bar" not in keywords[0] and "baz" not in keywords[0]) assert "foo" in keywords[1] and "bar" in keywords[ 1] and "baz" in keywords[1]
def test_function_originalname(self, pytester: Pytester) -> None: items = pytester.getitems(""" import pytest @pytest.mark.parametrize('arg', [1,2]) def test_func(arg): pass def test_no_param(): pass """) originalnames = [] for x in items: assert isinstance(x, pytest.Function) originalnames.append(x.originalname) assert originalnames == [ "test_func", "test_func", "test_no_param", ]
def test_function_instance(pytester: Pytester) -> None: items = pytester.getitems( """ def test_func(): pass class TestIt: def test_method(self): pass @classmethod def test_class(cls): pass @staticmethod def test_static(): pass """ ) assert len(items) == 3 assert isinstance(items[0], Function) assert items[0].name == "test_func" assert items[0].instance is None assert isinstance(items[1], Function) assert items[1].name == "test_method" assert items[1].instance is not None assert items[1].instance.__class__.__name__ == "TestIt" assert isinstance(items[2], Function) assert items[2].name == "test_static" assert items[2].instance is None