单元测试使用疑问
疑问:如果示例代码需要代码覆盖率达到100%,单元测试是否要针对不同参数的组合进行多次执行?
有看到文档上提到数据提供者,但是实际场景可能校验的地方次数会很多,并且不同的参数组合,请问有什么优雅的解决方案吗?
示例代码
/**
* 测试验证参数 * Created by Snakelis * Date: 2021/2/25 17:15 * @throws
*/
public function testCheckData()
{
$params = [];
$result = $this->checkData($params);
$this->assertTrue($result);
}
/**
* 验证参数 * Created by Snakelis * Date: 2021/2/25 17:14 * @param $params
* [@return](https://learnku.com/users/31554) bool
* @throws
*/
public function checkData($params)
{
if (empty($params['a'])) {
throw new Exception('...');
}
if (empty($params['b'])) {
throw new Exception('...');
}
if (empty($params['c'])) {
throw new Exception('...');
}
return true;
}
我理解的是通过模拟各种输入参数情况,这其中要覆盖到你的代码各种条件判断,并断言你的代码输出是否符合你的预期