Esempio n. 1
0
 def test_collect_module_two_doctest_no_modulelevel(
     self,
     pytester: Pytester,
     filename: str,
 ) -> None:
     path = pytester.makepyfile(
         **{
             filename:
             """
         '# Empty'
         def my_func():
             ">>> magic = 42 "
         def unuseful():
             '''
             # This is a function
             # >>> # it doesn't have any doctest
             '''
         def another():
             '''
             # This is another function
             >>> import os # this one does have a doctest
             '''
         """,
         }, )
     for p in (path, pytester.path):
         items, reprec = pytester.inline_genitems(p, "--doctest-modules")
         assert len(items) == 2
         assert isinstance(items[0], DoctestItem)
         assert isinstance(items[1], DoctestItem)
         assert isinstance(items[0].parent, DoctestModule)
         assert items[0].parent is items[1].parent
Esempio n. 2
0
    def test_mark_should_not_pass_to_siebling_class(
            self, pytester: Pytester) -> None:
        """#568"""
        p = pytester.makepyfile("""
            import pytest

            class TestBase(object):
                def test_foo(self):
                    pass

            @pytest.mark.b
            class TestSub(TestBase):
                pass


            class TestOtherSub(TestBase):
                pass

        """)
        items, rec = pytester.inline_genitems(p)
        base_item, sub_item, sub_item_other = items
        print(items, [x.nodeid for x in items])
        # new api segregates
        assert not list(base_item.iter_markers(name="b"))
        assert not list(sub_item_other.iter_markers(name="b"))
        assert list(sub_item.iter_markers(name="b"))
Esempio n. 3
0
    def test_early_ignored_attributes(self, pytester: Pytester) -> None:
        """Builtin attributes should be ignored early on, even if
        configuration would otherwise allow them.

        This tests a performance optimization, not correctness, really,
        although it tests PytestCollectionWarning is not raised, while
        it would have been raised otherwise.
        """
        pytester.makeini(
            """
            [pytest]
            python_classes=*
            python_functions=*
        """
        )
        pytester.makepyfile(
            """
            class TestEmpty:
                pass
            test_empty = TestEmpty()
            def test_real():
                pass
        """
        )
        items, rec = pytester.inline_genitems()
        assert rec.ret == 0
        assert len(items) == 1
Esempio n. 4
0
    def test_mark_closest(self, pytester: Pytester) -> None:
        p = pytester.makepyfile(
            """
            import pytest

            @pytest.mark.c(location="class")
            class Test:
                @pytest.mark.c(location="function")
                def test_has_own(self):
                    pass

                def test_has_inherited(self):
                    pass

        """
        )
        items, rec = pytester.inline_genitems(p)
        has_own, has_inherited = items
        has_own_marker = has_own.get_closest_marker("c")
        has_inherited_marker = has_inherited.get_closest_marker("c")
        assert has_own_marker is not None
        assert has_inherited_marker is not None
        assert has_own_marker.kwargs == {"location": "function"}
        assert has_inherited_marker.kwargs == {"location": "class"}
        assert has_own.get_closest_marker("missing") is None
Esempio n. 5
0
 def test_collect_module_single_modulelevel_doctest(self, pytester: Pytester):
     path = pytester.makepyfile(whatever='""">>> pass"""')
     for p in (path, pytester.path):
         items, reprec = pytester.inline_genitems(p, "--doctest-modules")
         assert len(items) == 1
         assert isinstance(items[0], DoctestItem)
         assert isinstance(items[0].parent, DoctestModule)
Esempio n. 6
0
    def test_collect_testtextfile(self, pytester: Pytester):
        w = pytester.maketxtfile(whatever="")
        checkfile = pytester.maketxtfile(test_something="""
            alskdjalsdk
            >>> i = 5
            >>> i-1
            4
        """)

        for x in (pytester.path, checkfile):
            # print "checking that %s returns custom items" % (x,)
            items, reprec = pytester.inline_genitems(x)
            assert len(items) == 1
            assert isinstance(items[0], DoctestItem)
            assert isinstance(items[0].parent, DoctestTextfile)
        # Empty file has no items.
        items, reprec = pytester.inline_genitems(w)
        assert len(items) == 0
Esempio n. 7
0
 def test_reportinfo(self, pytester: Pytester):
     """Make sure that DoctestItem.reportinfo() returns lineno."""
     p = pytester.makepyfile(test_reportinfo="""
         def foo(x):
             '''
                 >>> foo('a')
                 'b'
             '''
             return 'c'
     """)
     items, reprec = pytester.inline_genitems(p, "--doctest-modules")
     reportinfo = items[0].reportinfo()
     assert reportinfo[1] == 1
Esempio n. 8
0
 def test_collect_module_two_doctest_one_modulelevel(
         self, pytester: Pytester):
     path = pytester.makepyfile(whatever="""
         '>>> x = None'
         def my_func():
             ">>> magic = 42 "
     """)
     for p in (path, pytester.path):
         items, reprec = pytester.inline_genitems(p, "--doctest-modules")
         assert len(items) == 2
         assert isinstance(items[0], DoctestItem)
         assert isinstance(items[1], DoctestItem)
         assert isinstance(items[0].parent, DoctestModule)
         assert items[0].parent is items[1].parent
Esempio n. 9
0
    def test_mark_decorator_subclass_does_not_propagate_to_base(
            self, pytester: Pytester) -> None:
        p = pytester.makepyfile("""
            import pytest

            @pytest.mark.a
            class Base(object): pass

            @pytest.mark.b
            class Test1(Base):
                def test_foo(self): pass

            class Test2(Base):
                def test_bar(self): pass
        """)
        items, rec = pytester.inline_genitems(p)
        self.assert_markers(items, test_foo=("a", "b"), test_bar=("a", ))
Esempio n. 10
0
 def test_merging_markers_deep(self, pytester: Pytester) -> None:
     # issue 199 - propagate markers into nested classes
     p = pytester.makepyfile("""
         import pytest
         class TestA(object):
             pytestmark = pytest.mark.a
             def test_b(self):
                 assert True
             class TestC(object):
                 # this one didn't get marked
                 def test_d(self):
                     assert True
     """)
     items, rec = pytester.inline_genitems(p)
     for item in items:
         print(item, item.keywords)
         assert [x for x in item.iter_markers() if x.name == "a"]
Esempio n. 11
0
    def test_mark_decorator_baseclasses_merged(self, pytester: Pytester) -> None:
        p = pytester.makepyfile(
            """
            import pytest

            @pytest.mark.a
            class Base(object): pass

            @pytest.mark.b
            class Base2(Base): pass

            @pytest.mark.c
            class Test1(Base2):
                def test_foo(self): pass

            class Test2(Base2):
                @pytest.mark.d
                def test_bar(self): pass
        """
        )
        items, rec = pytester.inline_genitems(p)
        self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d"))
Esempio n. 12
0
 def test_collect_module_empty(self, pytester: Pytester):
     path = pytester.makepyfile(whatever="#")
     for p in (path, pytester.path):
         items, reprec = pytester.inline_genitems(p, "--doctest-modules")
         assert len(items) == 0