访问所有组合的最佳方式是什么

发布于 2024-10-31 03:53:05 字数 384 浏览 0 评论 0原文

作为测试人员的工作,我关心的问题之一是始终确保完整的测试覆盖率。这可能会变得很困难,因为有时可能的组合数量确实很多。让我们举个例子。泡茶的一个很好的例子

  • 要泡茶,您可以使用红茶、绿茶或白茶。 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别忘他 2024-11-07 03:53:05

它可能有点依赖于编程语言......但您基本上是在寻找参数集的笛卡尔积。

例如,在Python中

import itertools

for args in itertools.product(
      ['black tea','green tea','white tea'],
      ['milk','water'],
      ['sugar','honey','none'],
      ['iced','hot']
   ): 
   drink_tea(*args)

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

import itertools

for args in itertools.product(
      ['black tea','green tea','white tea'],
      ['milk','water'],
      ['sugar','honey','none'],
      ['iced','hot']
   ): 
   drink_tea(*args)
醉梦枕江山 2024-11-07 03:53:05

是的。嵌套 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.

西瓜 2024-11-07 03:53:05

嵌套循环可能会变得非常混乱。我已经递归地编写了测试生成器以避免这种情况,并且还推广到多个维度。

fun build_test_args(possible_values, i, current_values):
     if i>len(possible_values):
         do_test(current_values)
     for value in possible_values[i]:
         cur_list = current_values + [value]
         build_test_args(possible_values, i+1, cur_list)

Nested looping can get pretty messy. I've written test generators recursively to avoid this and also generalize to multiple dimensions.

fun build_test_args(possible_values, i, current_values):
     if i>len(possible_values):
         do_test(current_values)
     for value in possible_values[i]:
         cur_list = current_values + [value]
         build_test_args(possible_values, i+1, cur_list)
荆棘i 2024-11-07 03:53:05

基本上你正在形成一个矩阵......每个变量都是另一个维度。您只需将该矩阵与其自身相乘...

含义...嵌套循环就可以很好地工作。

创建一些数组...然后开始吧!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文