White-box testing series (three) condition coverage

Condition coverage

First, the definition:

Each program is determined to obtain the various conditions of the respective possible values ​​satisfies at least one

Second, characteristics:

1, to cover up for the lack of judgment - judgment of the entire final value (true or false) to measure
2, condition coverage may not be able to meet the decision coverage
3, condition coverage may not be able to meet the statement coverage

Third, the program flow:

Fourth, the source code:

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

Fifth, test case design

Use Case No. Test Case Coverage Path expected results
1 A=2,B=1,X=4 a-b-e X = 5
2 A=-1,B=0,X=1 a-b-d X = 1

Execution Example 1, determination (A> 1 and B == 0 ) is false, do not perform X = X / A;
determination (A == 2 or X> 1 ) is true, execute X = X + 1;
the value X = 5;
end of program

Example 2 execution determination (A> 1 and B == 0 ) is false, do not perform X = X / A;
determination (A == 2 or X> 1 ) is false, do not perform X = X + 1;
outputs X = 1;
end program

Can be derived from the above use cases:
1, the above-described embodiments use missed covered branch "c"; covering satisfy the condition, the determination is not satisfied but the cover
2, none of the above embodiment executed by the statement X = X / A; coverage satisfies the condition, but does not satisfy the statement coverage

Sixth, by using the above-described embodiments achieve 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_coverage_1(self):
        '''
        使用条件覆盖测试 方法demo
        A=2,B=1,X=4
        '''
        X = self.demo(A=2, B=1, X=4)
        expected = 5
        self.assertEqual(expected, X)

    def test_demo_with_conditional_coverage_2(self):
        '''
        使用条件覆盖测试 方法demo
        A=-1,B=0,X=1
        '''
        X = self.demo(A=-1, B=0, X=1)
        expected = 1
        self.assertEqual(expected, X)

if __name__ == '__main__':
    unittest.main()




Guess you like

Origin www.cnblogs.com/snailrunning/p/11013368.html