Exemplo n.º 1
0
 def test_lastfailed_xpass(self, pytester: Pytester) -> None:
     pytester.inline_runsource("""
         import pytest
         @pytest.mark.xfail
         def test_hello():
             assert 1
     """)
     config = pytester.parseconfigure()
     assert config.cache is not None
     lastfailed = config.cache.get("cache/lastfailed", -1)
     assert lastfailed == -1
Exemplo n.º 2
0
    def test_exact_teardown_issue1206(self, pytester: Pytester) -> None:
        """Issue shadowing error with wrong number of arguments on teardown_method."""
        rec = pytester.inline_runsource("""
            import pytest

            class TestClass(object):
                def teardown_method(self, x, y, z):
                    pass

                def test_method(self):
                    assert True
        """)
        reps = rec.getreports("pytest_runtest_logreport")
        print(reps)
        assert len(reps) == 3
        #
        assert reps[0].nodeid.endswith("test_method")
        assert reps[0].passed
        assert reps[0].when == "setup"
        #
        assert reps[1].nodeid.endswith("test_method")
        assert reps[1].passed
        assert reps[1].when == "call"
        #
        assert reps[2].nodeid.endswith("test_method")
        assert reps[2].failed
        assert reps[2].when == "teardown"
        longrepr = reps[2].longrepr
        assert isinstance(longrepr, ExceptionChainRepr)
        assert longrepr.reprcrash
        assert longrepr.reprcrash.message in (
            "TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
            # Python >= 3.10
            "TypeError: TestClass.teardown_method() missing 2 required positional arguments: 'y' and 'z'",
        )
Exemplo n.º 3
0
def test_monkeypatch_plugin(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        def test_method(monkeypatch):
            assert monkeypatch.__class__.__name__ == "MonkeyPatch"
    """)
    res = reprec.countoutcomes()
    assert tuple(res) == (1, 0, 0), res
Exemplo n.º 4
0
    def test_reprentries_serialization_196(self, pytester: Pytester) -> None:
        """Regarding issue pytest-xdist#196

        This test came originally from test_remote.py in xdist (ca03269).
        """
        from _pytest._code.code import ReprEntryNative

        reprec = pytester.inline_runsource(
            """
                            def test_repr_entry_native():
                                x = 0
                                assert x
                        """,
            "--tb=native",
        )
        reports = reprec.getreports("pytest_runtest_logreport")
        assert len(reports) == 3
        rep = reports[1]
        assert isinstance(rep.longrepr, ExceptionRepr)
        d = rep._to_json()
        a = TestReport._from_json(d)
        assert isinstance(a.longrepr, ExceptionRepr)

        rep_entries = rep.longrepr.reprtraceback.reprentries
        a_entries = a.longrepr.reprtraceback.reprentries
        for i in range(len(a_entries)):
            assert isinstance(rep_entries[i], ReprEntryNative)
            assert rep_entries[i].lines == a_entries[i].lines
Exemplo n.º 5
0
 def test_itemreport_outcomes(self, pytester: Pytester) -> None:
     # This test came originally from test_remote.py in xdist (ca03269).
     reprec = pytester.inline_runsource(
         """
         import pytest
         def test_pass(): pass
         def test_fail(): 0/0
         @pytest.mark.skipif("True")
         def test_skip(): pass
         def test_skip_imperative():
             pytest.skip("hello")
         @pytest.mark.xfail("True")
         def test_xfail(): 0/0
         def test_xfail_imperative():
             pytest.xfail("hello")
     """
     )
     reports = reprec.getreports("pytest_runtest_logreport")
     assert len(reports) == 17  # with setup/teardown "passed" reports
     for rep in reports:
         d = rep._to_json()
         newrep = TestReport._from_json(d)
         assert newrep.passed == rep.passed
         assert newrep.failed == rep.failed
         assert newrep.skipped == rep.skipped
         if newrep.skipped and not hasattr(newrep, "wasxfail"):
             assert isinstance(newrep.longrepr, tuple)
             assert len(newrep.longrepr) == 3
         assert newrep.outcome == rep.outcome
         assert newrep.when == rep.when
         assert newrep.keywords == rep.keywords
         if rep.failed:
             assert newrep.longreprtext == rep.longreprtext
Exemplo n.º 6
0
def test_module_and_function_setup(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        modlevel = []
        def setup_module(module):
            assert not modlevel
            module.modlevel.append(42)

        def teardown_module(module):
            modlevel.pop()

        def setup_function(function):
            function.answer = 17

        def teardown_function(function):
            del function.answer

        def test_modlevel():
            assert modlevel[0] == 42
            assert test_modlevel.answer == 17

        class TestFromClass(object):
            def test_module(self):
                assert modlevel[0] == 42
                assert not hasattr(test_modlevel, 'answer')
    """)
    rep = reprec.matchreport("test_modlevel")
    assert rep.passed
    rep = reprec.matchreport("test_module")
    assert rep.passed
Exemplo n.º 7
0
 def test_order_of_execution(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource(
         """
         values = []
         def test_1():
             values.append(1)
         def test_2():
             values.append(2)
         def test_3():
             assert values == [1,2]
         class Testmygroup(object):
             reslist = values
             def test_1(self):
                 self.reslist.append(1)
             def test_2(self):
                 self.reslist.append(2)
             def test_3(self):
                 self.reslist.append(3)
             def test_4(self):
                 assert self.reslist == [1,2,1,2,3]
     """
     )
     passed, skipped, failed = reprec.countoutcomes()
     assert failed == skipped == 0
     assert passed == 7
Exemplo n.º 8
0
 def test_teardown_final_returncode(self, pytester: Pytester) -> None:
     rec = pytester.inline_runsource("""
         def test_func():
             pass
         def teardown_function(func):
             raise ValueError(42)
     """)
     assert rec.ret == 1
Exemplo n.º 9
0
 def test_raises_output(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource("""
         import pytest
         def test_raises_doesnt():
             pytest.raises(ValueError, int, "3")
     """)
     passed, skipped, failed = reprec.listoutcomes()
     assert len(failed) == 1
     out = failed[0].longrepr.reprcrash.message  # type: ignore[union-attr]
     assert "DID NOT RAISE" in out
Exemplo n.º 10
0
 def test_collectreport_passed(self, pytester: Pytester) -> None:
     """This test came originally from test_remote.py in xdist (ca03269)."""
     reprec = pytester.inline_runsource("def test_func(): pass")
     reports = reprec.getreports("pytest_collectreport")
     for rep in reports:
         d = rep._to_json()
         newrep = CollectReport._from_json(d)
         assert newrep.passed == rep.passed
         assert newrep.failed == rep.failed
         assert newrep.skipped == rep.skipped
Exemplo n.º 11
0
 def test_mark_with_wrong_marker(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource("""
             import pytest
             class pytestmark(object):
                 pass
             def test_func():
                 pass
     """)
     values = reprec.getfailedcollections()
     assert len(values) == 1
     assert "TypeError" in str(values[0].longrepr)
Exemplo n.º 12
0
 def test_exit_first_problem(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource(
         """
         def test_one(): assert 0
         def test_two(): assert 0
     """,
         "--exitfirst",
     )
     passed, skipped, failed = reprec.countoutcomes()
     assert failed == 1
     assert passed == skipped == 0
Exemplo n.º 13
0
def test_method_setup_uses_fresh_instances(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        class TestSelfState1(object):
            memory = []
            def test_hello(self):
                self.memory.append(self)

            def test_afterhello(self):
                assert self != self.memory[0]
    """)
    reprec.assertoutcome(passed=2, failed=0)
Exemplo n.º 14
0
 def test_maxfail(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource(
         """
         def test_one(): assert 0
         def test_two(): assert 0
         def test_three(): assert 0
     """,
         "--maxfail=2",
     )
     passed, skipped, failed = reprec.countoutcomes()
     assert failed == 2
     assert passed == skipped == 0
Exemplo n.º 15
0
 def test_collectreport_fail(self, pytester: Pytester) -> None:
     """This test came originally from test_remote.py in xdist (ca03269)."""
     reprec = pytester.inline_runsource("qwe abc")
     reports = reprec.getreports("pytest_collectreport")
     assert reports
     for rep in reports:
         d = rep._to_json()
         newrep = CollectReport._from_json(d)
         assert newrep.passed == rep.passed
         assert newrep.failed == rep.failed
         assert newrep.skipped == rep.skipped
         if rep.failed:
             assert newrep.longrepr == str(rep.longrepr)
Exemplo n.º 16
0
def test_method_setup(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        class TestSetupMethod(object):
            def setup_method(self, meth):
                self.methsetup = meth
            def teardown_method(self, meth):
                del self.methsetup

            def test_some(self):
                assert self.methsetup == self.test_some

            def test_other(self):
                assert self.methsetup == self.test_other
    """)
    reprec.assertoutcome(passed=2)
Exemplo n.º 17
0
def test_setup_function_failure_no_teardown(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        modlevel = []
        def setup_function(function):
            modlevel.append(1)
            0/0

        def teardown_function(module):
            modlevel.append(2)

        def test_func():
            pass
    """)
    calls = reprec.getcalls("pytest_runtest_setup")
    assert calls[0].item.module.modlevel == [1]
Exemplo n.º 18
0
 def test_extended_report_deserialization(self, pytester: Pytester) -> None:
     """This test came originally from test_remote.py in xdist (ca03269)."""
     reprec = pytester.inline_runsource("qwe abc")
     reports = reprec.getreports("pytest_collectreport")
     assert reports
     for rep in reports:
         rep.extra = True  # type: ignore[attr-defined]
         d = rep._to_json()
         newrep = CollectReport._from_json(d)
         assert newrep.extra
         assert newrep.passed == rep.passed
         assert newrep.failed == rep.failed
         assert newrep.skipped == rep.skipped
         if rep.failed:
             assert newrep.longrepr == str(rep.longrepr)
Exemplo n.º 19
0
 def test_logstart_logfinish_hooks(self, pytester: Pytester) -> None:
     rec = pytester.inline_runsource("""
         import pytest
         def test_func():
             pass
     """)
     reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish")
     assert [x._name for x in reps] == [
         "pytest_runtest_logstart",
         "pytest_runtest_logfinish",
     ]
     for rep in reps:
         assert rep.nodeid == "test_logstart_logfinish_hooks.py::test_func"
         assert rep.location == ("test_logstart_logfinish_hooks.py", 1,
                                 "test_func")
Exemplo n.º 20
0
def test_module_setup_failure_no_teardown(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        values = []
        def setup_module(module):
            values.append(1)
            0/0

        def test_nothing():
            pass

        def teardown_module(module):
            values.append(2)
    """)
    reprec.assertoutcome(failed=1)
    calls = reprec.getcalls("pytest_runtest_setup")
    assert calls[0].item.module.values == [1]
Exemplo n.º 21
0
def test_class_setup_failure_no_teardown(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        class TestSimpleClassSetup(object):
            clslevel = []
            def setup_class(cls):
                0/0

            def teardown_class(cls):
                cls.clslevel.append(1)

            def test_classlevel(self):
                pass

        def test_cleanup():
            assert not TestSimpleClassSetup.clslevel
    """)
    reprec.assertoutcome(failed=1, passed=1)
Exemplo n.º 22
0
    def test_reprentries_serialization_170(self, pytester: Pytester) -> None:
        """Regarding issue pytest-xdist#170

        This test came originally from test_remote.py in xdist (ca03269).
        """
        from _pytest._code.code import ReprEntry

        reprec = pytester.inline_runsource(
            """
                            def test_repr_entry():
                                x = 0
                                assert x
                        """,
            "--showlocals",
        )
        reports = reprec.getreports("pytest_runtest_logreport")
        assert len(reports) == 3
        rep = reports[1]
        assert isinstance(rep.longrepr, ExceptionRepr)
        d = rep._to_json()
        a = TestReport._from_json(d)
        assert isinstance(a.longrepr, ExceptionRepr)

        rep_entries = rep.longrepr.reprtraceback.reprentries
        a_entries = a.longrepr.reprtraceback.reprentries
        for i in range(len(a_entries)):
            rep_entry = rep_entries[i]
            assert isinstance(rep_entry, ReprEntry)
            assert rep_entry.reprfileloc is not None
            assert rep_entry.reprfuncargs is not None
            assert rep_entry.reprlocals is not None

            a_entry = a_entries[i]
            assert isinstance(a_entry, ReprEntry)
            assert a_entry.reprfileloc is not None
            assert a_entry.reprfuncargs is not None
            assert a_entry.reprlocals is not None

            assert rep_entry.lines == a_entry.lines
            assert rep_entry.reprfileloc.lineno == a_entry.reprfileloc.lineno
            assert rep_entry.reprfileloc.message == a_entry.reprfileloc.message
            assert rep_entry.reprfileloc.path == a_entry.reprfileloc.path
            assert rep_entry.reprfuncargs.args == a_entry.reprfuncargs.args
            assert rep_entry.reprlocals.lines == a_entry.reprlocals.lines
            assert rep_entry.style == a_entry.style
Exemplo n.º 23
0
def test_method_setup_failure_no_teardown(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        class TestMethodSetup(object):
            clslevel = []
            def setup_method(self, method):
                self.clslevel.append(1)
                0/0

            def teardown_method(self, method):
                self.clslevel.append(2)

            def test_method(self):
                pass

        def test_cleanup():
            assert TestMethodSetup.clslevel == [1]
    """)
    reprec.assertoutcome(failed=1, passed=1)
Exemplo n.º 24
0
def test_class_setup(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource("""
        class TestSimpleClassSetup(object):
            clslevel = []
            def setup_class(cls):
                cls.clslevel.append(23)

            def teardown_class(cls):
                cls.clslevel.pop()

            def test_classlevel(self):
                assert self.clslevel[0] == 23

        class TestInheritedClassSetupStillWorks(TestSimpleClassSetup):
            def test_classlevel_anothertime(self):
                assert self.clslevel == [23]

        def test_cleanup():
            assert not TestSimpleClassSetup.clslevel
            assert not TestInheritedClassSetupStillWorks.clslevel
    """)
    reprec.assertoutcome(passed=1 + 2 + 1)
Exemplo n.º 25
0
    def test_xdist_report_longrepr_reprcrash_130(self, pytester: Pytester) -> None:
        """Regarding issue pytest-xdist#130

        This test came originally from test_remote.py in xdist (ca03269).
        """
        reprec = pytester.inline_runsource(
            """
                    def test_fail():
                        assert False, 'Expected Message'
                """
        )
        reports = reprec.getreports("pytest_runtest_logreport")
        assert len(reports) == 3
        rep = reports[1]
        added_section = ("Failure Metadata", "metadata metadata", "*")
        assert isinstance(rep.longrepr, ExceptionRepr)
        rep.longrepr.sections.append(added_section)
        d = rep._to_json()
        a = TestReport._from_json(d)
        assert isinstance(a.longrepr, ExceptionRepr)
        # Check assembled == rep
        assert a.__dict__.keys() == rep.__dict__.keys()
        for key in rep.__dict__.keys():
            if key != "longrepr":
                assert getattr(a, key) == getattr(rep, key)
        assert rep.longrepr.reprcrash is not None
        assert a.longrepr.reprcrash is not None
        assert rep.longrepr.reprcrash.lineno == a.longrepr.reprcrash.lineno
        assert rep.longrepr.reprcrash.message == a.longrepr.reprcrash.message
        assert rep.longrepr.reprcrash.path == a.longrepr.reprcrash.path
        assert rep.longrepr.reprtraceback.entrysep == a.longrepr.reprtraceback.entrysep
        assert (
            rep.longrepr.reprtraceback.extraline == a.longrepr.reprtraceback.extraline
        )
        assert rep.longrepr.reprtraceback.style == a.longrepr.reprtraceback.style
        assert rep.longrepr.sections == a.longrepr.sections
        # Missing section attribute PR171
        assert added_section in a.longrepr.sections
Exemplo n.º 26
0
    def test_exact_teardown_issue90(self, pytester: Pytester) -> None:
        rec = pytester.inline_runsource("""
            import pytest

            class TestClass(object):
                def test_method(self):
                    pass
                def teardown_class(cls):
                    raise Exception()

            def test_func():
                import sys
                # on python2 exc_info is keept till a function exits
                # so we would end up calling test functions while
                # sys.exc_info would return the indexerror
                # from guessing the lastitem
                excinfo = sys.exc_info()
                import traceback
                assert excinfo[0] is None, \
                       traceback.format_exception(*excinfo)
            def teardown_function(func):
                raise ValueError(42)
        """)
        reps = rec.getreports("pytest_runtest_logreport")
        print(reps)
        for i in range(2):
            assert reps[i].nodeid.endswith("test_method")
            assert reps[i].passed
        assert reps[2].when == "teardown"
        assert reps[2].failed
        assert len(reps) == 6
        for i in range(3, 5):
            assert reps[i].nodeid.endswith("test_func")
            assert reps[i].passed
        assert reps[5].when == "teardown"
        assert reps[5].nodeid.endswith("test_func")
        assert reps[5].failed
Exemplo n.º 27
0
 def test_syntax_error_module(self, pytester: Pytester) -> None:
     reprec = pytester.inline_runsource("this is really not python")
     values = reprec.getfailedcollections()
     assert len(values) == 1
     out = str(values[0].longrepr)
     assert out.find("not python") != -1