--- 原理:在正交实验法中,性别、班级、年龄区间这三个被测元素称为因素,每个因素的取值称之为水平值。
from allpairspy import AllPairs
def is_valid_combination(row): # row单个测试用例入参,例如:["男", "四年级", "8-10岁"]
n = len(row) # n=3
if n > 2: # 筛选条件
if "一年级" == row[1] and "10-13岁" == row[2]:
return False
return True
parameters = [
["男", "女"],
["一年级", "二年级", "三年级", "四年级", "五年级"],
["8岁以下", "8-10岁", "10-13岁"],
]
print("pairwise:")
# 随机生成单个测试用例,filter_func用于筛选
for i, pairs in enumerate(AllPairs(parameters, filter_func=is_valid_combination)):
print("用例编号{:2d}:{}".format(i, pairs))
输出结果
![]()
结合pytest
import pytest
from allpairspy import AllPairs
def function_to_be_tested(sex, grade, age):
if grade == "一年级" and age == "10-13岁":
return False
return True
class TestParameterized(object):
# 生成单个测试用例
@pytest.mark.parametrize(["sex", "grade", "age"], [
value_list for value_list in AllPairs([
["男", "女"],
["一年级", "二年级", "三年级", "四年级", "五年级"],
["8岁以下", "8-10岁", "10-13岁"]
])
])
def test(self, sex, grade, age):
assert function_to_be_tested(sex, grade, age)
运行结果
![]()