Sobes.tech
Senior

Have you written custom decorators? Can you show an example?

sobes.tech AI

Answer from AI

Yes, in QA Automation, custom decorators are often written to extend test functionality, such as for logging, rerunning, or measuring execution time.

An example of a custom decorator in Python using pytest that repeats a test upon failure:

import pytest
from functools import wraps

def retry(times):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            last_exception = None
            for _ in range(times):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    last_exception = e
            raise last_exception
        return wrapper
    return decorator

@retry(times=3)
def test_example():
    assert some_flaky_function() == expected_result

This decorator helps improve test stability by rerunning tests several times before ultimately failing. Similar decorators can be created for logging, collecting metrics, and other purposes.

Have you written custom decorators? Can you show an… - sobes.tech