【发布时间】:2015-05-06 12:28:51
【问题描述】:
对于下面的函数我需要写更多的测试用例,我已经写了一个,谁能给一些想法,也许是为了测试中间函数调用的返回值。
public function calculateShortestPath($graphObj, $start, $destination)
{
$shortestPath = null;
if ($this->validateParams($graphObj, $start, $destination) == true) {
$result = $this->getAllVerticesAndNeighbours($graphObj);
$vertices = $result[self::VERTICES];
$neighbours = $result[self::NEIGHBOURS];
$vertexCost = array_fill_keys($vertices, self::INFINITY);
$visitedVertices = array_fill_keys($vertices, null);
$vertexCost[$start] = 0;
$vertexQueue = $vertices;
$result = $this->getShortestPath($vertexQueue, $vertexCost, $neighbours, $visitedVertices, $destination);
$vertexCost = $result[self::VERTEX_COST];
$shortestPathVertices = $result[self::SHORTEST_PATH_VERTICES];
$path = $this->getRefinedShortestPath($shortestPathVertices, $destination);
$shortestPath = new ShortestPath($path, $vertexCost[$destination]);
}
return $shortestPath;
}
我已经写过下面的案例了,
/**
* Test for calculateShortestPath function
*
* @param string|int $start starting point
* @param string|int $destination destination point
* @param array $expectedShortestPath expected shortest path
* @param int|float $expectedCost expected cost
* @dataProvider testCalculateShortestPathDataProvider
*/
public function testCalculateShortestPath($start, $destination, $expectedShortestPath, $expectedCost)
{
$actualResult = $this->shortestPathCalc->calculateShortestPath($this->graph, $start, $destination);
/* @var $actualResult ShortestPath */
$this->assertEquals(
$expectedShortestPath,
$actualResult->getPath(),
sprintf('Incorrect shortest path from %d to %d !', $start, $destination)
);
$this->assertEquals(
$expectedCost,
$actualResult->getCost(),
sprintf('Incorrect shortest path cost from %d to %d !', $start, $destination)
);
}
【问题讨论】:
-
我需要写更多的测试用例 -- 为什么?
-
只是为了确保函数不会在中间调用中中断,还有其他意见吗?我也为中间函数编写了单独的测试
-
我不确定这种方法的作用和方式。所以我不能建议你如何测试你的 API。我只能提供常见的建议:尝试寻找边缘案例,探索覆盖范围。
标签: php unit-testing symfony phpunit