Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way of accessing all combinations

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?

like image 961
7hacker Avatar asked Oct 11 '22 08:10

7hacker


1 Answers

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)
like image 186
Jimmy Avatar answered Oct 18 '22 10:10

Jimmy