条件组合覆盖
一、定义:
判定中条件的各种组合都至少被执行一次
二、特点:
1、满足条件组合覆盖的用例一定满足语句覆盖
2、满足条件组合覆盖的用例一定满足条件覆盖
3、满足条件组合覆盖的用例一定满足判定覆盖
4、满足条件组合覆盖的用例一定满足条件判定覆盖
5、条件组合覆盖没有考虑各判定结果(真或假)组合情况,不满足路径覆盖
6、条件组合数量大,设计测试用例的时间花费较多
三、 程序流程图:
四、源码:
demo.py
#encoding:utf-8
A = int(input('请输入A的值'))
B = int(input('请输入B的值'))
X = int(input('请输入X的值'))
if A > 1 and B == 0:
X = X / A
if A == 2 or X > 1:
X = X + 1
print('结束')
五、测试用例设计示例
用例编号 | 测试用例 | 覆盖条件 | 覆盖路径 | 预期结果 |
---|---|---|---|---|
1 | A=2,B=0,X=4 | (A>1) and (B== 0), (A==2) or (X>1) | a-c-e | X = 3 |
2 | A=2,B=1,X=1 | (A>1) and (B!=0), (A==2) or (X<=1) | a-b-e | X = 2 |
3 | A=1,B=0,X=2 | (A<=1) and (B==0), (A!=2) or (X>1) | a-b-e | X = 3 |
4 | A=1,B=1,X=1 | (A<=1) and (B!=0), (A!=2) or (X<=1) | a-b-d | X = 1 |
执行用例1 ,判定(A > 1 and B == 0)为真 ,执行 X = X / A, X=2;
判定(A == 2 or X > 1)为真,执行X = X + 1 ;
输出 X = 3 ;
程序结束
执行用例2 ,判定(A > 1 and B == 0)为假 ,不执行 X = X / A ;
判定(A == 2 or X > 1)为真,执行X = X + 1 ,X=2 ;
输出 X = 2 ;
程序结束
执行用例3 ,判定(A > 1 and B == 0)为假 ,不执行 X = X / A ;
判定(A == 2 or X > 1)为真,执行X = X + 1 ,X=2 ;
输出 X = 3 ;
程序结束
执行用例4 ,判定(A > 1 and B == 0)为假,不执行X = X / A ;
判定(A == 2 or X > 1)为假,不执行X = X + 1 ;
输出 X = 1 ;
程序结束
从上述用例可以得出:
1、满足条件判定覆盖的测试用例满足语句覆盖
2、满足条件判定覆盖的测试用例满足条件覆盖,判定覆盖 ,条件判定覆盖
3、上述用例未考虑每个判定的真假组合情况(路径覆盖),程序所有路径没有覆盖
六、使用Python Unittest 实现上述用例
# encoding:utf-8
import unittest
class TestDemo(unittest.TestCase):
def demo(self, A, B, X):
if A > 1 and B == 0:
X = X / A
if A == 2 or X > 1:
X = X + 1
return X
def test_demo_with_conditional_combination_coverage_1(self):
'''
使用条件组合覆盖测试 方法demo
A=2,B=0,X=4
'''
X = self.demo(A=2, B=0, X=4)
expected = 3
self.assertEqual(expected, X)
def test_demo_with_conditional_combination_coverage_2(self):
'''
使用条件组合覆盖测试 方法demo
A=2,B=1,X=1
'''
X = self.demo(A=2, B=1, X=1)
expected = 2
self.assertEqual(expected, X)
def test_demo_with_conditional_combination_coverage_3(self):
'''
使用条件组合覆盖测试 方法demo
A=1,B=0,X=2
'''
X = self.demo(A=1, B=0, X=2)
expected = 3
self.assertEqual(expected, X)
def test_demo_with_conditional_and_decision_coverage_4(self):
'''
使用条件组合覆盖测试 方法demo
A=1,B=1,X=1
'''
X = self.demo(A=1, B=1, X=1)
expected = 1
self.assertEqual(expected, X)
if __name__ == '__main__':
unittest.main()