白盒测试用例设计方法

假设我们有以下待测试程序 :

python 复制代码
def func(a, b, x):
    if a > 1 and b == 0:
        x = x / a
    if a == 2 or x > 1
        x = x + 1
    return x

语句覆盖

语句覆盖:每条语句至少执行一次。

使用此准则测试上述函数, 只需要遍历路径 ace, 即可使得所有语句执行一次。

对应测试用例为 : a = 2, b = 0, x = 4

判定覆盖

判定覆盖:又叫分支覆盖, 每个判定的所有可能结果至少出现一次。

只需要涵盖路径 aceabd, 或 acdabe, 就可以使得两个判定为 "真" 和 "假" 的分支都执行一次。

前者 :

  • a = 2, b = 0, x = 4 -> ace √ √
  • a = 0, b = 1, x = 1 -> abd × ×

后者 :

  • a = 2, b = 0, x = 1 -> acd √ ×
  • a = 2, b = 1, x = 2 -> abe × √

条件覆盖

条件覆盖:每个条件的所有可能结果至少执行一次。

第一个判断的所有条件的可能取值情况 : <math xmlns="http://www.w3.org/1998/Math/MathML"> a > 1 a > 1 </math>a>1 或 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ≤ 1 a \leq 1 </math>a≤1, <math xmlns="http://www.w3.org/1998/Math/MathML"> b = 0 b = 0 </math>b=0 或 <math xmlns="http://www.w3.org/1998/Math/MathML"> b ≠ 0 b \neq 0 </math>b=0。

第二个判断的所有条件的可能取值情况 : <math xmlns="http://www.w3.org/1998/Math/MathML"> a = 2 a = 2 </math>a=2 或 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ≠ 2 a \neq 2 </math>a=2, <math xmlns="http://www.w3.org/1998/Math/MathML"> x > 0 x > 0 </math>x>0 或 <math xmlns="http://www.w3.org/1998/Math/MathML"> x ≤ 1 x \leq 1 </math>x≤1。

  • a = 1, b = 0, x = 3 : 触发 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ≤ 1 a \leq 1 </math>a≤1 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> b = 0 b=0 </math>b=0 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ≠ 2 a \neq 2 </math>a=2 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> x > 1 x > 1 </math>x>1。(出于简单起见, 这里不考虑逻辑中断的发生)

  • a = 2, b = 1, x = 1 : 触发 <math xmlns="http://www.w3.org/1998/Math/MathML"> a > 1 a > 1 </math>a>1 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> b ≠ 0 b \neq 0 </math>b=0 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> a = 2 a = 2 </math>a=2 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> x ≤ 1 x \leq 1 </math>x≤1。

组合覆盖

组合覆盖:每个判定中的所有可能的条件结果的组合,以及所有的入口点都至少执行一次。(注意"可能"二字,因为有些组合的情况难以生成。)

为了满足组合条件覆盖的测试用例, 必须覆盖以下八种组合。

  • a = 2, b = 0, x = 4 : 覆盖 1 + 5
  • a = 2, b = 1, x = 1 : 覆盖 2 + 6
  • a = 1, b = 0, x = 2 : 覆盖 3 + 7
  • a = 1, b = 1, x = 1 : 覆盖 4 + 8
相关推荐
大话性能5 小时前
等价类划分与组合测试:提升测试效率与覆盖率的关键
测试
越学不动啦1 天前
七、自动化概念篇
运维·软件测试·自动化·测试
A丹2 天前
学习 Puppeteer 的视口配置对象(Viewport)
测试
论迹3 天前
【测试】-- 测试用例
测试用例·测试
ho0xi3 天前
记一次使用Loadrunner 11监控服务器资源进行配置rpc.rstatd的踩坑
测试
北京_宏哥3 天前
🔥PC端自动化测试实战教程-4-pywinauto 操作PC端应用程序窗口 - 上篇(详细教程)
前端·python·测试
小张已启程5 天前
docker安装及使用
测试
gongzemin6 天前
利用Sentry监控应用里的前后端
前端·后端·测试
WebInfra7 天前
Midscene v0.14 - 新增即时操作接口和深度思考
前端·人工智能·测试
bytebeats8 天前
在Kotlin中编写依赖于时间的可测试协程代码
android·kotlin·测试