单元测试-黑和测试-等价测试用例 示意

定义一个简单的功能要求,判断三角形是等边/等腰/一般三角形,要求边长在(1,100)范围;

准备测试代码

1 require_once("./triangle.class.php");  
2 
3 function runTest($a, $b, $c){
4     $triangle = new triangle($a, $b, $c);
5     echo $triangle->getSahpe() . '</br>';
6 }
View Code

准备黑盒等价测试用例

同测试结果图

开发功能代码

 1 <?php
 2 
 3 class triangle {
 4 
 5     protected $a;
 6     protected $b;
 7     protected $c;
 8     protected $errArray = array(
 9         100 => '不能为空',
10         101 => '必须是数字',
11         102 => '必须大于1',
12         103 => '必须小于100',
13         104 => '2边之和必须大于第三边'
14     );
15     protected $shapes = array(
16         's101' => '等边三角形',
17         's102' => '等腰三角形',
18         's103' => '一般三角形',
19     );
20     protected $return = null;
21 
22     public function __construct($a, $b, $c) {
23         $this->a = $a;
24         $this->b = $b;
25         $this->c = $c;
26         
27         $this->_checkEdge($a, 'a');
28         if(!$this->return){
29             $this->_checkEdge($b, 'b');
30         }
31         if(!$this->return){          
32             $this->_checkEdge($c, 'c');
33         }
34         if(!$this->return){
35             $this->_checkEdges($a, $b, $c);
36         }
37         
38         if(!$this->return){
39             $this->_getShape();
40         }
41     }
42     
43     //检查三遍长度是否合法
44     protected function _checkEdge($param, $edge) {
45         if (empty($param)) {
46             $this->return = '边长' . $edge . $this->errArray[100];
47         } elseif (!is_numeric($param)) {
48             $this->return = '边长' . $edge . $this->errArray[101];
49         } elseif ($param <= 1) {
50             $this->return = '边长' . $edge . $this->errArray[102];
51         } elseif ($param >= 100) {
52             $this->return = '边长' . $edge . $this->errArray[103];
53         }
54 
55         return true;
56     }
57     
58     //检查三边之和
59     protected function _checkEdges($param1, $param2, $param3) {
60         $d1 = $param1 + $param2;
61         $d2 = $param1 + $param3;
62         $d3 = $param3 + $param2;
63 
64         if (($d1 <= $param3) || ($d2 <= $param2) || ($d3 <= $param1)) {
65             $this->return = $this->errArray[104];
66         }
67 
68         return true;
69     }
70 
71     //判断形状
72     protected function _getShape() {
73         if (($this->a == $this->b) && ($this->b == $this->c)) {
74             $this->return = $this->shapes['s101'];
75         } elseif (($this->a == $this->b) || ($this->a == $this->c) || ($this->c == $this->b)) {
76             $this->return = $this->shapes['s102'];
77         } else {
78             $this->return = $this->shapes['s103'];
79         }
80     }
81 
82     //获取判断
83     public function getSahpe() {
84         return $this->return;
85     }
86 
87 }
View Code

测试结果

原文地址:https://www.cnblogs.com/dennr/p/5274401.html