How to use combinations of sets as test data

前端 未结 5 1358
陌清茗
陌清茗 2020-12-05 13:45

I would like to test a function with a tuple from a set of fringe cases and normal values. For example, while testing a function which returns true whenever giv

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 14:38

    Interesting question!

    I would do this by picking combinations, something like the following in python. The hardest part is probably first pass verification, i.e. if f(1,2,3) returns true, is that a correct result? Once you have verified that, then this is a good basis for regression testing.

    Probably it's a good idea to make a set of test cases that you know will be all true (e.g. 3,4,5 for this triangle case), and a set of test cases that you know will be all false (e.g. 0,1,inf). Then you can more easily verify the tests are correct.

    # xpermutations from http://code.activestate.com/recipes/190465
    from xpermutations import *
    
    lengths=[-1,0,1,5,10,0,1000,'inf']
    for c in xselections(lengths,3):        # or xuniqueselections
        print c
    
    (-1,-1,-1);
    (-1,-1,0);
    (-1,-1,1);
    (-1,-1,5);
    (-1,-1,10);
    (-1,-1,0);
    (-1,-1,1000);
    (-1,-1,inf);
    (-1,0,-1);
    (-1,0,0);
    ...
    

提交回复
热议问题