def pytest_sessionfinish()

in conftest.py [0:0]


def pytest_sessionfinish(session):
    if not hasattr(session.config, "workerinput"):
        import psutil

        reporter = session.config.pluginmanager.get_plugin("terminalreporter")
        # Kill all Firefox processes remaining
        for proc in psutil.process_iter(["name", "pid", "status"]):
            try:
                if (
                    proc.create_time() > reporter._sessionstarttime
                    and proc.name().startswith("firefox")
                ):
                    logging.info(f"found remaining process: {proc.pid}")
                    proc.kill()
            except (ProcessLookupError, psutil.NoSuchProcess):
                logging.warning("Failed to kill process.")
                pass

    # TestRail reporting
    if not os.environ.get("TESTRAIL_REPORT"):
        logging.warning(
            "Not reporting to TestRail. Set env var TESTRAIL_REPORT to activate reporting."
        )
        return None

    if not hasattr(session.config, "_json_report"):
        logging.warning("No json_report in config, will try again with other workers.")
        return None

    report = session.config._json_report.report
    if report is None or report.get("tests") is None:
        logging.warning(
            "Not reporting to TestRail. This thread does not have a report in its config object."
        )
        return None

    creds = get_tc_secret()
    if creds:
        os.environ["TESTRAIL_USERNAME"] = creds.get("TESTRAIL_USERNAME")
        os.environ["TESTRAIL_API_KEY"] = creds.get("TESTRAIL_API_KEY")
        os.environ["TESTRAIL_BASE_URL"] = creds.get("TESTRAIL_BASE_URL")
    elif not os.environ.get("TESTRAIL_USERNAME"):
        logging.error(
            "Attempted to report to TestRail, but could not find credentials."
        )
        raise OSError("Could not find TestRail credentials")

    tr_session = tri.testrail_init()
    passes = tri.collect_changes(tr_session, report)
    tri.mark_results(tr_session, passes)