更新日期:2026年9月8日
关键词:ChatGPT Pro、Codex、GPT‑6 Astra、GPT6、自动化测试、回归测试、CI
本文仅讨论软件工程技术,不涉及充值或支付。
很多开发者第一次用 AI 写测试时,会得到一种非常强烈的感觉:测试生成速度突然变快了。
但很快也会发现另一个问题:测试数量变多,并不等于系统真的更安全。
GPT‑6 Astra 与 Codex 更值得关注的方向,不是"批量生成 100 个 assert",而是让 AI 参与整个测试工程:理解代码路径、寻找未覆盖风险、设计边界用例、运行测试、分析失败,并把结果变成下一轮修改依据。
截至 2026 年 9 月 8 日,GPT‑6 Astra 正在逐步进入 ChatGPT Pro、Work 与 Codex。OpenAI 将 Astra 定位为适合复杂推理、软件工程和端到端多步骤工作的模型;对于高频使用 Codex、持续运行测试与长链路调试的开发者来说,Pro 会比单次聊天式使用更接近真实工程场景。
本文就用一个 API 服务来说明如何构建一套"AI + 确定性测试"的工作流。
一、AI 最容易生成哪种"没用的测试"
假设有函数:
python
def normalize_username(name: str) -> str:
return name.strip().lower()
AI 很容易生成:
python
def test_normalize_username():
assert normalize_username(" Alice ") == "alice"
没有问题。
但现实代码往往更像:
python
def create_user(payload, repo, audit):
username = payload["username"].strip().lower()
if repo.exists(username):
raise ValueError("username exists")
user = repo.insert({
"username": username,
"role": payload.get("role", "user")
})
audit.write(
action="user_created",
user_id=user["id"]
)
return user
如果只让 AI:
给 create_user 写单元测试。
它可能会测试正常创建和重复用户名,但仍可能遗漏:
- 空 username;
- 只有空格;
- 大小写冲突;
- repo.insert 失败;
- audit 失败;
- role 非法;
- audit 是否发生在 insert 之后;
- insert 成功但 audit 失败时系统状态如何。
真正有价值的测试来自风险模型,而不是函数行数。
二、先让 GPT‑6 Astra 做风险建模
可以在 ChatGPT Pro 中把模块、接口定义和已有测试交给 GPT‑6 Astra,然后要求:
text
不要生成测试代码。
请为 create_user 建立风险矩阵。
从下面维度检查:
1. 输入边界;
2. 状态变化;
3. 外部依赖失败;
4. 并发;
5. 权限;
6. 数据一致性;
7. 可观察副作用。
每个风险输出:
- failure mode
- impact
- existing coverage
- suggested test
预期结果应该类似:
| 风险 | 后果 | 测试 |
|---|---|---|
| username 全空格 | 创建无效用户 | 输入校验 |
| 大小写重复 | 唯一性绕过 | Alice / alice |
| insert 成功 audit 失败 | 数据存在但审计缺失 | fault injection |
| 并发创建相同用户名 | 唯一性竞态 | integration test |
这一步的价值远高于直接"多写几个测试"。
三、让 Codex 阅读现有测试风格
接下来进入 Codex。
不要让它马上新建测试框架,而是:
text
Read the existing test suite.
Before adding tests, identify:
- test framework;
- fixture conventions;
- fake/mock conventions;
- database test setup;
- naming style;
- commands used by CI.
Do not introduce a new test library
if the repository already has one.
很多 AI 生成代码的问题来自"不尊重仓库已有习惯"。例如项目已经使用 pytest fixture:
python
@pytest.fixture
def user_repo(db):
return UserRepository(db)
Codex 就不应该突然引入另一套测试风格。
四、第一层:输入边界测试
我们先补最确定的输入验证。假设修改函数:
python
def normalize_username(name: str) -> str:
normalized = name.strip().lower()
if not normalized:
raise ValueError("username required")
return normalized
测试:
python
import pytest
@pytest.mark.parametrize(
"raw,expected",
[
("Alice", "alice"),
(" ALICE ", "alice"),
("\tBob\n", "bob")
]
)
def test_normalize_username(raw, expected):
assert normalize_username(raw) == expected
@pytest.mark.parametrize(
"raw",
["", " ", "\t", "\n"]
)
def test_normalize_username_rejects_blank(raw):
with pytest.raises(
ValueError,
match="username required"
):
normalize_username(raw)
这类测试确定性高,很适合 Codex 自动生成和执行。
五、第二层:副作用测试
然后验证关键行为:
python
def test_create_user_records_audit_event(
repo,
audit
):
user = create_user(
{"username": "Alice"},
repo,
audit
)
assert audit.events == [
{
"action": "user_created",
"user_id": user["id"]
}
]
再检查重复用户不会发生 insert:
python
def test_duplicate_user_is_not_inserted(
repo,
audit
):
repo.add_existing("alice")
with pytest.raises(
ValueError,
match="username exists"
):
create_user(
{"username": "Alice"},
repo,
audit
)
assert repo.insert_calls == []
assert audit.events == []
这里真正重要的是验证没有发生什么。这也是很多自动生成测试容易遗漏的地方。
六、第三层:故障注入
如果系统依赖多个组件,仅验证成功路径是不够的。
可以创建一个失败的 audit:
python
class FailingAudit:
def write(self, **kwargs):
raise RuntimeError("audit unavailable")
测试:
python
def test_audit_failure_is_visible(repo):
with pytest.raises(
RuntimeError,
match="audit unavailable"
):
create_user(
{"username": "Alice"},
repo,
FailingAudit()
)
但这个测试马上会暴露一个更深的问题:
text
repo.insert 已经发生
audit 失败
现在问题不再是"测试怎么写",而是业务语义:
创建用户成功但审计失败,系统应该回滚吗?
这正适合交给 GPT‑6 Astra 做方案推理,而不是让 Codex 自己随意决定。
可以问:
text
We discovered this behavior:
1. user insert succeeds;
2. audit write fails;
3. API returns an error;
4. user remains in database.
Compare three designs:
- database transaction;
- outbox pattern;
- best-effort audit.
Explain consistency guarantees,
operational complexity,
and failure recovery.
Do not choose a design without
stating the assumptions.
这就是高能力模型真正应该使用的地方。
七、让 Codex 自动做失败分析
假设 CI 出现:
text
FAILED test_create_user_records_audit_event
Expected user_id=u-21
Received user_id=u-20
普通用法是把错误复制到 ChatGPT。
更完整的 Codex 工作流是:
text
The test suite has one failure.
Do not edit anything yet.
1. reproduce the failure;
2. identify the exact assertion;
3. trace where both values come from;
4. inspect recent related changes;
5. classify the failure:
- production bug
- test bug
- nondeterminism
- environment problem
6. propose the smallest fix.
Only then edit.
这里最关键的一句话是:Do not edit anything yet.
否则 AI 很容易看到红色测试就马上改代码,而没有先确认测试是不是错的。
八、建立一个测试分类系统
大型项目里,我建议让 Codex 把测试按风险层级维护。
例如:
python
TEST_LEVELS = {
"unit": {
"timeout": 5,
"network": False
},
"integration": {
"timeout": 30,
"network": False
},
"e2e": {
"timeout": 120,
"network": True
}
}
CI 分阶段执行:
yaml
jobs:
unit:
steps:
- run: pytest tests/unit -q
integration:
needs: unit
steps:
- run: pytest tests/integration -q
e2e:
needs: integration
steps:
- run: pytest tests/e2e -q
这样 Codex 在修改代码后,可以先运行相关单元测试,再逐步扩大验证范围,而不是每次都无脑跑完整套测试。
九、让 GPT‑6 Astra 生成测试计划,而不是测试答案
如果通过 API 构建内部开发工具,可以让 Astra 输出结构化测试计划。
例如定义:
python
from pydantic import BaseModel
class TestCase(BaseModel):
name: str
risk: str
test_type: str
expected_behavior: str
class TestPlan(BaseModel):
cases: list[TestCase]
调用:
python
from openai import OpenAI
client = OpenAI()
response = client.responses.parse(
model="gpt-6-astra",
reasoning={"effort": "high"},
input="""
Design regression tests for the
supplied user-creation module.
Focus on behavior and failure modes.
Do not generate implementation code.
""",
text_format=TestPlan
)
plan = response.output_parsed
for case in plan.cases:
print(
case.name,
case.test_type,
case.risk
)
然后再让 Codex 根据 TestPlan 在仓库中实现测试。
这就形成一个更清晰的分工:
text
GPT‑6 Astra
↓
风险分析 / Test Plan
↓
Codex
↓
实现测试
↓
pytest / CI
↓
确定性结果
十、测试必须防止 AI 自己"作弊"
如果给 Codex 的目标只是:
让测试通过。
这是危险任务。因为满足目标的最简单路径可能是:
python
@pytest.mark.skip
def test_payment():
...
或者:
python
assert True
因此项目指令里应该明确:
markdown
## Testing rules
Never:
- delete a failing test only to make CI green;
- skip a failing test without explicit approval;
- weaken an assertion to hide a regression;
- replace real assertions with placeholders.
When a test conflicts with intended behavior,
report the conflict before changing the test.
还可以在 CI 中做简单检查:
bash
git diff --unified=0 origin/main...HEAD \
| grep -E '^\+.*(skip|xfail)'
当然这不是完整安全方案,但至少可以发现部分危险修改。
十一、为什么这类工作更适合 Pro
AI 测试工程不是一次性生成代码。真实流程通常是:
text
读模块
→ 读测试
→ 做风险分析
→ 写测试
→ 执行
→ 分析失败
→ 修改生产代码
→ 再执行
→ Review diff
→ 更新 CI
一次中等复杂度任务就可能产生很多轮交互。
这也是为什么我更推荐重度开发者关注 ChatGPT Pro + Codex,而不是单纯比较一次回答。
当前官方资料显示,Pro 的 GPT‑6 Astra 正逐步覆盖 Chat、Work 与 Codex;Work/Codex 的 Astra 使用上,Pro 可以使用其完整现有 allowance,而 Plus 属于较有限的 Astra 使用。
对于偶尔写几个测试的人,这不是决定性因素。但对于每天运行大量重构、回归和 CI 分析的工程师来说,这种持续可用性更有意义。
十二、最终建议:把 AI 放在测试"设计和执行之间"
AI 不应该代替测试。正确关系更像:
text
需求
↓
GPT‑6 Astra 分析风险
↓
Codex 编写/修改测试
↓
测试框架执行
↓
确定性结果
↓
GPT‑6 Astra 分析失败
↓
Codex 修改
↓
再次执行
这里每个环节都能相互约束。模型负责处理模糊问题,测试框架负责给出确定结果,开发者负责判断业务含义。
结语
GPT‑6 Astra 和 Codex 带来的真正变化,不是"以后不用写测试"。恰恰相反。
AI 让我们第一次有机会低成本地把更多精力放到失败模式、边界条件、回归风险、副作用、并发和兼容性这些过去经常因为时间不够而被忽略的部分。
对于 ChatGPT Pro 用户,最值得建立的习惯不是一直让模型写更多代码,而是把 Pro + Codex 变成一个持续运行的工程闭环:先分析,再实现;先测试,再相信;测试失败先定位,不要直接改;最终用 diff 和确定性工具验收。
当 GPT6 进入软件工程之后,这种工作方式比任何单条"神级提示词"都更重要。