Utils
qa_testing_utils
__all__ = ['ContainsStringIgnoringCase', 'Context', 'FromTupleMixin', 'ImmutableMixin', 'InvalidValueException', 'IsIteratorYielding', 'IsIteratorYieldingAll', 'IsStreamContainingEvery', 'IsWithinDates', 'IterableReader', 'LoggerMixin', 'SingletonBase', 'SingletonMeta', 'TestException', 'ThreadLocal', 'ToDictMixin', 'TracingMatcher', 'Valid', 'WithMixin', 'adapted_iterator', 'adapted_object', 'adapted_sequence', 'classproperty', 'configure', 'contains_string_ignoring_case', 'crc32_of', 'decompress_xz_stream', 'extract_files_from_tar', 'get_test_body', 'logger', 'makereport', 'match_as', 'process_next', 'read_lines', 'require_not_none', 'safely', 'sleep_for', 'stream_file', 'swallow', 'to_string', 'trace', 'tracing', 'valid', 'within_dates', 'write_csv', 'yields_every', 'yields_item', 'yields_items']
module-attribute
ContainsStringIgnoringCase
Bases: BaseMatcher[str]
Matcher that checks if a string contains a given substring, ignoring case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
substring
|
str
|
The substring to search for (case-insensitive). |
required |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
substring = substring.lower()
instance-attribute
__init__(substring)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
73 74 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
81 82 83 |
|
Context
dataclass
Per-thread context for reporting and logging, allowing dynamic formatting of messages.
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
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 |
|
__init__(_formatter)
default()
classmethod
Returns a default Context instance with a no-op formatter.
Returns:
Name | Type | Description |
---|---|---|
Context |
Context
|
A Context instance with the identity formatter. |
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
27 28 29 30 31 32 33 34 35 |
|
set(context_fn)
classmethod
Sets per-thread context function to be used for formatting report and log messages.
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
41 42 43 44 |
|
traced(func)
classmethod
Decorator to log function entry, arguments, and return value at DEBUG level.
Also adds an Allure step for reporting. Use on methods where tracing is useful for debugging or reporting.
Example
@Context.traced def my_method(self, x): ...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[P, R]
|
The function to be decorated. |
required |
*args
|
Any
|
Positional arguments to be passed to the function. |
required |
**kwargs
|
Any
|
Keyword arguments to be passed to the function. |
required |
Returns:
Type | Description |
---|---|
Callable[_P, _R]
|
Callable[P, R]: The result of the function call. |
Source code in qa-testing-utils/src/qa_testing_utils/logger.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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
FromTupleMixin
Mixin that adds a from_tuple
class method for instantiating objects from a tuple.
Allows creating an instance of a class (dataclass or regular class) by passing a tuple whose values match the order of the class fields. Works with frozen dataclasses as well.
Example
@dataclass(frozen=True) class Point(FromTupleMixin): x: int y: int p = Point.from_tuple((1, 2))
Source code in qa-testing-utils/src/qa_testing_utils/tuple_utils.py
9 10 11 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 |
|
from_tuple(data)
classmethod
Instantiates the class from a tuple of values, matching the order of class fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tuple[Any, ...]
|
Tuple of values corresponding to the class fields. |
required |
Returns: Self: An instance of the class with fields set from the tuple.
Source code in qa-testing-utils/src/qa_testing_utils/tuple_utils.py
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 |
|
ImmutableMixin
Mixin to enforce immutability after initialization.
Overrides setattr to raise AttributeError if an attribute is modified after being set.
Intended for use with non-dataclasses. For dataclasses, use @dataclass(frozen=True)
.
Limitations
- Does not work with WithMixin if attributes have default values.
- Does not work if applied to a superclass with a custom init.
Example
class MyImmutable(ImmutableMixin): foo: int = 1 obj = MyImmutable() obj.foo = 2 # Raises AttributeError
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
__setattr__(key, value)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
54 55 56 57 58 |
|
InvalidValueException
Bases: ValueError
Raised when an object fails validation via the Valid protocol.
Example
if not obj.is_valid(): raise InvalidValueException(obj)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
199 200 201 202 203 204 205 206 207 |
|
IsIteratorYielding
Bases: BaseMatcher[Iterator[T]]
Matcher for data yielded by iterators.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
element_matcher = element_matcher
instance-attribute
__init__(element_matcher)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
105 106 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
117 118 119 120 |
|
IsIteratorYieldingAll
Bases: BaseMatcher[Iterator[T]]
Matcher to ensure that the iterator yields at least one instance of each specified matcher.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
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 |
|
element_matchers = element_matchers
instance-attribute
__init__(element_matchers)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
155 156 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
169 170 171 172 173 174 175 |
|
IsStreamContainingEvery
Bases: BaseMatcher[Iterator[T]]
Matcher to ensure every element yielded by an iterator matches a given matcher.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
element_matcher = element_matcher
instance-attribute
__init__(element_matcher)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
131 132 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
143 144 145 146 |
|
IsWithinDates
Bases: BaseMatcher[DateOrDateTime]
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
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 |
|
end_date = end_date
instance-attribute
start_date = start_date
instance-attribute
__init__(start_date, end_date)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
183 184 185 186 187 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
217 218 219 220 221 222 223 224 |
|
IterableReader
Bases: RawIOBase
, LoggerMixin
, ImmutableMixin
I/O read-only stream over iterable of bytes, enabling streaming mode.
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
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 |
|
__init__(chunks)
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
27 28 29 |
|
from_(chunks, buffer_size=DEFAULT_BUFFER_SIZE)
staticmethod
Converts a stream of binary chunks into a BufferedReader.
You should ensure closing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks
|
Iterable[bytes]
|
stream of binary chunks |
required |
Returns:
Type | Description |
---|---|
BinaryIO
|
io.BufferedReader: buffered reader around stream of binary chunks. |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
readable()
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
31 32 33 |
|
readinto(output_buffer)
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
LoggerMixin
Mixin that provides a log
property for convenient class-based logging.
Inherit from this mixin to get a self.log
logger named after the class.
Useful for adding debug/info/error logging to any class without boilerplate.
Example
class MyClass(LoggerMixin): def do_something(self): self.log.info("Doing something")
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
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 |
|
log
cached
property
Returns a logger named after the class.
Returns:
Type | Description |
---|---|
Logger
|
logging.Logger: The logger instance for this class. |
trace(value)
Logs value at DEBUG level using this logger.
Use to log something as a value, usually in a lambda expression::
then.eventually_assert_that(
lambda: self.trace(...call some API...),
greater_that(0)) .and_....other verifications may follow...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
T
|
The value to log. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
The value (unchanged). |
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
SingletonBase
Base class for singletons using SingletonMeta.
Inherit from this class to make your class a singleton.
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
190 191 192 193 194 195 196 |
|
SingletonMeta
Bases: type
Thread-safe singleton metaclass.
Ensures only one instance of a class exists per process.
Use by setting metaclass=SingletonMeta
on your class.
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
__call__(*args, **kwargs)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
180 181 182 183 184 185 186 187 |
|
TestException
Bases: Exception
Marks an exception raised by tests infrastructure. Useful to differentiate between unexpected run-time exceptions, which should be handled as programming errors, and legitimate run-time exceptions such as time-out, not found, etc. The former might be handled via a retry mechanism.
Source code in qa-testing-utils/src/qa_testing_utils/exceptions.py
5 6 7 8 9 10 11 12 |
|
ThreadLocal
Thread-local storage for a value, with a default initializer.
Provides per-thread storage for a value of type T, initialized with a default.
Source code in qa-testing-utils/src/qa_testing_utils/thread_utils.py
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 |
|
__init__(default=None)
Initializes the thread-local storage with a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
Optional[T]
|
The default value for each thread, None if not specified. |
None
|
Source code in qa-testing-utils/src/qa_testing_utils/thread_utils.py
34 35 36 37 38 39 40 41 42 |
|
get()
Gets the thread-local value for the current thread.
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
The value for the current thread. |
Source code in qa-testing-utils/src/qa_testing_utils/thread_utils.py
53 54 55 56 57 58 59 60 |
|
set(value)
Sets the thread-local value for the current thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
T
|
The value to set for the current thread. |
required |
Source code in qa-testing-utils/src/qa_testing_utils/thread_utils.py
44 45 46 47 48 49 50 51 |
|
ToDictMixin
Mixin to add serialization methods to dataclasses.
Provides
- to_dict(): Recursively converts a dataclass (and nested dataclasses) to a dictionary.
- flatten(): Flattens nested structure for CSV or flat serialization.
Example
@dataclass class User(ToDictMixin): name: str age: int
user = User("Alice", 30) user.to_dict() # {'name': 'Alice', 'age': 30}
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
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 |
|
flatten(prefix='')
Flattens the nested structure into a flat dictionary for CSV serialization.
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
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 |
|
to_dict()
Converts a dataclass instance (with nested dataclasses) to a dictionary.
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
TracingMatcher
Bases: BaseMatcher[T]
, LoggerMixin
A matcher wrapper that adds debug logging around another matcher.
Logs the result of each match attempt using the class logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
matcher
|
Matcher[T]
|
The matcher to wrap and trace. |
required |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
__init__(matcher)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
37 38 |
|
describe_to(description)
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
45 46 |
|
Valid
Bases: Protocol
Specifies a method for validating objects.
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
is_valid()
Should be implemented by objects that need validation.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
true, if the object is valid |
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
26 27 28 29 30 31 32 33 |
|
WithMixin
Mixin to support copy-on-change (functional update) for objects.
Instead of mutating an object, use with_()
to create a copy with updated fields:
obj2 = obj.with_(field=new_value)
Works with both plain Python classes and dataclasses.
Example
@dataclass(frozen=True) class Point(WithMixin): x: int y: int
p1 = Point(1, 2) p2 = p1.with_(x=3) # p2 is Point(3, 2)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
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 |
|
with_(**changes)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
classproperty
Descriptor for defining class-level properties (like @property but for classes).
Example
class MyClass: @classproperty def foo(cls): return ...
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
fget = fget
instance-attribute
__get__(instance, owner)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
265 266 |
|
__init__(fget)
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
262 263 |
|
adapted_iterator(converter, matcher)
Hamcrest matcher adapting an Iterator of type T by specified converter and applying specified matcher. For example::
adapt_iterator( lambda message: message.id,
yields_item(is_greater_than(0)) )
where id being a number, and is_greater_than being a matcher that can be applied on numbers.
See more on PyHamcrest <https://github.com/hamcrest/PyHamcrest>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
converter
|
Callable[[T], R]
|
function converting T into R |
required |
matcher
|
Matcher[Iterator[R]
|
matcher for adapted Iterator of R |
required |
Returns:
Type | Description |
---|---|
Matcher[Iterator[T]]
|
Matcher[Iterator[T]]: matcher for target Iterator of type T |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
|
adapted_object(converter, matcher)
Hamcrest matcher adapting an object of type T by specified converter and applying specified matcher. For example::
adapt_object( lambda message: message.id,
is_greater_than(0) )
where id being a number, and is_greater_than being a matcher that can be applied on numbers.
See more on PyHamcrest <https://github.com/hamcrest/PyHamcrest>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
converter
|
Callable[[T], R]
|
function converting T into R |
required |
matcher
|
Matcher[R]
|
matcher for adapted type R |
required |
Returns:
Type | Description |
---|---|
Matcher[T]
|
Matcher[T]: matcher for target type T |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
adapted_sequence(converter, matcher)
Hamcrest matcher adapting a Sequence of type T by specified converter and applying specified matcher. For example::
adapt_sequence( lambda message: message.id,
has_item(is_greater_than(0)) )
where id being a number, and is_greater_than being a matcher that can be applied on numbers.
See more on PyHamcrest <https://github.com/hamcrest/PyHamcrest>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
converter
|
Callable[[T], R]
|
function converting T into R |
required |
matcher
|
Matcher[Sequence[R]
|
matcher for adapted Sequence of R |
required |
Returns:
Type | Description |
---|---|
Matcher[Sequence[T]]
|
Matcher[Sequence[T]]: matcher for target Sequence of type T |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
configure(config, path=Path(__file__).parent / 'logging.ini')
Configures logging for pytest using a specified INI file, or defaults to internal logging.ini.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Config
|
The pytest configuration object. |
required |
path
|
Path
|
Path to the logging configuration file. Defaults to 'logging.ini' in the current directory. |
parent / 'logging.ini'
|
Source code in qa-testing-utils/src/qa_testing_utils/conftest_helpers.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
contains_string_ignoring_case(substring)
Creates a matcher that checks if a given string contains the specified substring, ignoring case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
substring
|
str
|
The substring to search for within the target string, case-insensitively. |
required |
Returns:
Name | Type | Description |
---|---|---|
ContainsStringIgnoringCase |
ContainsStringIgnoringCase
|
A matcher object that evaluates whether the target string contains the specified substring, ignoring case. |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
86 87 88 89 90 91 92 93 94 95 96 |
|
crc32_of(file, chunk_size=DEFAULT_BUFFER_SIZE)
Calculate the CRC of a binary stream from its current position to its tail, using chunked reading.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
BinaryIO
|
The file object to read data from, starting from its current position. |
required |
chunk_size
|
int
|
The size of chunks to read at a time (default is 8192). |
DEFAULT_BUFFER_SIZE
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Calculated CRC value of the remaining file content. |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
decompress_xz_stream(compressed_chunks)
Decompresses XZ stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
compressed_chunks
|
Iterable[bytes]
|
stream of binary compressed chunks |
required |
Yields:
Type | Description |
---|---|
bytes
|
Iterator[bytes]: the decompressed stream |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
129 130 131 132 133 134 135 136 137 138 139 140 |
|
extract_files_from_tar(tar_chunks)
Extracts files from decompressed TAR stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tar_chunks
|
Iterable[bytes]
|
stream of decompressed TAR chunks |
required |
Yields:
Type | Description |
---|---|
Tuple[TarInfo, bytes]
|
Iterator[Tuple[tarfile.TarInfo, bytes]]: streams tuples of meta-data and data for each file |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
get_test_body(item)
Retrieves the source code of the test function for the given pytest item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
Item
|
The pytest test item. |
required |
Returns: str: The source code of the test function, or an error message if unavailable.
Source code in qa-testing-utils/src/qa_testing_utils/conftest_helpers.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
makereport(item, call)
Creates a pytest test report and appends the test body source code to the report sections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
Item
|
The pytest test item. |
required |
call
|
CallInfo[None]
|
The call information for the test. |
required |
Returns: pytest.TestReport: The generated test report with the test body included.
Source code in qa-testing-utils/src/qa_testing_utils/conftest_helpers.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
match_as(matcher)
Utility function to cast a generic matcher to the specific type Matcher[T].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
matcher
|
Matcher[object]
|
The original matcher that needs to be cast. |
required |
Returns:
Type | Description |
---|---|
Matcher[T]
|
A matcher cast to Matcher[T]. |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
396 397 398 399 400 401 402 403 404 405 406 |
|
process_next(i, p)
Processes next items per specified predicate. Useful for cases in which the first item in a stream decides the meaning of the rest of the items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i
|
Iterator[T]
|
the iterator to process |
required |
p
|
Predicate[T]
|
the predicate to be applied on |
required |
Returns:
Type | Description |
---|---|
Iterator[T]
|
Iterator[T]: the original iterator if the predicate evaluated true, otherwise empty iterator |
Source code in qa-testing-utils/src/qa_testing_utils/stream_utils.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
read_lines(byte_stream, encoding=UTF_8, eol=LF)
Converts a stream of binary chunks into stream of text lines. Handles cases where lines are split across chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
byte_stream
|
Iterable[bytes]
|
the binary (chunks) stream |
required |
encoding
|
str
|
expected text encoding. Defaults to 'utf-8'. |
UTF_8
|
eol
|
str
|
expected line-ending. Default to LF. |
LF
|
Yields:
Type | Description |
---|---|
str
|
Iterator[str]: stream of text lines, not terminated by EOL marker |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
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 |
|
require_not_none(value, message='Value must not be None')
Ensures that the provided value is not None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Optional[T]
|
The value to check for None. |
required |
message
|
str
|
The error message to use if value is None. Defaults to "Value must not be None". |
'Value must not be None'
|
Raises:
Type | Description |
---|---|
ValueError
|
If value is None. |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
The value, guaranteed to be not None. |
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
safely(supplier)
Calls a function safely, wrapping its result in Maybe, and swallowing any exceptions. The function should be a no-argument callable::
safely(lambda: call_something_that_may_fail(params))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
supplier
|
Supplier[T]
|
The supplier to be called. |
required |
Returns:
Type | Description |
---|---|
Maybe[T]
|
Maybe[T]: The result wrapped in Maybe, or Nothing if an exception occurs. |
Source code in qa-testing-utils/src/qa_testing_utils/exception_utils.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
sleep_for(duration)
Sleep for the specified duration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
duration
|
timedelta
|
The amount of time to sleep. |
required |
Source code in qa-testing-utils/src/qa_testing_utils/thread_utils.py
17 18 19 20 21 22 23 24 |
|
stream_file(file_path, chunk_size=DEFAULT_BUFFER_SIZE)
Streams a binary file from disk into an iterator.
If the iterator is not consumed, the file will be closed when the iterator will be garbage collected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
path to file |
required |
chunk_size
|
int
|
the chunk size. Defaults to 8192. |
DEFAULT_BUFFER_SIZE
|
Yields:
Type | Description |
---|---|
bytes
|
Iterator[bytes]: the binary chunks stream |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
swallow(func)
Decorates a function to swallow any exceptions.
If an exception will occur, None will be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
the function, supplied by the run-time |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable[..., Any]
|
the decorated function |
Source code in qa-testing-utils/src/qa_testing_utils/exception_utils.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
to_string(indent=' ', depth=1, width=72, seq_length=15, show_protected=False, show_private=False, show_static=False, show_properties=True, show_address=False, str_length=50)
Class decorator providing a readable str implementation.
The default Python str implementation, returns the type and the memory address of instance.
Important for diagnostics, actually every object that is logged, must provide such readable str.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indent
|
str
|
indentation; Defaults to ' '. |
' '
|
depth
|
int
|
depth in object hierarchy; defaults to 1. |
1
|
width
|
int
|
width of line before line-feed; defaults to 72. |
72
|
seq_length
|
int
|
how many items to include; defaults to 15. |
15
|
show_protected
|
bool
|
include protected; Defaults to False. |
False
|
show_private
|
bool
|
include private; defaults to False. |
False
|
show_static
|
bool
|
include static; defaults to False. |
False
|
show_properties
|
bool
|
include properties; defaults to True. |
True
|
show_address
|
bool
|
include object's memory address; defaults to False. |
False
|
str_length
|
int
|
maximum string length per item; defaults to 50. |
50
|
Returns:
Type | Description |
---|---|
Callable[[Type[T]], Type[T]]
|
Callable[[Type[T]], Type[T]]: description |
Source code in qa-testing-utils/src/qa_testing_utils/string_utils.py
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 |
|
trace(value)
Logs at debug level using the invoking module name as the logger.
Source code in qa-testing-utils/src/qa_testing_utils/logger.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
tracing(matcher)
Wraps a matcher with TracingMatcher to enable debug logging.
Usage
assert_that(actual, traced(contains_string("hello")))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
matcher
|
Matcher[T]
|
The matcher to wrap. |
required |
Returns: TracingMatcher[T]: The wrapped matcher with tracing enabled.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
valid(value)
Validates the specified object, assuming it supports the Valid protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
T
|
Valid): The object to validate. |
required |
Raises:
Type | Description |
---|---|
InvalidValueException
|
If the object is invalid (is_valid() returns False). |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
Valid: The validated object if valid. |
Source code in qa-testing-utils/src/qa_testing_utils/object_utils.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
within_dates(start_date, end_date)
Creates an instance of IsWithinDates to check if a date or datetime value falls within the specified start and end dates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_date
|
Optional[DateOrDateTime]
|
The start of the date range. Can be None to indicate no lower bound. |
required |
end_date
|
Optional[DateOrDateTime]
|
The end of the date range. Can be None to indicate no upper bound. |
required |
Returns:
Name | Type | Description |
---|---|---|
IsWithinDates |
IsWithinDates
|
An instance configured with the provided start and end dates. |
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
write_csv(file_path, data_stream)
Writes a stream of flattened telemetry packets to a CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
Path to the CSV file to be written. |
required |
data_stream
|
Iterable[dict[str, object]]
|
Iterable of dictionaries representing the rows to be written. |
required |
Source code in qa-testing-utils/src/qa_testing_utils/file_utils.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
yields_every(match)
Matches if every element yielded by the iterator matches a given matcher.
:param match: The matcher to satisfy, or an expected value for equality matching.
This matcher iterates through the evaluated iterator, checking that every element satisfies the given matcher. If any element does not match, the matcher fails.
If the match
argument is not a matcher, it is implicitly wrapped in an
equality matcher.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
yields_item(match)
Matches if any element of yielded by iterator matches a given matcher.
:param match: The matcher to satisfy, or an expected value for
:py:func:~hamcrest.core.core.isequal.equal_to
matching.
This matcher iterates the evaluated iterator, searching for any element
that satisfies a given matcher. If a matching element is found,
has_item
is satisfied.
If the match
argument is not a matcher, it is implicitly wrapped in an
:py:func:~hamcrest.core.core.isequal.equal_to
matcher to check for
equality.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|
yields_items(matches)
Matches if each specified item is yielded at least once by the iterator.
:param matches: An iterable of matchers or values, each of which should be yielded at least once in the iterator for this matcher to succeed.
This matcher will iterate through the evaluated iterator and check if it yields at least one instance of each specified matcher or value.
Source code in qa-testing-utils/src/qa_testing_utils/matchers.py
276 277 278 279 280 281 282 283 284 285 286 287 |
|