我的PHPUnit测试:
public function addWithNegative()
{
$result = $this->calculator->add(-2, -2);
$this->assertEquals(-5, $result);
}我的代码:
public function add($a, $b)
{
return $a + $b;
}我的问题(我不理解的)是,当我运行我的测试时,它仍然是真的/正确的。即使预期的结果应该是-4。
发布于 2018-01-08 17:56:09
你的测试没有通过。它甚至没有被执行,因为它不是一个测试。
默认情况下,PHPUnit将only the public methods whose names start with test视为测试。告诉PHPUnit方法是测试的另一种方法是在其文档块中使用@test annotation。
因此,为了使其成为测试,您可以将function addWithNegative()更改为:
public function testAddWithNegative()
{
$result = $this->calculator->add(-2, -2);
$this->assertEquals(-5, $result);
}或
/**
* @test
*/
public function addWithNegative()
{
$result = $this->calculator->add(-2, -2);
$this->assertEquals(-5, $result);
}https://stackoverflow.com/questions/48147620
复制相似问题