ISO26262 Part1 之 测试中的覆盖度指标的区别及应用

1.目的

1.1 Refer to Part 6 Software unit verification 9.4.4

目的:为了评估验证的完整性并提供证据证明已充分实现单元测试目标,应确定在软件单元层面的要求覆盖率,同时应按照表9列出的度量对结构覆盖率进行测定。
在这里插入图片描述

– 结构覆盖率分析可以显示基于需求的测试用例的不足、要求的缺陷、无作用码、无效代码或非预期功能

1.2 Refer to Part 6 Software integration and verification 10.4.5

目的:为了评估测试用例的完整性,并提供证据证明已充分实现集成测试的测试目标,应按照表12列出的方法来评估结构覆盖率。
在这里插入图片描述

– 结构覆盖率分析可以显示基于需求的测试用例的不足、要求的缺陷、无作用码、无效代码或非预期功能

2. 定义

在这里,设计一个程序实例来对不同的覆盖率进行解释:

if A and B then Action1 
if C or D then Action2 

2.1 语句覆盖率 Statement coverage

语句覆盖是一种白盒测试方法,是指选择足够的测试用例,使得运行这些测试用例时,被测程序的每一个语句至少执行一次;
It is used to design test box cases where it will find out the total number of executed statements out of the total statements present in the code

可以用来发现已执行的语句占所有语句的比例

Formula:

Statement coverage = (Number of executed statements / Total number of statements in source code) * 100

Statement coverage covers:

  • Dead code.
  • Unused statements.
  • Unused branches.
  • Missing statements.

Why is statement coverage used?

  • To check the quality of the code.
  • To determine the flow of different paths of the program.
  • Check whether the source code expected to perform is valid or not.
  • tests the software’s internal coding and infrastructure.

Drawback of Statement Coverage:

  • Cannot check the false condition.
  • Different input values to check all the conditions.
  • More than one test case may be required to cover all the paths with a coverage of 100%.

因此,针对语句覆盖,设计测试用例使得

A=true B=true C=true 

即可

2.2 分支覆盖率 Branch coverage

分支覆盖确保在每个判断节点带来的每个分支至少执行一次,True和False都需要执行;若程序中的判定是有几个条件联合构成时,它未必能发现每个条件的错误。
The main goal to ensure that each one of the possible branches from each decision point is executed at least once and thereby ensuring that all reachable code is executed. In the branch testing, each outcome from a code module is tested as if the outcomes are binary, you need to test both True and False outcomes.

设计用例

(1)A=true,B=true,C=true,D=false 真分支覆盖

(2)A=true,B=false,C=false,D=false。假分支覆盖

Formula:

分支覆盖 = 已执行的分支数目 / 分支总数目 * 100%

2.3 MC/DC 修改条件/判定覆盖率 (Modified Condition/Decision Coverage)

定义:

Modified Condition/Decision Coverage —— every point of entry and exit in the program has been invoked at least once,every condition in the program has taken all possible outcomes at least once,and each condition in a decision has been shown to independently affect a decision S outcome by varying just that condition while holding fixed all other possible conditions.

Condition —— a Boolean expression containing no Boolean operators:

Decision —— a Boolean expression composed of conditions and zero or more Boolean operators:

修正条件判定覆盖要求在一个程序中每一种输入输出至少得出现一次,在程序中的每一个条件必须产生所有可能的输出结果至少一次,并且每一个判定中的每一个条件必须能够独立影响一个判定的输出,即在其他条件不变的前提下仅改变这个条件的值,而使判定结果改变。
在这里插入图片描述

2.4 函数覆盖率 Function coverage

Function coverage reports whether each function is invoked at least once to find unused functions.
函数覆盖率统计所有的函数及子函数是否至少被执行一次

2.5 调用覆盖率 Call coverage

Call coverage reports each call found in the code is executed at least once.
调用覆盖率统计每一次函数调用端是否至少被执行一次

举例:

在这个例子中,我们有三个函数,但是在main函数中,因为if的条件为假,所以function3(4),永远不会执行,通过两种覆盖率的概念我们可以很快的计算出对应的覆盖率数值:

函数覆盖率为100%,因为所有的子函数都至少执行了一次。

调用覆盖率为75%,因为所有调用4次的函数中,function3(4)没有被执行。

所以说,100%的函数覆盖率不意味着100%的调用覆盖率,调用覆盖率能帮助我们发现需求错误,程序错误,死代码等问题
在这里插入图片描述

————————————————

理解了第一个例子,那我们继续来看一看第二个例子。在第二个例子中,代码执行情况又不同了,main函数中的if条件为真,那function1(1),function1(2)一定会执行,但是function3()根本就没有被调用,那我们不难计算出两种覆盖率:

函数覆盖率为66.6%,因为所有的子函数中function3(int c)压根就没执行。

调用覆盖率为100%,因为所有调用2次的函数中,function1(1),function(2)都被执行了。
在这里插入图片描述

所以例子二能说明,100%的调用覆盖率不意味着100%的函数覆盖率,**函数覆盖这个指标能帮助我们发现多余的未使用的函数,**进而优化我们的代码。

3. 参考引用

  1. https://www.geeksforgeeks.org/statement-coverage-testing/?ref=header_search
  2. https://www.geeksforgeeks.org/branch-software-testing/?ref=lbp
  3. https://www.bilibili.com/video/BV1ZJ4m1K7Sb?vd_source=741c7c102c2f67d3140218cd4c692cbd
  4. https://www.cnblogs.com/ChenM666/p/16137844.html
  5. https://blog.csdn.net/wind_08/article/details/6959669
  6. https://blog.csdn.net/parasoftchina/article/details/124602298
  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值