【发布时间】:2018-11-04 21:40:55
【问题描述】:
我正在学习 PHPUnit,但一开始,一些微不足道的事情让我跌跌撞撞。
这是 src 文件夹中的收据 PHP 文件 -
namespace TDD;
class Receipt {
public function total(array $items = []) {
return array_sum($items);
}
}
这就是测试文件夹中的 ReceiptTest.php 文件 -
namespace TDD\Test;
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
use PHPUnit\Framework\TestCase;
use TDD\Receipt;
class ReceiptTest extends TestCase {
public function testTotal() {
$Receipt = new Receipt();
$this->assertEquals(
14,
$Receipt->total([0,2,5,8]),
'When summming the total should equal 15'
);
}
}
这就是我运行 vendor\bin\phpunit 时的错误日志 -
PHPUnit 7.0.0 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 98 ms, Memory: 4.00MB
There was 1 error:
1) TDD\Test\ReceiptTest::testTotal
Error: Class 'TDD\Receipt' not found
C:\xampp\htdocs\PHPUnit\tests\ReceiptTest.php:10
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
【问题讨论】:
标签: unit-testing namespaces phpunit