参数化(数据驱动)测试使用多组输入和预期输出运行相同的测试逻辑 — 避免为每个场景重复编写测试。它们使得简洁地测试多个场景(包括边界情况)变得容易。
问题:重复的测试
Without parameterization, testing many input/output cases means COPYING the test:
test('add 2+3', () => expect(add(2,3)).toBe(5));
test('add 0+0', () => expect(add(0,0)).toBe(0));
test('add -1+1', () => expect(add(-1,1)).toBe(0));
→ lots of near-identical, repetitive tests (tedious, hard to maintain)
参数化测试 — 一个测试,多个场景
test.([
[, , ],
[, , ],
[-, , ],
[, , ],
])(, {
((a, b)).(expected);
});
