查找每个可能的测试用例的输入 [英] Finding inputs of every possible test case

查看:0
本文介绍了查找每个可能的测试用例的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Python程序,它接受大约40个用户输入,并返回对他们寿命的预测。用户输入的内容主要是特定的或有限的,例如性别、吸烟状况和出生年份。

我希望通过测试每个字段的所有可接受值来最大化测试用例,例如sex:['Male', 'Female', None]。有什么好方法可以在不使用数十个嵌套的for循环的情况下做到这一点吗?例如,itertools函数。我在考虑类似SCRICIT-LEARN的网格搜索,您可以列出可接受的值,然后它通过检查所有可能的组合来启动超参数优化

我要避免:

for sex in ['male', 'female', None]:
    for smoking_status in ['smoker', 'non-smoker', None]:
        for birth_year in [1900, ..., 2022, None]:
           assert(myfunc(sex, smoking_status, birth_year) == trueOutput)
假设trueOutput是动态的,并且总是给出正确的值(我计划将Python输出交叉引用到Excel电子表格,并为每个测试用例重写Excel输入,以获得更新的输出)。我还计划将所有可能的测试用例写入表示特定用户数据的JSON文件,这样我就可以测试失败的用例

推荐答案

您要这样使用itertools.product

sex = ['m', 'f', None]
smoking = ['smoker', 'non-smoker', None]
birth = [1999, 2000, 2001, None]

for item in itertools.product(sex, smoking, birth):
    print(item)

要将参数传递给函数,请使用扩散运算符:

sex = ['m', 'f', None]
smoking = ['smoker', 'non-smoker', None]
birth = [1999, 2000, 2001, None]

for item in itertools.product(sex, smoking, birth):
     assert myfunc(*item) == trueOutput
# or
for se, sm, b in itertools.product(sex, smoking, birth):
     assert myfunc(se, sm, b) == trueOutput

这篇关于查找每个可能的测试用例的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆