访问所有组合的最佳方式是什么
作为测试人员的工作,我关心的问题之一是始终确保完整的测试覆盖率。这可能会变得很困难,因为有时可能的组合数量确实很多。让我们举个例子。泡茶的一个很好的例子
要泡茶,您可以使用红茶、绿茶或白茶。 (3 个变量)
您可以使用牛奶或水(2 个变量)
您可以使用糖或蜂蜜或不使用(3 个变量)
你可以将其冰镇或热(2个变量)
如你所见,现在如果我想要要测试所有可能的泡茶方法(假设有一个假设的软件可以创建各种茶),那么我必须测试: 3x2x3x2 组合 = 36,因为确实有 36 种独特的泡茶方法
算法是什么在这种情况下最好。我认为嵌套 for 循环是最好的。我说得对吗?
As a job of a tester, one of my concerns is to always ensure complete test coverage. This can get hard since sometimes the number of possible combinations are really a lot. Lets take an example for instance. A good ol' example of making tea
To make tea you could use black tea, green tea or white tea. (3 variables)
You could use milk or water (2 variables)
you could use sugar or honey or none (3 variables)
You could have it iced or hot (2 variables)
As you can see, now if i want to test all the possible ways to make tea (assuming there is a hypothetical software which allows creation of a variety of teas), then i have to test: 3x2x3x2 combinations = 36, because there are indeed 36 unique ways to make tea
What algorithm is best in such a case. I can see a nested for loop being best. Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它可能有点依赖于编程语言......但您基本上是在寻找参数集的笛卡尔积。
例如,在Python中
It can be a bit programming language-dependent... but you're basically looking for the cartesian product of the set of arguments.
for example, in Python
是的。嵌套 for 循环最适合执行所有可能的组合。
值得注意的是,这对于大多数生产代码来说是不可行的,因为它们通常有数十个重要输入,每个输入都有多个输入系列。
Yep. The nested for loop is best for doing all possible combinations.
It's worth noting that this isn't feasable for most production code, which often have dozens of significant inputs, each with multiple input families.
嵌套循环可能会变得非常混乱。我已经递归地编写了测试生成器以避免这种情况,并且还推广到多个维度。
Nested looping can get pretty messy. I've written test generators recursively to avoid this and also generalize to multiple dimensions.
基本上你正在形成一个矩阵......每个变量都是另一个维度。您只需将该矩阵与其自身相乘...
含义...嵌套循环就可以很好地工作。
创建一些数组...然后开始吧!
Basically you are forming a matrix... with each variable being another dimension. You would simply multiply this matrix by itself...
MEANING... nested loops would work well.
Create some arrays... and get cranking!