Python中的成对测试组合生成器

2024-04-25 05:48:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Python中有没有工具可以为大型数组生成成对组合?在

所有对中的Bug 如果我给你这个

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

输出是

^{pr2}$

为了这个

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

我认为,对于较大的阵列,这是不正确的。在


Tags: pypitimeiscount测试工具xpinternalparameters
3条回答

我刚刚为mspict实现创建了一个包装器,这可能对您有用。显然有依赖性,但节省了重新创建轮子。在

import os
import tempfile
import subprocess


def all_pairs(*sequence):
    """
    Calculate the all pairs testing sequence from the Microsoft PICT all pairs application
    that is compiled from: https://github.com/Microsoft/pict

    >>> Type = ['Single', 'Span', 'Stripe', 'Mirror', 'RAID-5']
    >>> Size = [10, 100, 500, 1000, 5000, 10000, 40000]
    >>> Format_method = ['Quick', 'Slow']
    >>> File_system = ['FAT', 'FAT32', 'NTFS']
    >>> Cluster_size = [512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]
    >>> Compression = ['On', 'Off']
    >>> combinations = all_pairs(Type, Size, Format_method, File_system, Cluster_size, Compression)
    >>> assert len(combinations) == 56
    """
    exe = '/opt/pict/bin/pict'
    assert os.path.exists(exe), 'Make sure you have install the PICT executable to: {}'.format(exe)
    assert len(sequence) > 0
    for s in sequence:
        assert isinstance(s, list)
        assert len(s) > 0

    # create input file
    lines = list()
    for i, s in enumerate(sequence):
        seq = ', '.join([str(r) for r in range(len(s))])
        new_line = '{}: {}'.format(i, seq)
        lines.append(new_line)

    with tempfile.NamedTemporaryFile(mode='w') as tmp:
        tmp.write(os.linesep.join(lines))
        tmp.flush()
        result = subprocess.check_output([exe, tmp.name])

    options = list()
    for line in result.split(os.linesep)[1:]:  # skip the header
        if len(line) == 0:
            continue
        indices = [int(i) for i in line.split('\t')]
        options.append([s[i] for s, i in zip(sequence, indices)])
    return options

TL;DRAllPairs返回一个有效的成对测试集,尽管品牌和Windows版本的分布不均匀。在

示例

以下是您的示例:

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

有效性:

如果您检查,您会发现PICT和AllPairs都包含所有993个2元组。这意味着PICT和AllPairs都返回有效集。在

上述示例中的2元组的总数为:

^{2}$

性能:

典型的测试用例数为O(nm),其中n和m是最大的两个参数。在这种情况下:15*13=195是测试集的绝对最小数量。在

AllPairs返回200个5元组

PICT返回206个5元组

因此PICT和AllPairs返回到测试集的最小数量。在

分布

Raj指出,AllPairs返回的测试集分布不均匀。在

品牌:

Brand A : 16
Brand B : 16
Brand C : 16
Brand D : 15
Brand X : 16
Brand Y : 122

版本:

98 : 155
2000 : 15
NT : 16
XP : 15

PICT返回分布更均匀的测试集

品牌:

Brand A : 26
Brand B : 32
Brand C : 40
Brand D : 35
Brand X : 40
Brand Y : 33

版本:

98 : 48
2000 : 55
NT : 52
XP : 51

说明

品牌和版本分布不均仍然有效的原因是这两个字段的基数最低(6和4)。所以AllPairs似乎用大量的Brand YWindows 98来填充网络和工资所需的5元组

以下是每个字段的基数:

Brand : 6
Version : 4
Network : 15
Wage : 13
Time : 13

这样的怎么样?在

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)

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

^{2}$

我认为仍有一些优化空间(上面的生成器生成的结果比预期的稍多),但我认为您会同意它比使用笛卡尔积要短得多:

>>> all_possible = list(product(*parameters))
>>> len(all_possible)
60480

相关问题 更多 >