Exemplo n.º 1
0
 def test_class_subclassobject(self, pytester: Pytester) -> None:
     pytester.getmodulecol("""
         class test(object):
             pass
     """)
     result = pytester.runpytest()
     result.stdout.fnmatch_lines(["*collected 0*"])
Exemplo n.º 2
0
 def test_autouse_fixture(self, pytester: Pytester,
                          recwarn) -> None:  # rough jstests usage
     pytester.makeconftest("""
         import pytest
         def pytest_pycollect_makeitem(collector, name, obj):
             if name == "MyClass":
                 return MyCollector.from_parent(collector, name=name)
         class MyCollector(pytest.Collector):
             def reportinfo(self):
                 return self.fspath, 3, "xyz"
     """)
     modcol = pytester.getmodulecol("""
         import pytest
         @pytest.fixture(autouse=True)
         def hello():
             pass
         @pytest.fixture
         def arg1(request):
             return 42
         class MyClass(object):
             pass
     """)
     # this hook finds funcarg factories
     rep = runner.collect_one_node(modcol)
     # TODO: Don't treat as Any.
     clscol: Any = rep.result[0]
     clscol.obj = lambda: None
     clscol.funcargs = {}
     pytest._fillfuncargs(clscol)
     assert not clscol.funcargs
Exemplo n.º 3
0
    def test_reportinfo_with_nasty_getattr(self, pytester: Pytester) -> None:
        # https://github.com/pytest-dev/pytest/issues/1204
        modcol = pytester.getmodulecol(
            """
            # lineno 0
            class TestClass:
                def __getattr__(self, name):
                    return "this is not an int"

                def __class_getattr__(cls, name):
                    return "this is not an int"

                def intest_foo(self):
                    pass

                def test_bar(self):
                    pass
        """
        )
        classcol = pytester.collect_by_name(modcol, "TestClass")
        assert isinstance(classcol, Class)
        path, lineno, msg = classcol.reportinfo()
        func = list(classcol.collect())[0]
        assert isinstance(func, Function)
        path, lineno, msg = func.reportinfo()
Exemplo n.º 4
0
    def test_check_equality(self, pytester: Pytester) -> None:
        modcol = pytester.getmodulecol(
            """
            def test_pass(): pass
            def test_fail(): assert 0
        """
        )
        fn1 = pytester.collect_by_name(modcol, "test_pass")
        assert isinstance(fn1, pytest.Function)
        fn2 = pytester.collect_by_name(modcol, "test_pass")
        assert isinstance(fn2, pytest.Function)

        assert fn1 == fn2
        assert fn1 != modcol
        assert hash(fn1) == hash(fn2)

        fn3 = pytester.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]
            assert fn != modcol
            assert fn != [1, 2, 3]  # type: ignore[comparison-overlap]
            assert [1, 2, 3] != fn  # type: ignore[comparison-overlap]
            assert modcol != fn
Exemplo n.º 5
0
def test_make_hook_recorder(pytester: Pytester) -> None:
    item = pytester.getitem("def test_func(): pass")
    recorder = pytester.make_hook_recorder(item.config.pluginmanager)
    assert not recorder.getfailures()

    # (The silly condition is to fool mypy that the code below this is reachable)
    if 1 + 1 == 2:
        pytest.xfail("internal reportrecorder tests need refactoring")

    class rep:
        excinfo = None
        passed = False
        failed = True
        skipped = False
        when = "call"

    recorder.hook.pytest_runtest_logreport(
        report=rep)  # type: ignore[attr-defined]
    failures = recorder.getfailures()
    assert failures == [rep]  # type: ignore[comparison-overlap]
    failures = recorder.getfailures()
    assert failures == [rep]  # type: ignore[comparison-overlap]

    class rep2:
        excinfo = None
        passed = False
        failed = False
        skipped = True
        when = "call"

    rep2.passed = False
    rep2.skipped = True
    recorder.hook.pytest_runtest_logreport(
        report=rep2)  # type: ignore[attr-defined]

    modcol = pytester.getmodulecol("")
    rep3 = modcol.config.hook.pytest_make_collect_report(collector=modcol)
    rep3.passed = False
    rep3.failed = True
    rep3.skipped = False
    recorder.hook.pytest_collectreport(
        report=rep3)  # type: ignore[attr-defined]

    passed, skipped, failed = recorder.listoutcomes()
    assert not passed and skipped and failed

    numpassed, numskipped, numfailed = recorder.countoutcomes()
    assert numpassed == 0
    assert numskipped == 1
    assert numfailed == 1
    assert len(recorder.getfailedcollections()) == 1

    recorder.unregister()  # type: ignore[attr-defined]
    recorder.clear()
    recorder.hook.pytest_runtest_logreport(
        report=rep3)  # type: ignore[attr-defined]
    pytest.raises(ValueError, recorder.getfailures)
Exemplo n.º 6
0
 def test_issue1035_obj_has_getattr(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("""
         class Chameleon(object):
             def __getattr__(self, name):
                 return True
         chameleon = Chameleon()
     """)
     colitems = modcol.collect()
     assert len(colitems) == 0
Exemplo n.º 7
0
    def test_static_method(self, pytester: Pytester) -> None:
        """Support for collecting staticmethod tests (#2528, #2699)"""
        pytester.getmodulecol("""
            import pytest
            class Test(object):
                @staticmethod
                def test_something():
                    pass

                @pytest.fixture
                def fix(self):
                    return 1

                @staticmethod
                def test_fix(fix):
                    assert fix == 1
        """)
        result = pytester.runpytest()
        result.stdout.fnmatch_lines(["*collected 2 items*", "*2 passed in*"])
Exemplo n.º 8
0
def test_node_ctor_fspath_argument_is_deprecated(pytester: Pytester) -> None:
    mod = pytester.getmodulecol("")

    with pytest.warns(
        pytest.PytestDeprecationWarning,
        match=re.escape("The (fspath: py.path.local) argument to File is deprecated."),
    ):
        pytest.File.from_parent(
            parent=mod.parent,
            fspath=legacy_path("bla"),
        )
Exemplo n.º 9
0
 def test_class_reportinfo(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("""
         # lineno 0
         class TestClass(object):
             def test_hello(self): pass
     """)
     classcol = pytester.collect_by_name(modcol, "TestClass")
     assert isinstance(classcol, Class)
     fspath, lineno, msg = classcol.reportinfo()
     assert str(fspath) == str(modcol.path)
     assert lineno == 1
     assert msg == "TestClass"
Exemplo n.º 10
0
 def test_multiple_parametrize(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("""
         import pytest
         @pytest.mark.parametrize('x', [0, 1])
         @pytest.mark.parametrize('y', [2, 3])
         def test1(x, y):
             pass
     """)
     colitems = modcol.collect()
     assert colitems[0].name == "test1[2-0]"
     assert colitems[1].name == "test1[2-1]"
     assert colitems[2].name == "test1[3-0]"
     assert colitems[3].name == "test1[3-1]"
Exemplo n.º 11
0
 def test_class_reportinfo(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("""
         # lineno 0
         class TestClass(object):
             def test_hello(self): pass
     """)
     classcol = pytester.collect_by_name(modcol, "TestClass")
     assert isinstance(classcol, Class)
     fspath, lineno, msg = classcol.reportinfo()
     with pytest.warns(DeprecationWarning):
         assert fspath == modcol.fspath
     assert lineno == 1
     assert msg == "TestClass"
Exemplo n.º 12
0
    def test_reportinfo_with_nasty_getattr(self, pytester: Pytester) -> None:
        # https://github.com/pytest-dev/pytest/issues/1204
        modcol = pytester.getmodulecol("""
            # lineno 0
            class TestClass(object):
                def __getattr__(self, name):
                    return "this is not an int"

                def intest_foo(self):
                    pass
        """)
        classcol = pytester.collect_by_name(modcol, "TestClass")
        assert isinstance(classcol, Class)
        instance = list(classcol.collect())[0]
        assert isinstance(instance, Instance)
        fspath, lineno, msg = instance.reportinfo()
Exemplo n.º 13
0
 def test_issue751_multiple_parametrize_with_ids(
         self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("""
         import pytest
         @pytest.mark.parametrize('x', [0], ids=['c'])
         @pytest.mark.parametrize('y', [0, 1], ids=['a', 'b'])
         class Test(object):
             def test1(self, x, y):
                 pass
             def test2(self, x, y):
                 pass
     """)
     colitems = modcol.collect()[0].collect()[0].collect()
     assert colitems[0].name == "test1[a-c]"
     assert colitems[1].name == "test1[b-c]"
     assert colitems[2].name == "test2[a-c]"
     assert colitems[3].name == "test2[b-c]"
Exemplo n.º 14
0
 def test_collect_result(self, pytester: Pytester) -> None:
     col = pytester.getmodulecol("""
         def test_func1():
             pass
         class TestClass(object):
             pass
     """)
     rep = runner.collect_one_node(col)
     assert not rep.failed
     assert not rep.skipped
     assert rep.passed
     locinfo = rep.location
     assert locinfo[0] == col.fspath.basename
     assert not locinfo[1]
     assert locinfo[2] == col.fspath.basename
     res = rep.result
     assert len(res) == 2
     assert res[0].name == "test_func1"
     assert res[1].name == "TestClass"
Exemplo n.º 15
0
    def test_allow_sane_sorting_for_decorators(self,
                                               pytester: Pytester) -> None:
        modcol = pytester.getmodulecol("""
            def dec(f):
                g = lambda: f(2)
                g.place_as = f
                return g


            def test_b(y):
                pass
            test_b = dec(test_b)

            def test_a(y):
                pass
            test_a = dec(test_a)
        """)
        colitems = modcol.collect()
        assert len(colitems) == 2
        assert [item.name for item in colitems] == ["test_b", "test_a"]
Exemplo n.º 16
0
def test_fscollector_gethookproxy_isinitpath(pytester: Pytester) -> None:
    module = pytester.getmodulecol(
        """
        def test_foo(): pass
        """,
        withinit=True,
    )
    assert isinstance(module, pytest.Module)
    package = module.parent
    assert isinstance(package, pytest.Package)

    with pytest.warns(pytest.PytestDeprecationWarning, match="gethookproxy"):
        package.gethookproxy(pytester.path)

    with pytest.warns(pytest.PytestDeprecationWarning, match="isinitpath"):
        package.isinitpath(pytester.path)

    # The methods on Session are *not* deprecated.
    session = module.session
    with warnings.catch_warnings(record=True) as rec:
        session.gethookproxy(pytester.path)
        session.isinitpath(pytester.path)
    assert len(rec) == 0
Exemplo n.º 17
0
 def test_failing_import(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("import alksdjalskdjalkjals")
     pytest.raises(Collector.CollectError, modcol.collect)
Exemplo n.º 18
0
def test_modulecol_roundtrip(pytester: Pytester) -> None:
    modcol = pytester.getmodulecol("pass", withinit=False)
    trail = modcol.nodeid
    newcol = modcol.session.perform_collect([trail], genitems=0)[0]
    assert modcol.name == newcol.name
Exemplo n.º 19
0
 def test_module_considers_pluginmanager_at_import(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("pytest_plugins='xasdlkj',")
     pytest.raises(ImportError, lambda: modcol.obj)
Exemplo n.º 20
0
 def test_syntax_error_in_module(self, pytester: Pytester) -> None:
     modcol = pytester.getmodulecol("this is a syntax error")
     pytest.raises(modcol.CollectError, modcol.collect)
     pytest.raises(modcol.CollectError, modcol.collect)