Give Your Test Scripts a Brain: 15 Cutting‑Edge AI Decorators for 2026
The article showcases fifteen practical AI‑powered Python decorators that transform brittle if‑else test code into intelligent, self‑healing automation—covering smart retry, semantic assertions, data generation, flaky detection, traffic replay, dynamic timeouts, sensitive data masking, root‑cause analysis, and more—complete with concrete code samples and explanations.
Smart Retry: Stop Blind Retries
Traditional @retry blindly retries three times regardless of the error. @ai_retry sends the error message to an LLM, which decides whether a retry makes sense and, if so, how long to wait.
import time
from functools import wraps
from openai import OpenAI
client = OpenAI(api_key="your_key")
def ai_retry(max_attempts=3):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except Exception as e:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a ops expert. Analyze the error and decide if a retry is needed. Return JSON {'should_retry': bool, 'wait_seconds': int}"},
{"role": "user", "content": f"Error: {str(e)}. Attempt {attempt+1}"}
]
)
advice = eval(response.choices[0].message.content)
if advice['should_retry']:
print(f"AI suggests retry, waiting {advice['wait_seconds']} seconds…")
time.sleep(advice['wait_seconds'])
else:
print("AI decides retry is useless, aborting.")
raise e
return wrapper
return decorator
@ai_retry(max_attempts=3)
def call_payment_api():
raise ConnectionError("Connection timed out")Semantic Assertion: Say Goodbye to Regex Hell
Instead of hard‑coded asserts, @smart_assert lets you describe the expected outcome in natural language. The decorator forwards the response and description to an LLM, which replies with a simple yes/no.
def smart_assert(description):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
response_data = func(*args, **kwargs)
prompt = f"API response: {response_data}. Does it satisfy: {description}? Answer only yes or no."
result = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
if "yes" not in result.choices[0].message.content.lower():
raise AssertionError(f"AI assertion failed: {description}")
return response_data
return wrapper
return decorator
@smart_assert("check that the order succeeded")
def test_withdraw():
return {"code": 200, "msg": "操作成功", "balance": 100.5}Intelligent Data Faker: Business‑Aware Test Data
@data_fakeraccepts a natural‑language instruction and asks an LLM to generate a JSON payload that matches the business rule.
def data_faker(instruction):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a test data generator, output pure JSON."},
{"role": "user", "content": f"Generate test data that satisfies: {instruction}"}
]
)
mock_data = json.loads(response.choices[0].message.content)
kwargs.update(mock_data)
return func(*args, **kwargs)
return wrapper
return decorator
@data_faker("generate a customer support dialogue with a slight complaining tone, include user_name and content")
def test_sentiment_analysis(user_name, content):
print(f"Test user: {user_name}, content: {content}")Flaky Test Detector: Auto‑Isolate Unstable Tests
The decorator records each run result in a simulated database. When a test shows both PASS and FAIL over several runs, it warns and suggests isolation.
import hashlib
test_history_db = {}
def flaky_test_detector(func):
@wraps(func)
def wrapper(*args, **kwargs):
case_id = hashlib.md5(func.__code__.co_code).hexdigest()
try:
result = func(*args, **kwargs)
test_history_db[case_id] = test_history_db.get(case_id, []) + ["PASS"]
return result
except Exception:
test_history_db[case_id] = test_history_db.get(case_id, []) + ["FAIL"]
history = test_history_db[case_id]
if len(history) > 5 and "PASS" in history and "FAIL" in history:
print(f"⚠️ Warning: Test {func.__name__} is unstable, consider isolating it!")
raise
return wrapperTraffic Replay: Let AI Write the Script
@traffic_replaycaptures real production requests, asks an LLM to mask sensitive identifiers, and replays the sanitized traffic for testing.
def traffic_replay(record_file="traffic.json"):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
recorded_request = {"url": "/api/user/123", "method": "GET"}
prompt = "Replace the ID in the URL with a placeholder like /api/user/{{id}}"
# ...call LLM to transform...
print(f"Replaying traffic: {recorded_request} (AI‑desensitized)")
return func(*args, **kwargs)
return wrapper
return decoratorDynamic Timeout: Adapt to Load
@dynamic_timeoutqueries (simulated) server load and lets an LLM compute a new timeout value.
def dynamic_timeout(base_timeout=5):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
server_load = 0.9 # simulated high load
new_timeout = base_timeout * (1 + server_load)
print(f"High load detected, AI adjusts timeout to: {new_timeout} seconds")
return func(*args, **kwargs)
return wrapper
return decoratorSensitive Data Masker: NLP‑Based Compliance
The decorator sends log strings to an LLM that identifies phone numbers, ID numbers, etc., and replaces them with asterisks.
def sensitive_data_masker(func):
@wraps(func)
def wrapper(*args, **kwargs):
log_str = str(kwargs)
prompt = f"Identify phone numbers and ID numbers in the following text and replace them with ***: {log_str}"
# ...call LLM for masking...
return func(*args, **kwargs)
return wrapperRoot‑Cause Analyzer: Auto‑Generated Fix Suggestions
When a test raises an exception, the decorator sends the stack trace to an LLM, which returns a short diagnosis and advice.
def root_cause_analyzer(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
analysis = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": f"Analyze this Python stack trace and give a brief fix suggestion: {str(e)}"}]
)
print(f"💡 AI diagnosis: {analysis.choices[0].message.content}")
raise
return wrapperDocument Generator: Tests Produce Docs
After a test passes, @doc_generator extracts the function name, arguments, and response, asks an LLM to format a Markdown table row, and appends it to a documentation file.
def doc_generator(output_file="api_docs.md"):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
response = func(*args, **kwargs)
prompt = f"Based on function {func.__name__}, args {kwargs}, and response {response}, generate one Markdown table row."
doc = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
with open(output_file, "a") as f:
f.write(doc.choices[0].message.content + "
")
return response
return wrapper
return decoratorAnomaly Alert: Trend‑Based Performance Warning
The decorator records the execution time, compares it with a simulated historical average, and alerts if the current duration exceeds the average by a configurable factor.
def anomaly_alert(threshold_factor=2.0):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
try:
return func(*args, **kwargs)
finally:
duration = time.time() - start
avg_duration = 0.2 # simulated 200 ms
if duration > avg_duration * threshold_factor:
print(f"🚨 Performance anomaly! Current {duration:.2f}s > historical {avg_duration}s")
return wrapper
return decoratorSmart Mock: One‑Line Scenario Stubbing
@smart_mockreceives a natural‑language scenario, asks an LLM to generate a matching JSON response, and returns it during the test.
def smart_mock(scenario):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "Generate a JSON response for the given scenario."},
{"role": "user", "content": f"Scenario: {scenario}"}
]
)
return response.choices[0].message.content
return wrapper
return decorator
@smart_mock("Payment system under maintenance, return 503 error")
def mock_payment():
passFuzz Test Generator: AI‑Powered Edge Cases
The decorator asks an LLM to produce a short, malicious input (e.g., an SQL‑injection string) and injects it into the test call.
def fuzz_test(func):
@wraps(func)
def wrapper(*args, **kwargs):
prompt = "Generate a string with SQL‑injection characteristics, max 100 characters."
malicious_input = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
).choices[0].message.content
kwargs['data'] = malicious_input
return func(*args, **kwargs)
return wrapperVisual Regression: AI‑Aware UI Diff
@visual_diffcompares screenshot diff scores produced by an LLM and, if the score exceeds a threshold, asks a vision model whether the change is functionally relevant.
def visual_diff(threshold=0.1):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
diff_score = 0.05 # simulated AI output
if diff_score > threshold:
ai_judge = client.chat.completions.create(
model="gpt-4o-vision",
messages=[{"role": "user", "content": "Does this UI difference affect functionality?"}]
)
if "不影响" not in ai_judge.choices[0].message.content:
raise AssertionError("UI regression detected")
return func(*args, **kwargs)
return wrapper
return decoratorDependency Check: Skip Tests When Services Are Down
Before running a test, the decorator queries (simulated) the availability of a downstream service; if unavailable, it skips the test.
def dependency_check(service_name):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
is_available = False # simulated outage
if not is_available:
print(f"⏭️ Skipping test: Dependency {service_name} is under maintenance")
return None
return func(*args, **kwargs)
return wrapper
return decoratorValue Assessor: Prune Zombie Tests
The decorator looks at a simulated "last run" age; if a test hasn't run for over six months, it warns that the test may be removed.
def value_assessor(func):
@wraps(func)
def wrapper(*args, **kwargs):
last_run_days = 365
if last_run_days > 180:
print(f"🗑️ Warning: Test {func.__name__} hasn't run for {last_run_days} days, consider deleting.")
return func(*args, **kwargs)
return wrapperThese fifteen decorators share a common goal: liberate engineers from repetitive, error‑prone scripting and let AI handle decision‑making, data generation, analysis, and documentation, turning test code into a self‑aware, adaptable component of the 2026 testing ecosystem.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
