Skip to content

Converter

QaseConverter

Class for converting data from pytest to qase.

Source code in pytest_qaseio/converter.py
 12
 13
 14
 15
 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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
class QaseConverter:
    """Class for converting data from pytest to qase."""

    def __init__(
        self,
        browser: str,
        env: str,
        project_code: str,
        file_storage: storage.FileStorage | None,
        config: pytest.Config,
    ) -> None:
        """Init converter."""
        super().__init__()
        self._logger = logging.getLogger("qase")
        self._logger.addHandler(
            logging.StreamHandler(sys.stderr),
        )
        self._env = env
        self._browser = browser
        self._project_code = project_code
        self._file_storage = file_storage
        self._config = config

    def prepare_run_data(
        self,
        cases_ids_from_api: list[int],
        items: list[pytest.Function],
    ) -> tuple[RunCreate, dict[str, int | None]]:
        """Prepare data needed to create test run."""
        cases, tests = self._prepare_cases_for_run(
            cases_ids_from_api=cases_ids_from_api,
            items=items,
        )
        title = self._config.hook.pytest_get_run_name(
            config=self._config,
            env=self._env,
            browser=self._browser,
        )
        run_data = RunCreate(
            title=title,
            cases=cases,
        )

        return run_data, tests

    def prepare_report_data(
        self,
        case_id: int,
        run_id: int,
        item: pytest.Function,
        report: pytest.TestReport,
    ) -> ResultCreate:
        """Create a test result based on results from pytest."""
        if hasattr(report, "wasxfail"):
            return self._prepare_xfailed_test_report(
                case_id=case_id,
                report=report,
            )
        match report.outcome:
            case "passed":
                return self._prepare_passed_test_report(
                    case_id=case_id,
                    report=report,
                )
            case "skipped":
                return self._prepare_skipped_test_report(
                    case_id=case_id,
                    report=report,
                )
            case "failed":
                return self._prepare_failed_test_report(
                    case_id=case_id,
                    run_id=run_id,
                    item=item,
                    report=report,
                )
        raise ValueError("Failed to convert test result!")

    def _prepare_passed_test_report(
        self,
        case_id: int,
        report: pytest.TestReport,
    ) -> ResultCreate:
        """Prepare result report for passed test."""
        return ResultCreate(
            case_id=case_id,
            status="passed",
            comment=constants.TEST_PASSED,
            time_ms=int(report.duration * 1000),
        )

    def _prepare_skipped_test_report(
        self,
        case_id: int,
        report: pytest.TestReport,
    ) -> ResultCreate:
        """Prepare result report for skipped test."""
        *_, skip_reason = report.longrepr  # type: ignore
        return ResultCreate(
            case_id=case_id,
            status="skipped",
            comment=skip_reason,
            time_ms=int(report.duration * 1000),
        )

    def _prepare_xfailed_test_report(
        self,
        case_id: int,
        report: pytest.TestReport,
    ) -> ResultCreate:
        """Prepare result report for xfailed test.

        We use the `blocked` status so as not to mislead the QA team. If they
        see a `failed` case, they will go to retest it. But `xfail` implies
        that the case fails for some already known reason. So the `blocked`
        status will let them know that the case is blocked for some reason
        described in the `xfail` comment (this comment will be duplicated as
        a result of the case run).

        """
        return ResultCreate(
            case_id=case_id,
            status="blocked",
            comment=report.wasxfail,
            time_ms=int(report.duration * 1000),
        )

    def _prepare_failed_test_report(
        self,
        case_id: int,
        run_id: int,
        item: pytest.Function,
        report: pytest.TestReport,
    ) -> ResultCreate:
        """Prepare result report for failed test."""
        comment = constants.TEST_FAILED.format(when=report.when)
        debug_information = self._config.hook.pytest_get_debug_info(item=item)
        if debug_information and self._file_storage:
            folder = constants.REPORT_FOLDER_TEMPLATE.format(
                env=self._env,
                id=run_id,
                browser=self._browser,
                test_name=item.name,
            )
            debug_comment = debug_information.generate_debug_comment(
                file_storage=self._file_storage,
                folder=folder,
            )
            comment += f"\n{debug_comment}"

        return ResultCreate(
            case_id=case_id,
            status="failed",
            comment=comment,
            time_ms=int(report.duration * 1000),
            stacktrace=report.longreprtext,
        )

    def _prepare_cases_for_run(
        self,
        cases_ids_from_api: list[int],
        items: list[pytest.Function],
    ) -> tuple[list[int], dict[str, int | None]]:
        """Collect test cases from test markers.

        Raise InvalidCaseId in case if incorrect case id was provided.
        Raise DuplicatingCaseId in case if duplicated cased ids were provided.

        """
        cases_ids = []

        case_id_invalid = False
        # list of parsed markers to track duplicating case IDs
        # in different tests
        parsed_markers_ids = []
        # Mapping of pytest items ids and case id
        tests: dict[str, int | None] = {}
        for item in items:
            case_id = self._extract_case_id_from_test(
                project_code=self._project_code,
                item=item,
            )
            tests[item.nodeid] = case_id
            qase_marker = self._extract_qase_marker(item)
            qase_marker_id = id(qase_marker)
            if qase_marker_id in parsed_markers_ids:
                continue
            parsed_markers_ids.append(qase_marker_id)

            if not case_id:
                self._logger.error(
                    f"No case id not found for {item.name}! "
                    f"Please add case id for {item.location[0]}:1",
                )
                case_id_invalid = True
                continue
            if case_id not in cases_ids_from_api:
                self._logger.error(
                    f"Case with ID {case_id} not found for {item.name}! "
                    f"Please check case id for {item.location[0]}",
                )
                case_id_invalid = True
                continue
            cases_ids.append(case_id)

        duplicating_ids = [
            case_id
            for case_id, counter in collections.Counter(cases_ids).items()
            if counter > 1
        ]
        if duplicating_ids:
            raise plugin_exceptions.DuplicatingCaseId(duplicating_ids)

        if case_id_invalid:
            raise plugin_exceptions.InvalidCaseId()
        return cases_ids, tests

    def _extract_qase_marker(
        self,
        item: pytest.Function,
    ) -> pytest.Mark | None:
        """Extract qase's `Mark` object from test item."""
        qase_markers = [
            marker for marker in item.own_markers if marker.name == "qase"
        ]
        if not qase_markers:
            return None

        if len(qase_markers) == 1:
            return qase_markers[0]

        self._logger.error(
            f"Multiple qase IDs associated with: {item}",
        )
        raise plugin_exceptions.MultipleIDsForTest()

    def _extract_case_id_from_marker(
        self,
        project_code: str,
        marker: pytest.Mark,
    ) -> None | int:
        """Shortcut to extract qase case ID from marker."""
        if len(marker.args) != 1:
            return None
        url = marker.args[0]
        return int(url.split(f"{project_code}-")[-1])

    def _extract_case_id_from_test(
        self,
        project_code: str,
        item: pytest.Function,
    ) -> int | None:
        """Get test case's id marker from test item."""
        marker = self._extract_qase_marker(item=item)
        if not marker:
            return None
        return self._extract_case_id_from_marker(
            project_code=project_code,
            marker=marker,
        )

__init__(browser, env, project_code, file_storage, config)

Init converter.

Source code in pytest_qaseio/converter.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def __init__(
    self,
    browser: str,
    env: str,
    project_code: str,
    file_storage: storage.FileStorage | None,
    config: pytest.Config,
) -> None:
    """Init converter."""
    super().__init__()
    self._logger = logging.getLogger("qase")
    self._logger.addHandler(
        logging.StreamHandler(sys.stderr),
    )
    self._env = env
    self._browser = browser
    self._project_code = project_code
    self._file_storage = file_storage
    self._config = config

prepare_report_data(case_id, run_id, item, report)

Create a test result based on results from pytest.

Source code in pytest_qaseio/converter.py
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
def prepare_report_data(
    self,
    case_id: int,
    run_id: int,
    item: pytest.Function,
    report: pytest.TestReport,
) -> ResultCreate:
    """Create a test result based on results from pytest."""
    if hasattr(report, "wasxfail"):
        return self._prepare_xfailed_test_report(
            case_id=case_id,
            report=report,
        )
    match report.outcome:
        case "passed":
            return self._prepare_passed_test_report(
                case_id=case_id,
                report=report,
            )
        case "skipped":
            return self._prepare_skipped_test_report(
                case_id=case_id,
                report=report,
            )
        case "failed":
            return self._prepare_failed_test_report(
                case_id=case_id,
                run_id=run_id,
                item=item,
                report=report,
            )
    raise ValueError("Failed to convert test result!")

prepare_run_data(cases_ids_from_api, items)

Prepare data needed to create test run.

Source code in pytest_qaseio/converter.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def prepare_run_data(
    self,
    cases_ids_from_api: list[int],
    items: list[pytest.Function],
) -> tuple[RunCreate, dict[str, int | None]]:
    """Prepare data needed to create test run."""
    cases, tests = self._prepare_cases_for_run(
        cases_ids_from_api=cases_ids_from_api,
        items=items,
    )
    title = self._config.hook.pytest_get_run_name(
        config=self._config,
        env=self._env,
        browser=self._browser,
    )
    run_data = RunCreate(
        title=title,
        cases=cases,
    )

    return run_data, tests