0

作为主题,我想生成测试数据以涵盖某些逻辑表达式的所有可能条件,如下所示:

 ((a==3000710)||(b==3000700))
   &&(b>=30 && b<=33)
   &&((c==1)||(c>=4 && c<=6))
   &&((v==1.0.9)||(v==2.0.0))

欢迎任何评论。

顺便说一句,逻辑表达式是应用于我们后端服务器的简化规则。

4

1 回答 1

1

我必须说的第一件事 - 重构它!将其分解为几个更容易验证、反转逻辑和提前退出的 if 语句。如果没有看到实际的代码和上下文,很难给出更详细的建议。

另一件事 if(b == 3000700)&&(b>=30 && b<=33)返回 false,这使得这部分语句||(b==3000700)毫无意义。也许它应该是(a == 3000700)

关于测试用例......再次,如果没有看到完整的代码片段并了解上下文,提供有意义的建议有点困难。但无论如何我都会尝试。

让我们看看每个变量的“临界值”。

  • 变量 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);
        }

您只需要在测试执行时存储生成的测试数据

于 2017-07-10T09:19:57.697 回答