Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pairwise Testing Combination generator in Python

I am trying pairwise testing and wanted Python based pairwise testing tool. I already tried AllPairs(http://pypi.python.org/pypi/AllPairs/2.0.1). It has bug when I give 10 entries in a Column. Currently using Microsoft PICT to generate pairwise combination.

Is there any tool in Python that generates pairwise combinations for large arrays?

Bug in AllPairs If I give this

parameters = [ [ "Brand X", "Brand Y","Brand A","Brand B","Brand C","Brand D" ]
             , [ "98", "NT", "2000", "XP"]
             , [ "Internal", "Modem","A","B","C","D","E","F","G","H","I","J","K","L","M" ]
             , [ "Salaried", "Hourly", "Part-Time", "Contr.","AA","BB","CC","DD","EE","FF","GG","HH","II" ]
             , [ 6, 10, 15, 30, 60, 70, 80, 90, 100, 110, 120, 130, 140 ]
             ]

Output is

Brand X count is 16
Brand Y count is 122
Brand A count is 16
Brand B count is 16
Brand C count is 16
Brand D count is 15

for this

parameters = [ [ "Brand X", "Brand Y","Brand A","Brand B","Brand C","Brand D" ]
             , [ "98", "NT", "2000", "XP"]
             , [ "Internal", "Modem" ]
             , [ "Salaried", "Hourly", "Part-Time", "Contr." ]
             , [ 6, 10, 15, 30, 60 ]
             ]

output is

Brand X count is 5
Brand Y count is 5
Brand A count is 5
Brand B count is 5
Brand C count is 5
Brand D count is 6

I think, it is not correct working for larger array.

like image 644
Rajasankar Avatar asked Jun 08 '12 05:06

Rajasankar


1 Answers

How about something like this?

from itertools import chain, combinations, product

def pairwiseGen(*sequences):
    unseen = set(chain.from_iterable(product(*i) for i in combinations(sequences, 2)))
    for path in product(*sequences):
        common_pairs = set(combinations(path, 2)) & unseen
        if common_pairs:
            yield path
            unseen.difference_update(common_pairs)

Usage (using the parameters list you defined in your question):

>>> pairs = list(pairwiseGen(*parameters))
>>> len(pairs)
846

I think there is still some room for optimization (the above generator yielded slightly more results than were expected), but I think you'll agree that it's much shorter than using the cartesian product:

>>> all_possible = list(product(*parameters))
>>> len(all_possible)
60480
like image 135
Joel Cornett Avatar answered Nov 14 '22 23:11

Joel Cornett