|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (c) 2023, Cyber Alpaca |
| 4 | +# All rights reserved. |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | +from contextlib import contextmanager |
| 8 | + |
| 9 | +import squish |
| 10 | +import test |
| 11 | + |
| 12 | + |
| 13 | +class LogLevel: |
| 14 | + DEBUG = 10 |
| 15 | + LOG = 20 |
| 16 | + WARNING = 30 |
| 17 | + FAIL = 40 |
| 18 | + FATAL = 50 |
| 19 | + |
| 20 | + |
| 21 | +LOGLEVEL = LogLevel.LOG |
| 22 | + |
| 23 | + |
| 24 | +def __is_level_enabled(level: LogLevel) -> bool: |
| 25 | + """Checks the given log level against the currently set LOGLEVEL |
| 26 | +
|
| 27 | + Args: |
| 28 | + level (LogLevel): log level to check |
| 29 | +
|
| 30 | + Returns: |
| 31 | + bool: True if level is higher then LOGLEVEL, False otherwise |
| 32 | + """ |
| 33 | + return LOGLEVEL <= level |
| 34 | + |
| 35 | + |
| 36 | +def debug(msg: str, details: str = "") -> None: |
| 37 | + """Adds a DEBUG-level log entry with the given message and details to a test report. |
| 38 | +
|
| 39 | + This function adds a log message to Squish's test report at the DEBUG log level, |
| 40 | + which allows for detailed debugging information to be recorded. |
| 41 | + The log message will include the given message and details provided as arguments. |
| 42 | + The message will be prefixed with the string 'DEBUG: ' to indicate its log level. |
| 43 | +
|
| 44 | + The log message will only be visible if the LOGLEVEL is set to DEBUG. |
| 45 | + Otherwise, it will be ignored and not included in the test report. |
| 46 | +
|
| 47 | + Args: |
| 48 | + - msg (str): The message to include in the log entry. |
| 49 | + - details (str): Optional additional details to include in the log entry. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + None |
| 53 | + """ |
| 54 | + if __is_level_enabled(LogLevel.DEBUG): |
| 55 | + test.fixateResultContext(1) |
| 56 | + try: |
| 57 | + test.log(f"[DEBUG] {msg}", details) |
| 58 | + finally: |
| 59 | + test.restoreResultContext() |
| 60 | + |
| 61 | + |
| 62 | +def log(msg: str, details: str = "") -> None: |
| 63 | + """Adds a log entry with the given message and details to a test report. |
| 64 | +
|
| 65 | + This function adds a log message to Squish's test report at the LOG log level |
| 66 | + or lower, depending on the current log level setting. |
| 67 | + The log message will include the given message and details provided as arguments. |
| 68 | +
|
| 69 | + The log message will only be visible if the LOGLEVEL is set to LOG or lower. |
| 70 | + Otherwise, it will be ignored and not included in the test report. |
| 71 | +
|
| 72 | + Args: |
| 73 | + - msg (str): The message to include in the log entry. |
| 74 | + - details (str): Optional additional details to include in the log entry. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + None |
| 78 | + """ |
| 79 | + if __is_level_enabled(LogLevel.LOG): |
| 80 | + test.fixateResultContext(1) |
| 81 | + try: |
| 82 | + test.log(msg, details) |
| 83 | + finally: |
| 84 | + test.restoreResultContext() |
| 85 | + |
| 86 | + |
| 87 | +def warning(msg: str, details: str = "") -> None: |
| 88 | + """Adds a warning entry with the given message and details to a test report. |
| 89 | +
|
| 90 | + This function adds a warning message to Squish's test report at the WARNING |
| 91 | + log level or lower, depending on the current log level setting. |
| 92 | + The warning message will include the given message and details provided |
| 93 | + as arguments. |
| 94 | +
|
| 95 | + The warning message will only be visible if the LOGLEVEL is set to WARNING or lower. |
| 96 | + Otherwise, it will be ignored and not included in the test report. |
| 97 | +
|
| 98 | + Args: |
| 99 | + - msg (str): The message to include in the warning entry. |
| 100 | + - details (str): Optional additional details to include in the warning entry. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + None |
| 104 | + """ |
| 105 | + if __is_level_enabled(LogLevel.WARNING): |
| 106 | + test.fixateResultContext(1) |
| 107 | + try: |
| 108 | + test.warning(msg, details) |
| 109 | + finally: |
| 110 | + test.restoreResultContext() |
| 111 | + |
| 112 | + |
| 113 | +def fail(msg: str, details: str = "") -> None: |
| 114 | + """Adds a fail entry with the given message and details to a test report. |
| 115 | +
|
| 116 | + This function adds a fail message to Squish's test report at the FAIL log level |
| 117 | + or lower, depending on the current log level setting. |
| 118 | + The fail message will include the given message and details provided as arguments. |
| 119 | +
|
| 120 | + The fail message will only be visible if the LOGLEVEL is set to FAIL or lower. |
| 121 | + Otherwise, it will be ignored and not included in the test report. |
| 122 | +
|
| 123 | + Args: |
| 124 | + - msg (str): The message to include in the fail entry. |
| 125 | + - details (str): Optional additional details to include in the fail entry. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + None |
| 129 | + """ |
| 130 | + if __is_level_enabled(LogLevel.FAIL): |
| 131 | + test.fixateResultContext(1) |
| 132 | + try: |
| 133 | + test.fail(msg, details) |
| 134 | + finally: |
| 135 | + test.restoreResultContext() |
| 136 | + |
| 137 | + |
| 138 | +def fatal(msg: str, details: str = "") -> None: |
| 139 | + """Adds a fatal entry with the given message and details to a test report, |
| 140 | + then aborts the test case execution. |
| 141 | +
|
| 142 | + This function adds a fatal message to Squish's test report at the FATAL log level |
| 143 | + or lower, depending on the current log level setting. |
| 144 | + The fatal message will include the given message and details provided as arguments. |
| 145 | +
|
| 146 | + The fatal message will only be visible if the LOGLEVEL is set to FATAL or lower. |
| 147 | + Otherwise, it will be ignored and not included in the test report. |
| 148 | +
|
| 149 | + After adding the fatal message, the function aborts the test case execution. |
| 150 | +
|
| 151 | + Args: |
| 152 | + - msg (str): The message to include in the fatal entry. |
| 153 | + - details (str): Optional additional details to include in the fatal entry. |
| 154 | +
|
| 155 | + Returns: |
| 156 | + None |
| 157 | + """ |
| 158 | + if __is_level_enabled(LogLevel.FATAL): |
| 159 | + test.fixateResultContext(1) |
| 160 | + try: |
| 161 | + squish.testSettings.throwOnFailure = True |
| 162 | + test.fatal(msg, details) |
| 163 | + finally: |
| 164 | + test.restoreResultContext() |
| 165 | + |
| 166 | + |
| 167 | +def enable_loglevel_in_test_module(): |
| 168 | + """Adds support for log levels to the Squish 'test' module. |
| 169 | +
|
| 170 | + DISCLAIMER: This function uses monkeypathching |
| 171 | + https://en.wikipedia.org/wiki/Monkey_patch |
| 172 | +
|
| 173 | + This function overwrites some of the existing functions in the 'test' module |
| 174 | + to support logging at different log levels. |
| 175 | + Furthermore, it enhances the functionality of the 'test' module by adding |
| 176 | + a new test.debug(...) function. |
| 177 | +
|
| 178 | + By default, the 'test' module does not support LOGLEVEL at all. |
| 179 | + However, this function adds support for setting the log level to a higher |
| 180 | + or lower level, depending on the needs of the developer. |
| 181 | +
|
| 182 | + After calling this function, the following 'test' module's functions will support |
| 183 | + LOGLEVEL report setting: |
| 184 | + - test.debug(...) |
| 185 | + - test.log(...) |
| 186 | + - test.warning(...) |
| 187 | + - test.fail(...) |
| 188 | + - test.fatal(...) |
| 189 | +
|
| 190 | + Returns: |
| 191 | + None |
| 192 | + """ |
| 193 | + test.debug = debug |
| 194 | + test.log = log |
| 195 | + test.warning = warning |
| 196 | + test.fail = fail |
| 197 | + test.fatal = fatal |
| 198 | + |
| 199 | + |
| 200 | +@contextmanager |
| 201 | +def section(title: str, description: str = "") -> None: |
| 202 | + """Allows using Squish's sections as context managers |
| 203 | +
|
| 204 | + https://doc.qt.io/squish/squish-api.html#test-startsection-function |
| 205 | + Args: |
| 206 | + title (str): Section title |
| 207 | + description (str): Optional additional description of the section |
| 208 | + Examples: |
| 209 | + with section("Add new person"): |
| 210 | + squish.type(squish.waitForObject(names.forename_edit), "Bob") |
| 211 | + squish.mouseClick(squish.waitForObject(names.ok_button)) |
| 212 | + """ |
| 213 | + |
| 214 | + test.fixateResultContext(1) |
| 215 | + test.startSection(title, description) |
| 216 | + test.restoreResultContext() |
| 217 | + try: |
| 218 | + yield |
| 219 | + except Exception: |
| 220 | + raise |
| 221 | + finally: |
| 222 | + test.endSection() |
0 commit comments