## 结对编程2——单元测试（40，60，61）

2017-03-29 14:29  olivia-sun  阅读(223)  评论(0编辑  收藏  举报

201421123060 谷天鹏

201421123061 常昊

## 单元测试框架

### 计算模块

<?php

class Quiz {
function simplify($mol,$den) {
$Mol =$mol;
$Den =$den;
if($den == 0){ return "分母不为0"; } while ($den) {
$temp =$mol % $den;$mol = $den;$den = $temp; }$temp = $mol; if ($temp != 0) {
$mol =$Mol / $temp;$den = $Den /$temp;
if ($den == 1) { return$mol;
} else {
return "$mol/$den";
}
}
}

function getQuiz($Fra_a,$Fra_b,$Fra_c,$Fra_d,$operator) {$answer = 0;
switch ($operator) { case '+':$mol = ($Fra_a *$Fra_d) + ($Fra_c *$Fra_b);
$den =$Fra_b * $Fra_d;$answer = $this->simplify($mol, $den); break; case '-':$mol = ($Fra_a *$Fra_d) - ($Fra_c *$Fra_b);
$den =$Fra_b * $Fra_d;$answer = $this->simplify($mol, $den); break; case '×':$mol = $Fra_a *$Fra_c;
$den =$Fra_b * $Fra_d;$answer = $this->simplify($mol, $den); break; case '÷':$mol = $Fra_a *$Fra_d;
$den =$Fra_b * $Fra_c;$answer = $this->simplify($mol, $den); break; default:$answer="error";
echo $answer; break; } return$answer;
}
}

## 代码覆盖率查看

php下通过phpunit 与 Xdebug扩展完成代码覆盖率的查看以及报告生成

ide内部查看代码覆盖率

phpunit --coverage-html ./coverage ./QuizTest.class.php

（1）良好的设计

（2）编码规范

（3）必要的注释