Skip to content

pytest_collection_finish_hook

This module contains the PytestCollectionFinish class which is used to handle the collection finish hook in Pytest.

PytestCollectionFinish

A class that handles the collection finish hook in Pytest.

Attributes:

Name Type Description
session_store SessionStore

The session store object.

logger Logger

The logger object.

file_handler_obj FileHandler

The file handler object.

session Session

The session object.

Methods:

Name Description
__init_collection_finish

Initializes the collection finish hook.

collection_finish_hook

The collection finish hook method.

Source code in libs\cafex_core\src\cafex_core\utils\hooks_\pytest_collection_finish_hook.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class PytestCollectionFinish:
    """A class that handles the collection finish hook in Pytest.

    Attributes:
        session_store (SessionStore): The session store object.
        logger (Logger): The logger object.
        file_handler_obj (FileHandler): The file handler object.
        session (Session): The session object.

    Methods:
        __init_collection_finish: Initializes the collection finish hook.
        collection_finish_hook: The collection finish hook method.
    """

    def __init__(self, session):
        self.date_time_util = DateTimeActions()
        self.collection_start_time = self.date_time_util.get_current_date_time()
        self.logger = CoreLogger(name=__name__).get_logger()
        self.file_handler_obj = None
        self.session = session
        self.session_store = SessionStore()
        self.request_store = RequestSingleton()
        self.hook_util = HookUtil()
        self.__init_collection_finish()

    def __init_collection_finish(self):
        """Initializes the collection finish hook by setting up the session
        store, logger, and file handler."""
        self.file_handler_obj = FileHandler()

    def collection_finish_hook(self):
        """The collection finish hook method that is called at the end of the
        test collection.

        It logs the collection finish event, retrieves the worker ID,
        gathers scenario details, creates a JSON file with collection
        details, and logs the collection details.
        """
        if self.session_store.worker_id in ["master", "gw0"]:
            self.logger.info(f"Worker ID : {self.session_store.worker_id}")
            test_details = [
                ItemAttributeAccessor(item_).get_properties() for item_ in self.session.items
            ]

            # Count different test types
            test_type_counts = Counter(test["testType"] for test in test_details)

            # Collect all unique tags
            all_tags = [tag for test in test_details for tag in test["tags"]]
            tag_statistics = dict(Counter(all_tags))

            collection_end_time = self.date_time_util.get_current_date_time()
            collection_duration_seconds = self.date_time_util.get_time_difference_seconds(
                collection_end_time, self.collection_start_time
            )

            collection_details = {
                "testCount": len(self.session.items),
                "pytestCount": test_type_counts.get("pytest", 0),
                "pytestBddCount": test_type_counts.get("pytestBdd", 0),
                "unittestCount": test_type_counts.get("unittest", 0),
                "collectionStartTime": self.collection_start_time,
                "collectionEndTime": collection_end_time,
                "collectionDurationSeconds": collection_duration_seconds,
                "collectionDuration": self.date_time_util.seconds_to_human_readable(
                    collection_duration_seconds
                ),
                "uniqueTags": list(set(all_tags)),
                "tagStatistics": tag_statistics,
                "testDetails": test_details,
            }
            self.file_handler_obj.create_json_file(
                self.session_store.temp_execution_dir, "collection.json", collection_details
            )

__init_collection_finish()

Initializes the collection finish hook by setting up the session store, logger, and file handler.

Source code in libs\cafex_core\src\cafex_core\utils\hooks_\pytest_collection_finish_hook.py
41
42
43
44
def __init_collection_finish(self):
    """Initializes the collection finish hook by setting up the session
    store, logger, and file handler."""
    self.file_handler_obj = FileHandler()

collection_finish_hook()

The collection finish hook method that is called at the end of the test collection.

It logs the collection finish event, retrieves the worker ID, gathers scenario details, creates a JSON file with collection details, and logs the collection details.

Source code in libs\cafex_core\src\cafex_core\utils\hooks_\pytest_collection_finish_hook.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def collection_finish_hook(self):
    """The collection finish hook method that is called at the end of the
    test collection.

    It logs the collection finish event, retrieves the worker ID,
    gathers scenario details, creates a JSON file with collection
    details, and logs the collection details.
    """
    if self.session_store.worker_id in ["master", "gw0"]:
        self.logger.info(f"Worker ID : {self.session_store.worker_id}")
        test_details = [
            ItemAttributeAccessor(item_).get_properties() for item_ in self.session.items
        ]

        # Count different test types
        test_type_counts = Counter(test["testType"] for test in test_details)

        # Collect all unique tags
        all_tags = [tag for test in test_details for tag in test["tags"]]
        tag_statistics = dict(Counter(all_tags))

        collection_end_time = self.date_time_util.get_current_date_time()
        collection_duration_seconds = self.date_time_util.get_time_difference_seconds(
            collection_end_time, self.collection_start_time
        )

        collection_details = {
            "testCount": len(self.session.items),
            "pytestCount": test_type_counts.get("pytest", 0),
            "pytestBddCount": test_type_counts.get("pytestBdd", 0),
            "unittestCount": test_type_counts.get("unittest", 0),
            "collectionStartTime": self.collection_start_time,
            "collectionEndTime": collection_end_time,
            "collectionDurationSeconds": collection_duration_seconds,
            "collectionDuration": self.date_time_util.seconds_to_human_readable(
                collection_duration_seconds
            ),
            "uniqueTags": list(set(all_tags)),
            "tagStatistics": tag_statistics,
            "testDetails": test_details,
        }
        self.file_handler_obj.create_json_file(
            self.session_store.temp_execution_dir, "collection.json", collection_details
        )