我必须说的第一件事 - 重构它!
将其分解为几个更容易验证、反转逻辑和提前退出的 if 语句。如果没有看到实际代码和上下文,很难给出更详细的建议。
另外,如果(b == 3000700),那么&&(b>=30 && b<=33) 返回false,这使得这部分语句||(b==3000700) 毫无意义。也许它应该是(a == 3000700)?
关于测试用例...再次,如果没有看到完整的代码 sn-p 并了解上下文,提供有意义的建议有点困难。不过我还是会试试的。
让我们看看每个变量的“临界值”。
- 变量a:
3000710,任何其他
- 变量b:
3000700, [30, 33], any
other
- 变量c:
1, [4, 6], any other
- 变量 v:
1.0.9, 2.0.0, any other
使用测试理论(等价划分和边界值分析),我们可以限制上述“关键”值列表。
[30, 33] => 30, 31, 33 (The value outside of this range is already covered by "any other")
[4, 6] => 4, 5, 6 (The value outside of this range is already covered by "any other". Though we did't really change anything in this case)
Nunit 有一个属性[Combinatorial],它为为测试参数提供的各个数据项的所有可能组合生成测试用例。
*假设:变量a, b, c是int类型,变量v是字符串
代码看起来像这样:
[Test, Combinatorial]
public void FirstTest(
[Values(3000710, 0)] int a,
[Values(30, 31, 33, 3000700, 0)] int b,
[Values(1, 4, 5, 6, 0)] int c,
[Values("1.0.9", "2.0.0", "")] string v)
{
RunTestMethod(a, b, c, v);
}
您只需要在测试执行时存储生成的测试数据