用于Python的Pairwaise测试组合生成器

我正在尝试成对测试,并希望基于Python的成对测试工具.我已经尝试过AllPairs(http://pypi.python.org/pypi/AllPairs/2.0.1).当我在列中输入10个条目时,它有bug.目前使用Microsoft PICT生成成对组合.

Python中是否有任何工具可以为大型数组生成成对组合?

AllPairs中的错误
如果我给这个

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 ]
             ]

输出是

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

为了这

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 ]
             ]

输出是

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

我认为,对于更大的阵列工作是不正确的.

解决方法:

这样的事怎么样?

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)

用法(使用您在问题中定义的参数列表):

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

我认为还有一些优化空间(上面的发生器产生的结果比预期的要多一些),但我认为你会同意它比使用笛卡尔积更短:

>>> all_possible = list(product(*parameters))
>>> len(all_possible)
60480
上一篇:java – 使用字典顺序和按位算法顺序生成有限集的所有组合


下一篇:C++ 练习题