Best Python code snippet using pytest
test_hooks.py
Source:test_hooks.py  
...208        terminal_writer_mock: mock.MagicMock,209        getoption_mock: ConfigGetOptionMock,210        test_pytest_bdd_session: Session,211    ) -> None:212        pytest_collection_finish(test_pytest_bdd_session)213        terminal_writer_mock.assert_not_called()214    @pytest.mark.parametrize("getoption_mapping", [{_OptionName.ENABLE_INJECTION.as_variable: True}], indirect=True)215    def test_pytest_collection_finish_admin_factory_injection_enabled_with_not_patched_pytest(216        self,217        terminal_writer_mock: mock.MagicMock,218        getoption_mock: ConfigGetOptionMock,219        test_pytest_bdd_session: Session,220        patched_hook_admin_proxy_manager: IProxyManager,221    ) -> None:222        pytest_collection_finish(test_pytest_bdd_session)223        terminal_writer_mock.assert_called_once()224        assert not patched_hook_admin_proxy_manager.collection_prepared225    @pytest.mark.parametrize("getoption_mapping", [{_OptionName.ENABLE_INJECTION.as_variable: True}], indirect=True)226    def test_pytest_collection_finish_test_execution_factory_injection_enabled_with_not_patched_pytest(227        self,228        terminal_writer_mock: mock.MagicMock,229        getoption_mock: ConfigGetOptionMock,230        test_pytest_bdd_session: Session,231        patched_hook_test_execution_proxy_manager: IProxyManager,232    ) -> None:233        pytest_collection_finish(test_pytest_bdd_session)234        terminal_writer_mock.assert_not_called()235        assert not patched_hook_test_execution_proxy_manager.collection_prepared236    @pytest.mark.parametrize("getoption_mapping", [{_OptionName.ENABLE_INJECTION.as_variable: True}], indirect=True)237    def test_pytest_collection_admin_factory_finish_injection_enabled_with_patched_pytest(238        self,239        terminal_writer_mock: mock.MagicMock,240        getoption_mock: ConfigGetOptionMock,241        test_pytest_bdd_session: Session,242        patched_hook_admin_proxy_manager: IProxyManager,243    ) -> None:244        pytest_configure(test_pytest_bdd_session.config)245        pytest_collection_finish(test_pytest_bdd_session)246        assert terminal_writer_mock.call_count == 2247        assert patched_hook_admin_proxy_manager.collection_prepared248    @pytest.mark.parametrize("getoption_mapping", [{_OptionName.ENABLE_INJECTION.as_variable: True}], indirect=True)249    def test_pytest_collection_finish_test_execution_factory_injection_enabled_with_patched_pytest(250        self,251        terminal_writer_mock: mock.MagicMock,252        getoption_mock: ConfigGetOptionMock,253        test_pytest_bdd_session: Session,254        patched_hook_test_execution_proxy_manager: IProxyManager,255    ) -> None:256        pytest_configure(test_pytest_bdd_session.config)257        pytest_collection_finish(test_pytest_bdd_session)258        assert terminal_writer_mock.call_count == 1259        assert not patched_hook_test_execution_proxy_manager.collection_prepared260    def test_pytest_runtest_setup(self, test_clean_item: Item) -> None:261        with mock.patch(262            "overhave.get_description_manager", return_value=mock.MagicMock()263        ) as mocked_description_manager:264            pytest_runtest_setup(item=test_clean_item)265            mocked_description_manager.assert_not_called()266    @pytest.mark.parametrize("enable_html", [True])267    def test_pytest_runtest_makereport_clean(268        self,269        clear_get_description_manager: None,270        description_handler_mock: mock.MagicMock,271        link_handler_mock: mock.MagicMock,...plugin.py
Source:plugin.py  
...206        item.reconcile_and_print(self._prev_item, self._tw, report.outcome)207        self._prev_item = item208    # This is probably the best function to override. _print_collecteditems() is also a candidate, but209    # I think it's more liable to change because it's a private method.210    def pytest_collection_finish(self, session):211        if self.config.getoption("collectonly"):212            prev_it_item = None213            for item in session.items:214                it_item = ItItem(item)215                it_item.reconcile_and_print(prev_it_item, self._tw, outcome=None)216                prev_it_item = it_item217        # NOTE: this logic is copied from TerminalReporter.pytest_collection_finish218        lines = self.config.hook.pytest_report_collectionfinish(219            config=self.config, startdir=self.startdir, items=session.items220        )221        self._write_report_lines_from_hooks(lines)222        if self.config.getoption("collectonly"):223            if self.stats.get("failed"):224                self._tw.sep("!", "collection failures")...__init__.py
Source:__init__.py  
...110    After tests are collected and before any modification is performed.111    https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_collection_modifyitems112    """113    config._syrupy.collect_items(items)114def pytest_collection_finish(session: Any) -> None:115    """116    After collection has been performed and modified.117    https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_collection_finish118    """119    session.config._syrupy.select_items(session.items)120def pytest_runtest_logfinish(nodeid: str) -> None:121    """122    At the end of running the runtest protocol for a single item.123    https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_logfinish124    """125    global _syrupy126    if _syrupy:127        _syrupy.ran_item(nodeid)128@pytest.hookimpl(tryfirst=True)...discovery_output_plugin.py
Source:discovery_output_plugin.py  
...31                           'message': error.longreprtext})32        except:33            pass34    return errors35def pytest_collection_finish(session):36    print('==DISCOVERED TESTS BEGIN==')37    tests = extract_discovered_tests(session)38    errors = extract_discovery_errors()39    print(json.dumps({'tests': tests,40                      'errors': errors}))41    print('==DISCOVERED TESTS END==')42def pytest_collectreport(report):43    if report.failed:...Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!
