白盒測試系列(二)斷定覆蓋(分支覆蓋)

斷定覆蓋(分支覆蓋)

1、定義:

程序中每一個斷定至少有一次爲真值,有一次爲假值,使得程序中每一個分支至少執行一次python

2、特色:

一、知足斷定覆蓋的測試用例必定知足語句覆蓋
二、對整個斷定的最終取值(真或假)進行度量,但斷定內部每個子表達式的取值未被考慮測試

3、 程序流程圖:

4、源碼:

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('結束')

5、測試用例設計

用例編號 測試用例 覆蓋路徑 預期結果
1 A=3,B=0,X=1 a-c-d X = 1/3
2 A=2,B=1,X=3 a-b-e X = 4

執行用例1 ,斷定(A > 1 and B == 0)爲真 ,執行X = X / A ,X =1/3;
斷定(A == 2 or X > 1)爲假,不執行X = X + 1 ; 程序結束code

執行用例2 ,斷定(A > 1 and B == 0)爲假,不執行X = X / A ;
斷定(A == 2 or X > 1)爲真,執行X = X + 1 ,X = 4 ; 程序結束blog

從上述用例能夠得出:
一、斷定覆蓋測試用例必定知足語句覆蓋
二、斷定覆蓋只需考慮每一個斷定真假,每一個分支執行一次便可utf-8

6、語句覆蓋和斷定覆蓋關係圖

7、使用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_decision_coverage_1(self):
        '''
        使用斷定覆蓋測試 方法demo
        A=3,B=0,X=1
        '''
        X = self.demo(A=3, B=0, X=1)
        expected = 1/3
        self.assertEqual(expected, X)

    def test_demo_with_decision_coverage_2(self):
        '''
        使用斷定覆蓋測試 方法demo
        A=2,B=1,X=3
        '''
        X = self.demo(A=2, B=1, X=3)
        expected = 4
        self.assertEqual(expected, X)

if __name__ == '__main__':
    unittest.main()
相關文章
相關標籤/搜索