Stop Hand‑Writing Prompts: Use LangChain Templates to Automate API Testing with AI

API testing often involves repetitive string concatenation, inconsistent output formats, and costly token usage; this article shows how LangChain's PromptTemplate and ChatPromptTemplate turn prompts into reusable, composable components that generate assertions, test data, log analysis, multi‑turn debugging, and more, with concrete Python examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Stop Hand‑Writing Prompts: Use LangChain Templates to Automate API Testing with AI

API testing engineers frequently face "manual" prompt work: concatenating strings, fixing missing newlines or quotes, dealing with inconsistent AI output formats, and re‑sending context for each debugging round, which burns tokens. The real need is a prompt "scaffolding" rather than raw AI capability.

1. PromptTemplate: Dynamically Generate API Assertion Code

Use case: automatically create pytest assertions based on method, endpoint, and expected status code.

Basic example:

from langchain_core.prompts import PromptTemplate
template = "请为 {method} 请求 {endpoint} 接口生成一个 Pytest 断言,期望状态码为 {expected_code}。"
prompt = PromptTemplate.from_template(template)
result = prompt.format(method="POST", endpoint="/api/login", expected_code=200)
print(result)

Advanced example with partial to preset common parameters:

base_prompt = prompt.partial(method="POST")
final_result = base_prompt.format(endpoint="/api/order/create", expected_code=201)
print(final_result)

Outputs:

Basic: 请为 POST 请求 /api/login 接口生成一个 Pytest 断言,期望状态码为 200。

Advanced: 请为 POST 请求 /api/order/create 接口生成一个 Pytest 断言,期望状态码为 201。

2. PromptTemplate: Dynamically Generate Test Data

Use case: bulk‑generate JSON request bodies that follow specific field rules.

Basic example:

template = "生成 {count} 条用于接口测试的 {data_type} 数据,要求包含 id 和 name 字段。"
prompt = PromptTemplate.from_template(template)
print(prompt.format(count=2, data_type="用户注册"))

Advanced example with Jinja2 conditional logic (e.g., orders must have amount > 0):

jinja_template = """生成 {count} 条 {data_type} 数据:{% if data_type == '订单' %}金额必须大于0{% endif %}"""
prompt = PromptTemplate.from_template(jinja_template, template_format="jinja2")
print(prompt.format(count=3, data_type="订单"))

Outputs:

Basic: 生成 2 条用于接口测试的用户注册数据,要求包含 id 和 name 字段。

Advanced: 生成 3 条订单数据:金额必须大于0。

3. PromptTemplate: API Exception Log Analysis

Use case: feed an error log to the LLM and ask for possible causes.

Basic example:

template = "接口测试失败,错误日志如下:
{error_log}
请分析可能的原因。"
prompt = PromptTemplate.from_template(template)
print(prompt.format(error_log="ConnectionTimeout: 5000ms"))

Advanced example using partial to preset a concrete log:

analyst_prompt = prompt.partial(error_log="NullPointerException at OrderService.java:45")
print(analyst_prompt.format())

Outputs:

Basic: 接口测试失败,错误日志如下:ConnectionTimeout: 5000ms,请分析可能的原因。

Advanced: 接口测试失败,错误日志如下:NullPointerException at OrderService.java:45,请分析可能的原因。

4. ChatPromptTemplate: Multi‑Turn Interface Debugging

Use case: keep context across turns when debugging an API.

Basic example building a system‑human message list:

from langchain_core.prompts import ChatPromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
    ("system", "你是一个接口调试专家。"),
    ("human", "当前接口 {endpoint} 返回 403,请给出排查思路。")
])
messages = chat_prompt.format_messages(endpoint="/api/admin/users")
print(messages.content)

Advanced example using dictionary style for clearer structure:

chat_prompt = ChatPromptTemplate.from_messages([
    {"role": "system", "content": "你是测试专家。"},
    {"role": "user", "content": "接口 {ep} 报错:{err}"}
])
msgs = chat_prompt.format_messages(ep="/api/pay", err="Token Expired")
print(msgs.content)

Outputs:

Basic: 当前接口 /api/admin/users 返回 403,请给出排查思路。

Advanced: 接口 /api/pay 报错:Token Expired。

5. ChatPromptTemplate: Few‑Shot Assertion Standardization

Use case: provide example assertions so the model follows a consistent style.

Basic example:

chat_prompt = ChatPromptTemplate.from_messages([
    ("human", "为 GET /api/status 写断言"),
    ("ai", "assert response.status_code == 200"),
    ("human", "为 POST /api/login 写断言,期望 201")
])
print(chat_prompt.format_messages()[-1].content)

Advanced example dynamically injecting historical examples:

examples = [("为 GET /api/status 写断言", "assert response.status_code == 200")]
messages = []
for q, a in examples:
    messages.append(("human", q))
    messages.append(("ai", a))
messages.append(("human", "为 {target} 写断言"))
chat_prompt = ChatPromptTemplate.from_messages(messages)
print(chat_prompt.format_messages(target="DELETE /api/user/1")[-1].content)

Outputs:

Basic: 为 POST /api/login 写断言,期望 201。

Advanced: 为 DELETE /api/user/1 写断言。

6. PromptTemplate: Load Complex Business Rules from External Files

Use case: keep long rule texts separate from code.

Basic example reading rule.txt:

with open("rule.txt", "r", encoding="utf-8") as f:
    template = f.read()
prompt = PromptTemplate(input_variables=["api", "rules"], template=template)
print(prompt.format(api="退款接口", rules="金额不能为负"))

Advanced example using a global rule file and partial:

with open("global_rules.txt", "r", encoding="utf-8") as f:
    base_template = f.read()
base_prompt = PromptTemplate.from_template(base_template)
fixed_prompt = base_prompt.partial(rules="所有金额保留两位小数")
print(fixed_prompt.format(api="支付接口"))

Outputs:

Basic: 请根据以下规则校验 退款接口:金额不能为负。

Advanced: 校验 支付接口:所有金额保留两位小数。

7. PromptTemplate: Dynamic Time Injection

Use case: include the current time in prompts for coupon expiry checks.

Basic example using a lambda:

from datetime import datetime
template = "当前时间 {time},请判断优惠券是否过期。"
prompt = PromptTemplate.from_template(template)
dynamic_prompt = prompt.partial(time=lambda: datetime.now().strftime("%H:%M"))
print(dynamic_prompt.format())

Advanced example injecting a timestamp function:

def get_test_timestamp():
    return str(int(datetime.now().timestamp()))
ts_prompt = prompt.partial(time=get_test_timestamp)
print(ts_prompt.format())

Outputs (example):

Basic: 当前时间 14:30,请判断优惠券是否过期。

Advanced: 当前时间 1720000000,请判断优惠券是否过期。

8. PromptTemplate: Jinja2 Conditional Environment Adaptation

Use case: enforce different constraints in test vs. production environments.

Basic example:

template = "执行 {env} 环境测试。{% if env == 'prod' %}禁止写操作!{% endif %}"
prompt = PromptTemplate.from_template(template, template_format="jinja2")
print(prompt.format(env="prod"))

Advanced example with nested conditions:

jinja = """{% if env == 'test' %}开启Debug日志{% elif env == 'prod' %}仅记录Error{% endif %}"""
p = PromptTemplate.from_template(jinja, template_format="jinja2")
print(p.format(env="test"))

Outputs:

Basic: 执行 prod 环境测试。禁止写操作!

Advanced: 开启Debug日志。

9. PromptTemplate: Jinja2 Loop for Bulk Test Case Generation

Use case: generate validation points for each field listed in a Swagger document.

Basic example:

template = "请校验以下字段:{% for f in fields %}{{ f }}、{% endfor %}"
prompt = PromptTemplate.from_template(template, template_format="jinja2")
print(prompt.format(fields=["username", "password"]))

Advanced example producing assert statements:

loop_template = """{% for f in fields %}assert '{f}' in response.json(){% endfor %}"""
p = PromptTemplate.from_template(loop_template, template_format="jinja2")
print(p.format(fields=["id", "token"]))

Outputs:

Basic: 请校验以下字段:username、password、

Advanced: assert 'id' in response.json()assert 'token' in response.json()

10. PipelinePromptTemplate: Modular Prompt Assembly

Use case: combine role definition, test task, and user input like building blocks.

Basic example:

from langchain_core.prompts import PipelinePromptTemplate, PromptTemplate
role = PromptTemplate.from_template("你是测试专家。")
task = PromptTemplate.from_template("请测试 {api}。")
final = PromptTemplate.from_template("{role}
{task}")
pipeline = PipelinePromptTemplate(final_prompt=final, pipeline_prompts=[("role", role), ("task", task)])
print(pipeline.format(api="/login"))

Advanced example swapping the role for security testing:

new_role = PromptTemplate.from_template("你是安全测试专家。")
pipeline.pipeline_prompts = ("role", new_role)
print(pipeline.format(api="/upload"))

Outputs:

Basic: 你是测试专家。请测试 /login。

Advanced: 你是安全测试专家。请测试 /upload。

Testing Veteran Pitfall Guide

PromptTemplate vs. ChatPromptTemplate: use PromptTemplate for plain LLMs and ChatPromptTemplate for chat models (GPT‑4, Claude) that require system/human/ai roles. partial is a automation shortcut: preset fixed parameters like base URL or token to keep code clean.

Avoid over‑using Jinja2 logic inside prompts; keep conditions simple and move complex logic to separate Pipeline prompts.

When loading external rule files, always specify encoding="utf-8" to prevent garbled Chinese characters that cause the model to fail.

Integrating these template techniques into your automation framework will elevate your AI‑driven test scripts beyond naïve string concatenation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAutomationPrompt EngineeringLangChainAPI testingPromptTemplateJinja2ChatPromptTemplate
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.